0 Replies - 349 Views - Last Post: 24 June 2012 - 11:47 PM

#1 GolfyMcG  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 22-November 11

PHP/jQuery Form Submission

Posted 24 June 2012 - 11:47 PM

Hey all,

So, I had an issue earlier this year where I would input data and I would have to refresh the page with the form contents cleared in order to see the updated calculations based on the new data. I've recently been delving back into the project now that the actual competition I was monitoring is over and I discovered a way to fix the problem. It is described in this tutorial:

http://www.youtube.c...h?v=v8HoVdenZFM

It works splendidly and I liked it so much I applied it to all of my forms. I have a new issue now though. I would still like to be able to click "enter" or "return" at the end of a form to submit it. This new method of submission (using jQuery and a button which triggers a function to submit the information to the server) never actually "submits" the form. I was wondering if there is a way to get the best of both worlds. I've provided some truncated parts of the form for brevity. They should be able to describe what it is that I'm trying to do.

An example form:
<form name="registration">
	<table>
		<tr>
			<td colspan="4"><h3>Registration Form</h3></td>
		</tr>
		<tr>
			<td><p>First Name:</p></td>
			<td colspan="3"><input type="text" name="first_name" size="30"></td>
		</tr>
		<tr>
			<td><p>Last Name:</p></td>
			<td colspan="3"><input type="text" name="last_name" size="30"></td>
		</tr>
		<tr>
			<td><p>Email Address:</p></td>
			<td colspan="3"><input type="text" name="email" size="30"></td>
		</tr>
		<tr>
			<td><p>Confirm Email Address:</p></td>
			<td colspan="3"><input type="text" name="repeat_email" size="30"></td>
		</tr>
		<tr><td colspan="4"><div id="errors" class="register_errors"></div></td></tr>
	</table>
	<input type="button" value="Register" onclick="get_register();">
</form>


Example Script:
<script type="text/javascript">
	function get_register()	{
		$('#errors.register_errors').hide();
		$.post('js/process.php', { 
				form: "registration",
				first_name: registration.first_name.value,
				last_name: registration.last_name.value,
				email: registration.email.value,
				repeat_email: registration.repeat_email.value 
			},
			function(output)	{
				$('#errors.register_errors').html(output).fadeIn(1000)
			});
			}
</script>



And the corresponding PHP submission:
if(($_POST['form'])=='registration')
{	
	$date = date("Y-m-d");

	$first_name=mysql_real_escape_string($_POST['first_name']);
	$last_name=mysql_real_escape_string($_POST['last_name']);
	
	$email=mysql_real_escape_string($_POST['email']);
	$repeat_email=mysql_real_escape_string($_POST['repeat_email']);;
	
	$errors = array();
	
	$email_check= mysql_query("SELECT email FROM user WHERE email='$email'") or die("Email query didn't work.");
	$email_count = mysql_num_rows($email_check);
	
	if($email_count!=0)
		$errors[] = "Email is already in use";
	if(!$first_name||!$last_name||!$email||!$repeat_email)
		$errors[] = "Please fill in <b>all</b> fields.";
	if ($email!=$repeat_email)
		$errors[] = "Your emails do not match.";
	if(strlen($email)<6||strpos($email,'.') == false||strpos($email,'@') == false)
		$errors[] =  "Please enter a valid email address.";
		
	if(empty($errors))
	{

		//generate random number for activation
		$active=rand(23456789,98765432);

		$query = mysql_query("
		INSERT INTO user
		VALUES (' ','$date','$first_name','$last_name','$email') 
		") or die("Insert query didn't work");

		$lastid = mysql_insert_id();                                                   

		//send activation email
		$to = $email;
		$subject = "Activate your account!";
		$headers = "From:admin@website.com";
		$headers .= "\r\nBcc:admin@website.com";
		$server = "mail.website.com";

		ini_set("SMTP", $server);
		$body = "
	Hello"." ".$first_name.",
		
	You need to activate your account with the link below:
	http://www.website.com/activate.php?id=$lastid&active=$active\n\n


	Thank you for registering!
	";


		//function to send email
		mail($to, $subject, $body, $headers);

		echo"You have been registered! Check your email to activate your account!";
	}
	else 
	{
		foreach($errors as $error) 
		{
		  echo $error, '<br />';
		}
	}
}



And as always, thank you in advance for the help!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1