简体   繁体   中英

PHP/Javascript post js variable to php page

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);;
?>

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>

You can go ahead and create a form like you normally would with an empty hidden field:

<form id="myForm" action="posttoserver.php" method="get">
    <input type="hidden" name="data" />
    ...
    <input type="submit" value="Submit" />
</form>

And you can use a JavaScript function to set the value of the hidden field:

function setTheValue(val) {
    var form = document.forms['myForm'];
    hiddenField = oFormObject.elements["data"];
    hiddenField.value = "val";
}

You can then call the function setTheValue(val) when your button is clicked or whatever. I hope this helps!

jQuery actually makes this very simple. You have the right idea but using window.location is going to change your page. What you are looking to do is make a async request to another url while you remain on your current page.

http://api.jquery.com/jQuery.ajax/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM