I wonder whether someone may be able to help me please.
For sometime I've been searching for a tutorial/article that shows me how to set up a form and submit the user input to a mySQL database using AJAX.
Although this doesn't show me how to send the data to the database, this article provided me with a good starting pointing and it also gave me the 'look and feel' in respect of online messages which I would like to incorporate into my form.
I've now started to adapt the example script but I'm having difficulty in submitting the data to my database.
In the form I've changed this section:
$.ajax( {
url: contactForm.attr( 'action' ) + "?ajax=true",
type: contactForm.attr( 'method' ),
data: contactForm.serialize(),
success: submitFinished
} );
}
to
$.ajax( {
url: contactForm.attr( 'savecontact.php' ) + "?ajax=true",
type: contactForm.attr( 'post' ),
data: contactForm.serialize(),
success: submitFinished
} );
}
and in the PHP script, I've changed this to now read:
<?php
// Read the form values
$success = false;
$senderName = isset( $_POST['senderName']);
$senderEmail = isset( $_POST['senderEmail']);
$message = isset( $_POST['message']);
if ( $senderName && $senderEmail && $message ) {
$query = "INSERT INTO `contact` (senderName, senderEmail, message) VALUES ('$senderName', '$senderEmail', '$message')";
mysql_query($query) or die(mysql_error());
}
// Return an appropriate response to the browser
if ( isset($_GET["ajax"]) ) {
echo $success ? "success" : "error";
} else {
?>
<html>
<head>
<title>Thanks!</title>
</head>
<body>
<?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
<?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
<p>Click your browser's Back button to return to the page.</p>
</body>
</html>
<?php
}
?>
The problem I'm having, is that upon sending the form, the sending message appears on screen and continues to be stuck on this message and non of the data is sent to my database and I must admit I'm really not sure why.
I appreciate that because of my lack of experience there may be an easier way to achieve the results I would like and I may be looking at this too simplistically, but I just wondered whether someone could perhaps have a look at this please and let me know where I'm going wrong.
Many thanks and kind regards
savecontact.phpon a form. You should rollback your JavaScript code.