开发者

jQuery Ajax function does not complete call

开发者 https://www.devze.com 2023-02-14 06:14 出处:网络
I wanted to make my register form use the $.ajax function instead of using the normal way - of redirecting the user to another php page.

I wanted to make my register form use the $.ajax function instead of using the normal way - of redirecting the user to another php page.

I call this function:

function register(){
$.ajax({
    type: "POST",
    url: "user/registerConfirm.php",        
    data:   "regUsername=" + document.getElementById("regUsername").value + 
        "&regPassword=" + document.getElementById("regPassword").value +
        "&myPassCheck=" + document.getElementById("myPassCheck").value +
        "&regEmail=" + document.getElementById("regEmail").value,
    dataType: "json",
    success: function(msg){ 
         alert("Success!");
         alert(msg);
    },
    complete: function(msg){                        
        alert("Displaying return value now.." + msg);       
    },
    error: function(request, status, error) {           
        //alert(request.responseText);
        alert("Error, returned: " + request);
        alert("Error, returned: " + status);
        alert("Error, returned: " + error);
    }
});
}

When clicking on the submit button. However, the 'complete' function is never called, and neither are the other functions.

This is the php file is uses:

<?php
session_start();
ob_start();

require("../widgets/functions.php");
connectToDB();

// Check email
if (!filter_var(clean($_POST['regEmail']), FILTER_VALIDATE_EMAIL))
{   
    $error = 'Invalid email!';          
}
else
{               
    if ( clean($_POST['regPassword']) == clean($_POST['myPassCheck']) && clean($_POST['regPassword']) != NULL 
        &&  clean($_POST['regUsername']) != NULL && clean($_POST['regEmail']) != NULL ) // Register can be allowed
    {           
        // Check if their already is a user with the same name..
        $sql="SELECT * FROM `users` WHERE username='".clean(trim($_POST['regUsername']))."'";
        $result=mysql_query($sql);

        // Mysql_num_row is counting table row
        $count=mysql_num_rows($result);     

        if($count==1){
        // Their was already a user with this name!                         
            $error = 'Registration failed - username already taken..';
        }
        else if ( $count == 0 ){ 
        // Registration allowed, no user found with the same name               

            // Encrypt password
            $encryptPass = md5($_POST['regPassword']);

            $subject = "Confirmation registration";
            $message = '
                        <html>
                            <head>
                          <title>Registration at codexplained</title>
                        </head>
                            <body>
                                <p>Hello '.clean($_POST['regUsername']).',</p>

                                <p>Thank you for registering at our website!
                                </p>

                                <p>If you wish, you can now edit your profile, to change your display options and/or to upload your own profile image!<br />
                                We hope to see you commenting on our articles soon!<br />
                                If you wish to receive more information - Please contact us, or message any of our moderators.</p>
                                <hr />
                                <p>- Current moderators - <br />
                                Ruud<br />
                                Willem<br />
                                Quint
                                </p>
                                <hr />                              
                                </p>
                                - Contact details - <br />
                                Codexplained.tk<br />
                                Codexplained@gmail.com
                                </p>
开发者_JAVA百科                            </body>
                        </html>
                        ';
            $from = "Codexplained@admin.com";
            $headers = 'From: Codexplained'."\r\n";
            $headers  .= 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $to = clean($_POST['regEmail']);
            if ( mail($to,$subject,$message,$headers) )
            {
                // Success
            }
            else
            {
                // Failed
            }
            // Insert data
            $query = "INSERT INTO `users` 
            (
            `Id` ,`Username` ,`Password` ,`Rank`,`E-mail` ,`PostAmount`, `ProfileImage`, `Ip`, `LastIP`
            )
            VALUES ( NULL , '".clean(trim($_POST['regUsername']))."' , '".$encryptPass."' , 'member', '".clean($_POST['regEmail'])."' , '0', 'none', '".$_SERVER['REMOTE_ADDR']."','".$_SERVER['REMOTE_ADDR']."' )      
            ";
            mysql_query($query)or die(mysql_error());

            $error = 'Registration completed!';
        }
    }
    else
    {           
        if ( clean($_POST['regPassword']) != clean($_POST['myPassCheck']) )
        {
             $error = 'Passwords do not match...';
        }
        else
        {
            $error = 'Registration failed - not enough data..';
        }
    }
}

echo $error;    

//mysql_close();
//header("location:../index.php?page=register");?>

This actually works - when pressing submit, and while entering the correct data it will in fact register the user, is just won't give any feedback - which is a problem.

I am hoping you can find what I missed.

Thanks in Advance.


Are you getting any javascript errors? The ajax function is expecting a json response but by the look of it you are just returning a plain string which jQuery wont be able to parse into json.

Change the datatype to text and see if that works.

function register(){
$.ajax({
   type: "POST",
   url: "user/registerConfirm.php",        
   data:   "regUsername=" + document.getElementById("regUsername").value + 
     "&regPassword=" + document.getElementById("regPassword").value +
     "&myPassCheck=" + document.getElementById("myPassCheck").value +
     "&regEmail=" + document.getElementById("regEmail").value,
   dataType: "text", // <-------- There
   success: function(msg){ 
       alert("Success!");
       alert(msg);
   },
   complete: function(msg){                        
       alert("Displaying return value now.." + msg);       
   },
   error: function(request, status, error) {           
       //alert(request.responseText);
       alert("Error, returned: " + request);
       alert("Error, returned: " + status);
       alert("Error, returned: " + error);
   }
 });
}


See http://json.org/ for the right syntax of json. In your Case, change the last line in the php script to:

echo '{"error": "'. $error .'"}';

Or use another datatype like text, see the answer of Richard Dalton

0

精彩评论

暂无评论...
验证码 换一张
取 消