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条)

报告相同问题?

悬赏问题

  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan