Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:51:23+00:00 2026-06-02T20:51:23+00:00

Though a novice in javascript, I need to take javascript variable (an array) reflecting

  • 0

Though a novice in javascript, I need to take javascript variable (an array) reflecting what a user has done on client side and post it to a PHP server page on submit.

It was suggested that I include this as a value in a hidden field in a form to post to the php page. However, since the JS variable is dynamically created by the user, I can’t write to the page for inclusion in the form unless I call a function that refreshes the page. To avoid a double page refresh, I’d prefer to have the submit function both grab the data and simultaneously post it to the php script. AJAX if I understand correctly, should not be needed because I’m okay reloading the page once on submit. I just don’t want to reload twice.

The following uses the function suggested by Andrew to set the js variable and post. Th form posts as I get the other hidden variable in the form but I am not getting the variable set by js, possibly because there is a mistake with the naming of the variables.

  <html>
     <head>
    <style type="text/css">
        select
        {
            width:100px;
        }
    </style>
    <script type="text/Javascript">
        function moveToRightOrLeft(side)
        {
            if (side == 1)
            {
                var list1 = document.getElementById('selectLeft');
                var list2 = document.getElementById('selectRight');
            }
            else
            {
                var list1 = document.getElementById('selectRight');
                var list2 = document.getElementById('selectLeft');
            }

            if (list1.options.length == 0)
            {
                alert('The list is empty');
                return false;
            }
            else
            {
                var selectedItem = list1.options[list1.selectedIndex];
                move(list2, selectedItem.value, selectedItem.text);
                list1.remove(list1.selectedIndex);
                if (list1.options.length > 0)
                    list1.options[0].selected = true;
            }
            return true;
        }

        function move(listBoxTo, optionValue, optionDisplayText)
        {
            var newOption = document.createElement("option");
            newOption.value = optionValue;
            newOption.text = optionDisplayText;
            listBoxTo.add(newOption, null);
            return true;
        }

        function postData(listBoxID)
        {
            var options = document.getElementById(listBoxID).options;
            for (var i = 0; i < options.length; i++) 
            window.location = "posttoserver.php?data="+options[i].value;

        }

        function setTheValue(val) {
    var options = document.getElementById(listBoxID).options;
    var form = document.forms['myForm'];
    hiddenField = oFormObject.elements["data"];
    hiddenField.value = "val";
}
    </script>
    </head>
    <body>
    <select id="selectLeft" multiple="multiple">
        <option value="1">Value 1</option>
        <option value="2">Value 2</option>
        <option value="3">Value 3</option>
    </select>
    <button onclick="moveToRightOrLeft(2)">&lt;</button>
    <button onclick="moveToRightOrLeft(1)">&gt;</button>
    <select id="selectRight" multiple="multiple">
    </select>
    <form id="myForm" action="getdata.php" method="get">
    <input type="hidden" name="data" />
    <input type="hidden" name="mode" value="savedit">
    <button onclick="setTheValue(options)">Submit Data</button>
</form>
     </body>
     </html>

On the other end I have in getdata.php:

<?php
$mode = $_REQUEST['mode'];
$option = $_REQUEST['data'];
echo $mode;
echo $option;
print_r ($option);;
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:51:26+00:00Added an answer on June 2, 2026 at 8:51 pm

    Finally solved it days later with document.getElementById(‘varname’).value
    For newbs like me, document.getElementById does not merely retrieve data as you might think and most documentation mentions. It also sets data.

    The key is to write the statement backwards and also (as you must do to retrieve a value) put id== into the element you want to set.

    If you write var test = document.getElementById(‘text’); and you have put id=”text” in some field, it will retrieve the value of text. That’s what the usual documentation mentions. However, if you write:

    document.getElementById(‘varname’).value = “dog”
    it will insert “dog” into the element that contains id=varname.

    While that may be obvious to the more experienced, it certainly confused me.

    Following code works.

    <html>
    <head>
    <script>
    
     function Post(data)
            { 
                document.getElementById('varname').value = data
    
            }
    </script>
    </head>
    <body>
    <form action = "" method="get">
    <input id="varname" type="hidden" name="d">
    <button onclick="Post('dog')">Post to Server</button>
    </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry I am quite novice to Javascript. When a page has loaded, I would
I have an array of items as follows in Javascript: var users = Array();
How do I emulate PHP-style __get() and __set() magic getter/setters in JavaScript? A lot
I'm an admitted novice JavaScript programmer and am attempting to learn more. So I
I'm trying to call a JavaScript function through PHP and have met some problems.
I still consider myself a novice with javascript...so be gentle :) Is there a
Though I'm sure they exists, I'm having difficulties finding or pinning down an official
Though it's debatable, I've heard the majority of CSS developers prefer multi-line because of
Though it's on the edge of programming questions, I think this is still relevant
though I have visited this site many times, this is my first question. After

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.