Jump to content

Ajax form without reload/refresh


adam87

Recommended Posts

Hey guys I'm currently working on a project where I would like a simple form where which has a username and a comments section, I then want this data to be stored in my MySQL database using PHP, I found this really nice form on http://www.dustindiaz.com/ajax-contact-form/ I downloaded and tried messing around with it to see if I can get it to add to my database rather then send an email, but didn't have any luck. Would someone know how to configure this form to store to a database or know of any forms similar to this that I can implement to my site.

 

Thanks in advance

 

Adam

Link to comment
Share on other sites

Yes well you might have more luck if you weren't trying to use a plugin for wordpress --- unless of course you're using this with wordpress.  If so then by all means, keep plugging.  Otherwise I'd point you to jquery, which makes ajax pretty much a snap.  With that said --- Ajax really isn't appropriate for a contact form in my opinion.  What is there to Ajax?  You have a form, you fill it out, you hit submit --- this is what forms and $_POST do very well.  You could certainly ajax, but what is the point?  Ajax is good for many things, but this is not one of them.  Incremental search--- Ajax, great!  A Chat application --- Ajax!  New user registration --- checking whether a username already exists.... Ajax! 

 

Contact form?  Err not so much.

Link to comment
Share on other sites

Hey thanks for the reply, I wasnt aware it was built for wordpress. However I have been looking for tutoirals, and I found one that has all the functionality that I want just it sends an email rather than storing it to a MySQL DB, was wondering is anyone has a example script where it would send the data to an DB rather than send a email.

 

http://www.queness.com/post/160/create-a-ajax-based-form-submission-with-jquery

 

Thanking you

 

Adam

Link to comment
Share on other sites

Adam, are you willing to exert any effort or are you just looking to be spoonfed? If so, this is not the communiity for you.  I went and pulled the php script from the tutorial.  It is OBVIOUS where you would need to make changes to store the data in a mysql table.  Put in some effort and people will help when you have problems.  Take a look at the php.net/mysql_query page. 

 


//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$website = ($_GET['website']) ?$_GET['website'] : $_POST['website'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

//Simple server side validation for POST data, of course, 
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 

//if the errors array is empty, send the mail
if (!$errors) {

//recipient - change this to your name and email
$to = 'Your Name ';	
//sender
$from = $name . ' ';

//subject and the html message
$subject = 'Comment from ' . $name;	
$message = '
br />	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




Name' . $name . '
Email' . $email . '
Website' . $website . '
Comment' . nl2br($comment) . '


';

//send the mail
$result = sendmail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
	if ($result) echo 'Thank you! We have received your message.';
	else echo 'Sorry, unexpected error. Please try again later';

//else if GET was used, return the boolean value so that 
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
	echo $result;	
}

//if the errors array has values
} else {
//display the errors message
for ($i=0; $i';
echo 'Back';
exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";

$result = mail($to,$subject,$message,$headers);

if ($result) return 1;
else return 0;
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.