Php 表格未能提交

Php 表格未能提交,php,javascript,jquery,ajax,forms,Php,Javascript,Jquery,Ajax,Forms,我有一个表单,我想通过AJAX在div中显示响应。这是funds\u transfer.php中的表单图像 我用manualmethod=post测试它,并随表单一起提交,它工作得很好。以下是资金转移后端.PHP中的部分PHP代码: $index_num = $_POST['index_num']; $to_account_num = $_POST['recipient']; $amount = $_POST['amount'];

我有一个表单,我想通过AJAX在
div
中显示响应。这是
funds\u transfer.php
中的表单图像

我用manual
method=post
测试它,并随表单一起提交,它工作得很好。以下是
资金转移后端.PHP
中的部分PHP代码:

        $index_num = $_POST['index_num'];
        $to_account_num = $_POST['recipient'];
        $amount = $_POST['amount'];

        if ($amount == '' || $to_account_num == '' || $index_num == -1){
            echo "Please complete the form!";
            $response = -1;
        }
        else {  
                    // other code goes here..

        $display = array('response' => $response); // for ajax response later
        echo json_encode($display);
PHP给了我以下输出:

Please complete the form!{"response":-1}

现在我想用AJAX实现它,但它目前不起作用。以下是我当前无法使用的html+jQuery代码:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script type="text/javascript">

 function update() {
      var    two = $('#index_num').val();
      var    three = $('#recipient_box').val();
      var    five = $('#amount_box').val();


 $.post("funds_transfer_backend.php", { 
    index_num   : two,
    recipient   : three, 
    amount      : five

},function(data){


    if (data.response==-1) { 
        $('#stage').show().html("Please complete the form!"); 
    }

        $('#stage').delay(2000).fadeOut();


},"json");

        } 

</script>

//other code goes here..

<p>Transfer your funds to other account

    <script type="text/javascript">
    // Pre populated array of data
    var myData = new Array();

    <?php

                $sql="SELECT * FROM `account` WHERE client_id='$id'";
                $result=mysqli_query($conn, $sql);

                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 

                        echo "myData.push('".$row['funds']."');";
                        $result_array[] = $row['id'];
                } 

    ?>
</script>

<form id="example" name="example">
        Select your account<select id="selector" name="index_num" style="margin-left: 10px;">
            <option value="-1">Account Number</option>

    <?php //echo $result_array[0]; 

    $num = count($result_array) - 1;

    $i=0;
    while($i<=$num)
      {
      echo "<option value=\"$i\">$result_array[$i]</option>";
      $i++;
      }

    ?>

        </select>
        <br />
       Funds : RM <input type="text" id="populateme" name="funds" disabled/><br>
       Recipient Account Number <input type="text" id="recipient_box" name="recipient" />  <br> 
       Amount : RM <input type="text" id="amount_box" name="amount"/><br>    

    <input type="button" value="Submit" onclick="update();">
    <input type="reset" value="Reset">
    </form>


    <div id="stage" style="background-color:#FF6666; padding-left:20px; color: white;"></div> 

    <script type="text/javascript">
        document.example.selector.onchange = updateText;

        function updateText() {
          var obj_sel = document.example.selector;
          document.example.populateme.value = myData[obj_sel.value];
    }
    </script>


</p>

函数更新(){
var two=$('#index_num').val();
变量三=$(“#收件人_框”).val();
var five=$(“#金额框”).val();
$.post(“资金\转账\后端.php”,{
索引数量:2,
收件人:三位,
金额:五
},函数(数据){
如果(data.response==-1){
$(#stage').show().html(“请填写表格!”);
}
$('阶段').delay(2000).fadeOut();
}“json”);
} 
//其他代码在这里。。
将您的资金转到其他帐户
//预填充的数据数组
var myData=新数组();

删除此行的逗号:

amount      : five,

删除此行的逗号:

amount      : five,
我认为您应该使用$('form[name=YourFormName]')

我认为您应该使用$('form[name=YourFormName]')


如果您使用的是chrome,请启动开发者工具(ctrl+shift+I),转到网络选项卡,然后提交表单。如果表单实际上正在提交,那么您将能够检查请求和响应,以准确地查看提交到服务器和从服务器返回的内容。如果您的表单未提交,例如由于JavaScript错误,该错误将出现在控制台选项卡中

FireFox也有类似的调试工具

还有几件事需要注意:

1) 您的JavaScript错误地填充了“two”变量。它引用id为“index_num”的元素,但该元素上的id为“selector”。也就是说,实际上应该使用Vu提到的serialize()方法

2) 您的PHP代码在稍后回显实际的JSON响应之前回显了一条原始错误消息。这将导致无效的JSON。您应该将错误存储到变量中,并使用适当的键(例如“message”)将该错误推送到关联数组中。然后,您可以将其显示在页面上,如下所示:

$('#stage').show().html(data.message);

如果您使用的是chrome,请启动开发者工具(ctrl+shift+I),转到网络选项卡,然后提交表单。如果表单实际上正在提交,那么您将能够检查请求和响应,以准确地查看提交到服务器和从服务器返回的内容。如果您的表单未提交,例如由于JavaScript错误,该错误将出现在控制台选项卡中

FireFox也有类似的调试工具

还有几件事需要注意:

1) 您的JavaScript错误地填充了“two”变量。它引用id为“index_num”的元素,但该元素上的id为“selector”。也就是说,实际上应该使用Vu提到的serialize()方法

2) 您的PHP代码在稍后回显实际的JSON响应之前回显了一条原始错误消息。这将导致无效的JSON。您应该将错误存储到变量中,并使用适当的键(例如“message”)将该错误推送到关联数组中。然后,您可以将其显示在页面上,如下所示:

$('#stage').show().html(data.message);

首先,您在PHP中回显错误,并希望AJAX成功中的响应中包含JSON。因此,首先删除server/PHP脚本中的以下行,或者说除了JSON之外不要回显任何内容

echo "Please complete the form!";
并确保您的PHP输出在正确的场景中如下所示

{"response":-1}
现在,ajaxsuccess中的
数据
变量将只包含json格式。在检查
if(data.response==-1)
之前,执行
parseJSON
,如下所示

function(data){
    data = $.parseJSON(data);
    if (data.response==-1) { 
        $('#stage').show().html("Please complete the form!"); 
    }
    $('#stage').delay(2000).fadeOut();
}

首先,您在PHP中回显错误,并希望AJAX成功中的响应中包含JSON。因此,首先删除server/PHP脚本中的以下行,或者说除了JSON之外不要回显任何内容

echo "Please complete the form!";
并确保您的PHP输出在正确的场景中如下所示

{"response":-1}
现在,ajaxsuccess中的
数据
变量将只包含json格式。在检查
if(data.response==-1)
之前,执行
parseJSON
,如下所示

function(data){
    data = $.parseJSON(data);
    if (data.response==-1) { 
        $('#stage').show().html("Please complete the form!"); 
    }
    $('#stage').delay(2000).fadeOut();
}
添加id
因为您的index_num值没有发送,并且您会收到一个通知作为响应
json。同时从funds\u transfer\u backend.php中删除这一行->echo“请填写表单!”
添加id
因为您的index_num值没有发送,并且您会收到一个通知作为响应
json。同时从funds\u transfer\u backend.php中删除这一行->echo“请填写表单!”

我删除了逗号但仍然没有更改:(我删除了逗号但仍然没有更改:(我仍然需要使用
来触发
更新()
函数吗?我仍然需要使用
来触发
更新()
函数吗?