首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表单提交失败

表单提交失败
EN

Stack Overflow用户
提问于 2013-06-04 10:38:21
回答 5查看 175关注 0票数 0

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

我用manual method=post测试它,并用表单提交,它工作得很好。以下是来自funds_transfer_backend.php的部分PHP代码

代码语言:javascript
复制
        $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给了我这个输出:

代码语言:javascript
复制
Please complete the form!{"response":-1}

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

代码语言:javascript
复制
<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查询将从db获取数据,并将其填充到select boxdisabled text box中。这没有问题,它目前工作得很好。

问题是在提交和验证data.response==-1之后,div id="stage中没有响应。我不确定这里有什么问题,可能表单根本没有提交。请提前给予帮助和感谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-06-04 14:37:23

代码语言:javascript
复制
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!"
票数 1
EN

Stack Overflow用户

发布于 2013-06-04 10:46:04

删除此行上的逗号:

代码语言:javascript
复制
amount      : five,
票数 1
EN

Stack Overflow用户

发布于 2013-06-04 11:56:57

我认为你应该使用$('formname=YourFormName').serialize();

代码语言:javascript
复制
....
var posted = $('form[name=YourFormName]').serialize();
$.post("funds_transfer_backend.php", posted ,function(data){


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

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


},"json");
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16908991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档