0 Replies - 547 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:


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

Example Script:
<script type="text/javascript">
	function get_register()	{
		$.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)	{

And the corresponding PHP submission:
	$date = date("Y-m-d");

	$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);
		$errors[] = "Email is already in use";
		$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.";

		//generate random number for activation

		$query = mysql_query("
		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:[email protected]";
		$headers .= "\r\nBcc:[email protected]";
		$server = "mail.website.com";

		ini_set("SMTP", $server);
		$body = "
	Hello"." ".$first_name.",
	You need to activate your account with the link below:

	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!";
		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