简体   繁体   English

我如何在PHP中获取javascript动态行值?

[英]How do i get the javascript dynamic row values in php?

jsfiddle 的jsfiddle

I'm trying to print the input text field values on nextpage (postdata.php). 我正在尝试在nextpage(postdata.php)上打印输入文本字段值。 But it always print the first row result only. 但它始终只打印第一行结果。 I didn't get the remaining row values on next page. 我没有在下一页获得剩余的行值。 I've posted my full codes on jsfiddle.. How do i get the remaining js dynamic row values in php (postdata.php page)? 我在jsfiddle上发布了我的完整代码..如何在php(postdata.php页面)中获取剩余的js动态行值?

JS JS

$(document).ready(function(){
    $("span").click(function(){
        $("#container").append('<tr><td>Email : </td><td><input type="text" name="email[]" placeholder="Email id" /></td> &nbsp; <td>Name : </td><td><input type="text" name="name[]"  placeholder="Your Name "/></td>&nbsp; <td><a title="Delete this row" href="javascript:void(0);" class="remove">Del</a></td></tr>');
    });
    $("#container").on('click','.remove',function(){
        $(this).parent().parent().remove();
    });
});

Php

<?php
    echo "
    <table>
    <tr>   
        <td>
            Email :
        </td>
        <td>
             $_POST[email]
        </td>
        <td>
            Name :
        </td>
        <td>
             $_POST[name]
        </td>
    </tr>
    </table>";
?>

When you add square brackets to the name of an input field, PHP will receive it's value as an array. 当您在输入字段的名称中添加方括号时,PHP将以数组形式接收它的值。 Your JS code is fine, but the PHP code doesn't handle arrays at all. 您的JS代码很好,但PHP代码根本不处理数组。 Look at the following: 请看以下内容:

echo "
<table>";
if(!is_array($_POST['email'])) {
    $_POST['email'] = array($_POST['email']);
    $_POST['name'] = array($_POST['name']);
}

foreach($_POST['email'] as $key => $email) {
    // get the corresponding name to the email
    $name = $_POST['name'][$key];
    echo "<tr>   
        <td>
            Email :
        </td>
        <td>
             $email
        </td>
        <td>
            Name :
        </td>
        <td>
             $name
        </td>
    </tr>";
}

echo "</table>";

Note: This code will check whether multiple values were submitted or not and will work in both scenarios. 注意:此代码将检查是否提交了多个值,并且将在两种方案中都有效。

Since name of fields you declared is array the $_POST becomes multidimensional array.So try like this 由于您声明的字段名称是数组,因此$ _POST变为多维数组。所以请尝试这样

<?php
   $size = sizeof($_POST['email']);
   echo "<table>" ;
   for($i=0; $i<$size;$i++)
   {
    echo "
      <tr>   
        <td>
            Email :
        </td>
        <td>
             ".$_POST['email'][$i]."
        </td>
        <td>
            Name :
        </td>
        <td>
             ".$_POST['name'][$i]."
        </td>
    </tr>
    ";
    }
     echo "</table>";
?>

also in your html change names of Name and Emai l field to name[] and email[] respectively.Also you have misplaced the form tag. 同样在你的html NameEmai l字段的name[]分别name[]email[] 。你也错放了form标签。 It starts after <table> and ends after <table> . 它之后开始<table>和结束后<table> which was not correct. 哪个不对。 so place form tag before table tag 所以将form标记之前table标签

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

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