简体   繁体   English

我如何在没有ajax的情况下将数据从jquery存储到PHP中的2D数组中?

[英]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. jQuery代码组成一个表,并将“ hide1”和“ hide2”的行数和列数发送到php,并将其存储在数据库中。

$(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代码。

<?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. 我无法弄清楚如何使2D数组获取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: 例如,如果您需要简单的数组,例如[1,2,3,4,5,6] ,则输入中的名称必须类似于:

<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: 如果需要2D数组,例如[[1,2,3],[4,5,6],[7,8,9]] ,则输入中的名称必须类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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