dongyin8009 2015-08-11 09:59
浏览 48
已采纳

Php ajax只想显示错误信息表单提交

After send my form data to php file its return if any error found. But its also return success before ajax redirect page. I want display error message only and if success, redirect another page.

ajax:

$("#msform").submit(function(){
     $.ajax({
           type:"post",
           url:"pagesubmit.php",
           data:  $("#msform").serialize(),
           dataType : 'json',
           success: function(data){
           if ( ! data.success) {
                $(".help-block").fadeIn().html(data.error);
           } else {
                $(".help-block").fadeOut();
                $("#msform")[0].reset();
                window.location = 'http://dbsvawdez.com/' + data.success;
           }
           }
     });
});

php:

include_once ("db.php");
global $dbh;

function check($name){
  if(!$name || strlen($name = trim($name)) == 0){
     $error ="* Username not entered";
  }
  else{
     $name = stripslashes($name);
     if(strlen($name) < 5){
        $error ="* Name below 5 characters";
     }
     else if(!preg_match("/^([0-9a-z])+$/i", $name)){
        $error ="* Name not alphanumeric";
     }
     else {
        return 1;  
     }
  } 
}

  $name = mysqli_real_escape_string($dbh, $_POST['name']);

  $thisname = strtolower($name);

  $retval = check($thisname);

  if($retval ==1){ // if no error found
   $success ='upage/userpage?user='.$_SESSION['username'].'';                           
  }


$data = array();
$data['error'] = $error;
$data['success'] = $success;
if (!empty($data)) {
    echo json_encode($data);
}
  • 写回答

2条回答 默认 最新

  • dousu8767 2015-08-13 14:41
    关注

    Solved the problem, in this way:

    Ajax:

    $("#msform").submit(function(){
    // collect input name
    ver name = var catag=$('#name').val();
       $.ajax({
           type:"post",
           url:"pagesubmit.php",
           data:  $("#msform").serialize(),
           success: function(data){
           if ( data != 'success') {
                $(".help-block").fadeIn().html(data);
           } else {
                $(".help-block").fadeOut();
                $("#msform")[0].reset();
                window.location = 'http://dbsvawdez.com/' + name;
           }
           }
       });
    });
    

    php:

    function check($name){
        if(!$name || strlen($name = trim($name)) == 0){
        echo "* Username not entered";
        }
        else{
            $name = stripslashes($name);
            if(strlen($name) < 5){
            echo "* Name below 5 characters";
            }
            else if(!preg_match("/^([0-9a-z])+$/i", $name)){
            echo "* Name not alphanumeric";
            }
            else {
            return 1;  
            }
        } 
    }
    
    $name = mysqli_real_escape_string($dbh, $_POST['name']);
    $thisname = strtolower($name);
    $retval = check($thisname);
    if($retval ==1){ // if no error found
       echo 'success';                         
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动