php - 表格提交失败

标签 php javascript jquery ajax forms

我有一个表单,我想通过 AJAX 在 div 中显示响应。这是来自 funds_transfer.php 的表单图像。

enter image description here

我使用手动method=post对其进行测试并使用表单提交,效果很好。以下是来自 funds_transfer_backend.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>

上面的 SQL 查询将从数据库获取数据并将其填充到选择框禁用文本框中。没问题,目前运行良好。

问题是提交和验证后 div id="stage 没有响应 data.response==-1 。我不确定这里出了什么问题可能表单根本没有提交。请提供帮助并提前致谢。

最佳答案

Add id 
<select id="index_num" name="index_num" style="margin-left: 10px;">
because you index_num value is not sending and you get a notice as response with your
json. Also remove from funds_transfer_backend.php this line -> echo "Please complete the form!"

关于php - 表格提交失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16908991/

相关文章:

php - 有没有更好的或替代的方法来编写我的 SQL 查询?

php - PCRE:环视的 DEFINE 语句

javascript - 用 TypeScript 计算 knockout ?

javascript - 来自 jquery 的 cameratag 设置

javascript - 使用 jQuery,如何使用 "live"方法在添加新元素时发出警报?

jquery - 所有包含内容的 div 的每个函数

php - 无法让 mySQL 将数字作为新列输入

javascript - 更深入地理解 Promise

javascript - geoJson 层保存 [for send DB] 在 map-Leaflet-Draw 上绘制的特征

php - Sublime text 2 中像 Dreamweaver 一样的默认页面设置?