简体   繁体   中英

How do i store data from jquery to a 2D array in php without ajax?

The jquery code makes up a table and sends "hide1" and "hide2" number of rows and columns to php, where it should be stored into the database.

$(document).ready(function(){
    var i=4;
    var ctr=0;    
    var col=0;
    var ctr2=0;
    var col=3;
    var ctr1=0;
    var i2=1;

    $("#add").click(function(){
        $("table").append('<tr id="'+ctr1+'"></tr>');
        for(var j=1;j<=col;j++){
            $("#"+ctr1).append('<td><input type="text" name="n'+(i++)+'"></td>');

        }

        ctr1++;
        $("#h1").val(ctr1);

    });
    $("button").click(function(){
        ctr2++;
        col++;
        $("tr").append('<td><input type="text" name="c'+(i2++)+'"></td>');
        $("#h2").val(ctr2);
    });
});

And here is the php code.

<?php
    if(isset($_REQUEST['sub']))
    {
        $cr=$_GET['hide1'];
        echo "No. of added rows=".$cr."<br>";
        $cc=$_GET['hide2'];
        echo "<br> No. of added columns=".$cc;
        $data[$cr][$cc];
    }
?>

<html>
    <title> BIA </title>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-     2.1.4.min.js"></script>
        <script type="text/javascript" src="http://localhost/1.js"></script>
    </head>
    <fieldset>
        <legend>Enter text</legend>
        <form method="GET" action="#">

            <table border="1">
                <tr>
                    <td><input type="text" name="n1"></td>
                    <td> <input type="text" name="n2"></td>
                    <td> <input type="text" name="n3"></td>
                </tr>
            </table>
            <input type="hidden" name="hide1" id="h1">
            <input type="hidden" name="hide2" id="h2">
            <input type="submit" name="sub" value="SEND">
            <input type="submit" name="next" value="Enter More">
            <input type="button" id="add" value="add row">


        </form>
        <button>Add Column</button>
    </fieldset>
    </body>
</html>

I am not able to figure out how to make a 2D array get the elements returned by jquery.

Use indexes in input names, to make array. For example, if you need simple array, like [1,2,3,4,5,6] the name in inputs must be like:

<tr><td><input type="text" name="n[0]"></td></tr>
<tr><td><input type="text" name="n[1]"></td></tr>
<tr><td><input type="text" name="n[2]"></td></tr>

If you need 2D array, like [[1,2,3],[4,5,6],[7,8,9]] the name in inputs must be like:

<tr><td><input type="text" name="n[0][1]"></td></tr>
<tr><td><input type="text" name="n[0][2]"></td></tr>
<tr><td><input type="text" name="n[0][3]"></td></tr>

<tr><td><input type="text" name="n[1][1]"></td></tr>
<tr><td><input type="text" name="n[1][2]"></td></tr>
<tr><td><input type="text" name="n[1][3]"></td></tr>

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