Jump to content

jQuery .ajax - success


ruddie

Recommended Posts

Hello, I decided I would make my websites load less by implementing ajax/jQuery..

 

I however stumbled upon a problem..

 

This is the code I use to direct myself to the registeration page...

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,
	success: function(html){
		 $('#response').html(html);
		 alert("success...");
	},
	complete: function(html){
		$('#response').html(html);
		 alert("completed..");
	}
});		
}

 

When running - it shows me the "alert("completed..");", and in fact- the code used to insert the user in my database is working.

However, I never seem to get the 'html' variable that they use in tutorials I found. (meaning I can't show some sort of response)

 

The php code used:

<?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 />
								[email protected]
								</p>
							</body>
						</html>
						';
			$from = "[email protected]";
			$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;
exit;

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

 

And here is the form code:

<?php
session_start();
ob_start();

connectToDB();
$query = "SELECT * FROM `users`";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo '
	<div id="registerHolder">
	<h1>
		<strong>
			Registration
		</strong>
	</h1>
	<em>Currently... we have '.$num.' registered members! Join them by filling in the form below!</em><br />
	<p>
		Please fill in these boxes and press confirm to register:		
		<br />		
		<form id="registerForm" method="post" action="">
			<table>
				<tr>
					<td style="width:200px">
						Username:
					</td>
					<td>
						<input name="regUsername" type="text" id="regUsername" style="width:300px" class="box" value="" />												
					</td>
				</tr>

				<tr> 
					<td>
						Password:
					</td>
					<td>
						<input name="regPassword" type="password" id="regPassword" style="width:300px" class="box" value="" />
					</td>
				</tr>
				<tr>
					<td>
						Confirm Password:
					</td>
					<td>
						<input name="myPassCheck" type="password" style="width:300px" id="myPassCheck" value="" />
					</td>
				</tr>
				<tr>
					<td>
						E-mail:
					</td>
					<td>
						<input name="regEmail" type="text" id="regEmail" style="width:300px" class="box" value="" />
					</td>
				</tr>
				<tr>
					<td>
						<input type="submit" name="Submit" class="btn" value="Confirm" onclick="register()" />
					</td>
				</tr>
			</table>
			<br />
		</form>	
	</p>	

		<div id="response">

		</div>
	</div>
	';			
?>

 

I am wondering what I missed, and hoped someone out here could assist me.

Thanks in advance.

 

 

Edit: Added register.php code (form)

Link to comment
https://forums.phpfreaks.com/topic/229160-jquery-ajax-success/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.