【问题标题】:cannot get value from $_post无法从 $_post 获得价值
【发布时间】:2020-04-30 03:39:18
【问题描述】:

我对我的代码有疑问。

我使用 $post 将数组发送到 do.insert_php。

$(document).ready(function(){  
    $('#submit_button').click(function(){
        if (confirm("Are you sure you want to Process this form?")){
            var form=$('#form2').serialize();
            $.ajax({
                url:"do/do_insert.php",
                method:"POST",
                data:form,
                success:function(){
                    $('#form2')[0].reset();
                    alert('Process Success');
                } 
            });
        }
    });
});

然后在 do_insert 过程中循环该值。

foreach($_GET["contract"] as $k=>$val1){
//code here
}

这是我的合同动态字段。

$data1 = json_decode($_POST['myData']);
//$data = json_decode(stripslashes($_POST['data']));
if (is_array($data1) || is_object($data1))
{
    $count = 0;
    echo "<tr id='row'> ";
    foreach($data1 as $mydata){
        $count++;

        //echo "<td width='197'><input type='text' id='contract.".$mydata->Id."' name= 'contract[]' value='".$mydata->Id."'/></td>";
        echo "<td width='197'><input type='hidden' id='contract.".$mydata."' name= 'contract[]' value='".$mydata."'/></td>";


   }
   echo "</tr> ";
   echo "<tr id='row'> ";
   echo "<td width='197'>Total Record</td>";
   echo "<td width='197'><input type='text' id='total' name= 'total' value='".$count."'/></td>";
   echo "</tr> ";
}

我的表格

<form id="form2" name="form2" method="POST">
<td><input name='submit_button' type='button' id='submit_button' value='Submit' class='btn'/></td>
<table id="dynamic_field">

</table>


</form>

问题是,当我在do_insert.php中使用$_get时,进程会运行,但是当我把它改成$_post时,它会报错,报错是“Undefined index: contract”

你能帮我解决这个问题吗? 谢谢之前

【问题讨论】:

  • 我已经添加了我的表单代码
  • 只是好奇,你到底写的是$post还是$_POST
  • @SarojShrestha 抱歉我的错字,我使用了 $_POST
  • @LodeweykAndri 你能不能在do_insert.php 中做print_r($_POST) 并告诉我们那里显示的内容。
  • @Phil 检查网络后,我发现请求方法:GET。怎么可能?

标签: php jquery post jquery-1.8


【解决方案1】:

尝试使用type:"POST", 而不是method:"POST",

method:type: 的别名,但仅从 jQuery 1.9 开始

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2017-05-06
    • 2018-01-04
    相关资源
    最近更新 更多