如何从JavaScript获取数组值并在php中显示相同的值

时间:2013-10-04 13:54:31

标签: javascript php

**Main.php file** 

<html>
    <head>
        <script>

        function showUser(str) {
            if(str =="") {
                document.getElementById("txtHint").innerHTML="";
                return;
            }

            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                }
            }
            var internal = new Array();
            var external = new Array();
            var sub = new Array();
            var end = document.getElementById("tot").value;
            for (var i=0; i<end; i++) {
                sub[i] = document.getElementById("sub["+ i +"]").innerHTML;
                internal[i] = document.getElementById("inte["+ i + "]").value;
                external[i] =document.getElementById("exte["+ i +"]").value;
                xmlhttp.open("GET","getsub.php?
                q="+sub[i]+"&in="+internal[i]+"&ex="+external[i]+"&ed="+end,true);
                xmlhttp.send();
            }
        }
        </script>
    </head>
<body>

<?php     

echo "<td> <input type='text' name='internal' id = 'inte[$i]' onkeypress='mykey()' width='30'/> </td>";
echo "<td> <input type='text' name='external' id = 'exte[$i]' width='30'/> </td>";

?>      
<div id = "txtHint"></div>

</body>
</html>

**The php file** getsub.php

<?php

if (isset($_GET['q[]'])
    AND isset($_GET['in[]'])
    AND isset($_GET['ex[]'])
    AND  isset($_GET['ed'])) {

    $host='localhost';
    $user='test1';
    $pass='test1';
    $db='test1';
    $con = mysql_connect($host,$user,$pass);
    mysql_select_db($db);

    $i=0;
    echo $ed = $_GET['ed'];

    $q[] = array();
    $m[] = array();
    $y[] = array();

    for($i=0; $i<=$ed; $i++) {
        echo $q[$i] = $_GET['q[$i]'];
        echo $m[$i] = $_GET['in[$i]'];
        echo $y[$i] = $_GET['ex[$i]'];
    }
}
?>

我想显示我从Javascript传递到PHP文件的记录。 我想传递包含5个值的每个变量,从Javascript到PHP文件。相同的5个值,我想在PHP文件中显示并将它们存储在数据库中。

2 个答案:

答案 0 :(得分:0)

你基本上已经做了正确的事,但你不应该单独发送每个数组条目,发送数组包装:

// Main.php 
function showUser(str) {
    // ...

    for (var i=0; i<end; i++) {
        sub[i] = document.getElementById("sub["+ i +"]").innerHTML;
        internal[i] = document.getElementById("inte["+ i + "]").value;
        external[i] =document.getElementById("exte["+ i +"]").value;

    }

    // Implode arrays make them to strings
    sub_string = sub.join(';');
    internal_string = sub.join(';');
    external_string = external.join(';');
    // Now Prepare Requet
    xmlhttp.open("GET","getsub.php?q="+sub_string+"&in="+internal_string+"&ex="+external_string true);
    // And send just one Request for all the data!
    xmlhttp.send();
}

// getsub.php
<?php

// To get the arrays in php use explode
$sub = explode(';', $_GET['q']);
// Use print_r
print_r($sub);

但请注意,此方法中数组[1,2,3]已合并到"1;2;3",因此您应确保分隔符;不在数组中!

答案 1 :(得分:0)

你快到了,做我的评论,然后将你的代码更改为此。它将转储您在ajax请求中发送的值。

if (isset($_GET['q']) && isset($_GET['in']) && isset($_GET['ex']) && isset($_GET['ed']))  {

$q = $_GET['q'];
$in = $_GET['in'];
$ex = $_GET['ex'];
$ed = $_GET['ed'];

print_r($q);
print_r($in);
print_r($ex);
print_r($ed);

}