0
**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 文件中显示并将它们存储在数据库中。

4

2 回答 2

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"所以你应该确保你的分隔符;不在数组中!

于 2013-10-04T14:09:34.350 回答
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);

}
于 2013-10-04T14:20:17.667 回答