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

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);
}

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';                         
}

EDIT

Set your variables $success and $error

$success = "";
$error= "";

If you doesn't init them, you cannot use them and the .=operator is for concatenation not for set.

Then you should encode the response in php in JSON.

Something like

$response = json_encode(
    array(  
        'success'=> true,  
        'route' => "mypage/info?user=$_SESSION['username']"
    )
);

And return this, then access your response like you already do :

var success = response.success;

UPDATE

change this code to add an else statement :

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

and this line :

else {
    return 1;  
}

to :

else {
   $error = 'none';
}

and in your javascript :

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