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]'];
    }
}
?>

I want to display the records which I have passed from Javascript to a PHP file. I want to pass for example each variable containing 5 values, from Javascript to the PHP file. The same 5 values, I want to display in the PHP file and store them in a database.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Bala Krishnan
  • 25
  • 2
  • 9
  • 1
    indent your code, will make it easier for people to look at in the future - http://beta.phpformatter.com/ – Edward Oct 04 '13 at 13:56
  • Encode your javascript array in JSON. Then `json_decode()` in your PHP. Simple. – Jimbo Oct 04 '13 at 14:00
  • accept my revision and ill help :) – Edward Oct 04 '13 at 14:06
  • Firstly use && instead of AND - http://stackoverflow.com/questions/2803321/and-vs-as-operator. you do not need the square brackets on line 56,57,58,59 71,72,73. – Edward Oct 04 '13 at 14:11

2 Answers2

0

You basically did the right thing already, but you should not send each array entry alone, send the array packed:

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

But be careful in this method an array [1,2,3] is merged to "1;2;3" so you should be sure that your delimiter ; is not in the array!

mjb4
  • 989
  • 1
  • 9
  • 14
  • Jquery's serialize() will do all of that for you. – Edward Oct 04 '13 at 14:12
  • True but knowing the basics might not be bad at all! – mjb4 Oct 05 '13 at 00:32
  • its not display the values. its display the Array Array Array only – Bala Krishnan Oct 05 '13 at 05:17
  • i am using the following source code its display the only last value only not before values if (isset($_GET['q']) AND isset($_GET['in'])AND isset($_GET['ex'])AND isset($_GET['ed'])) { echo $sub = $_GET['q']; echo $internal= $_GET['in']; echo $external = $_GET['ex']; } – Bala Krishnan Oct 05 '13 at 05:32
  • An Array is a structure consisting out of values. If you want to safe sth. with it's structure you have to use @Edward 's reply. Dont just read in the values by `$sub = $_GET['q'];` use `$sub = explode(';', $_GET['q']);` and you will get your arrays! Print them with `print_r($sub)` for a full list. – mjb4 Oct 05 '13 at 15:51
  • I edited my answer so it should be more clear what you need to edit in your code! – mjb4 Oct 06 '13 at 11:10
0

You are nearly there, do my comment and then change your code to this. It will dump the values send in your ajax request.

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

}
Edward
  • 1,806
  • 5
  • 26
  • 36