dsbckxk165039 2013-05-23 21:05
浏览 32
已采纳

根据用户输入表单错误处理ajax

Couldn't find any valueable answers on this question: Its a pretty basic ajax form process handler:

$(document).ready(function(){

});
function functionToUpdate(id)
{
    var data = $('form').serialize();
    $('form').unbind('submit');                
    $.ajax({
        url: "Blahblah.php",
        type: 'POST',
        data: data,
        dataType:"json",
        beforeSend: function() {

        },
        success: function(msg) 
        {
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(req.responseText);      
        }
    });
    return false;
} 

Works fine to update stuff in the mysql database. (This is a php file) I check in the php file the values , with different checks as isset($Post) etc.

So for example:

if(isset($_POST['submit']))
{
    //do stuff here if button is clicked
}
else{
    // play a nice error message
}

How to get this info from the url displayed in the ajax's succes part? something like this: (For example)

success: function(msg) 
{
   if(post = true)
   {
       $('#succesfull').html("Succesfull query").fadeIn(800)
   }
   else
   {
       $('#fault').html("U didn't fill stuff in, failed!").fadeIn(800)
   }
},

For example this upset. But i just can't get this data from the focused url.

all help is appriciated.

  • 写回答

1条回答 默认 最新

  • douchuza8908 2013-05-23 21:17
    关注

    You need to process the output of Blahblah.php retuned as msg.

    success: function(msg) 
            {
               if(msg=='xxxx'){
                 //Do stuff
               }
            },
    

    Edit:

    I think you may have misunderstood the meaning of success: in the ajax call. This is indicating the called page loaded successfully.

    Take a look at .ajax. For this case it may be simpler to use $.post

    or even $.getJSON

    If you use JSON the output of Blahblah.php must be json_encode.

    Edit:

    To return the html output of Blahblah.php try something like:

    <?php
    //must be before any output
    ob_start();
    
    //php code
    
    $output = ob_get_contents();
    $return['data'] = utf8_encode($output);
    ob_end_clean();
    echo json_encode($return);
    ?>
    

    Then in jQuery

    $.post("Blahblah.php", data, function(msg)
    {
      $('#succesfull').html(msg.data);
    }, "json");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源