Note that there are some explanatory texts on larger screens.

plurals
  1. POEmail form content using PHP without loading a new page?
    primarykey
    data
    text
    <p>I'm creating a form which sends me an email, but doesn't load a new page, instead it simply modifies some HTML and CSS values on that same page.</p> <p>I've tried to implement it, and it's close to being complete, it just doesn't work correctly. Upon clicking submit, it doesn't validate the form, it loads the PHP page and says "Thank you...", then I get an email. I'd really appreciate it if you could help me understand and debug this. Thanks!</p> <p>I can confirm that jQuery is linked correctly. I have the setup in this order:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; (link for jquery) &lt;style&gt; &lt;script&gt; validateForm() JavaScript jQuery &lt;body&gt; (no loading functions via onload) </code></pre> <p>HTML form:</p> <pre><code>&lt;form id='form' method='post' action='sendemail.php'&gt; Name: &lt;br /&gt; &lt;input type="text" id="name" name="name" /&gt; &lt;br /&gt;&lt;br /&gt; Email: &lt;br /&gt; &lt;input type="email" id="email" name="email" /&gt; &lt;br /&gt;&lt;br /&gt; Subject: &lt;br /&gt; &lt;input type="text" id="subject" name="subject" /&gt; &lt;br /&gt;&lt;br /&gt; Comments: &lt;br /&gt; &lt;textarea id="comments" name="comments" rows="10" cols="60"&gt;&lt;/textarea&gt; &lt;br /&gt;&lt;br /&gt; &lt;input type="image" src="..." name="myFormSubmitted" value="Submit" /&gt; &lt;/form&gt; &lt;div id="formResponse"&gt;&lt;/div&gt; </code></pre> <p>PHP - sendmail.php:</p> <pre><code>&lt;?php if(isset($_POST['myFormSubmitted'])) { $message = ''; // Construct the message $message .= &lt;&lt;&lt;TEXT Name: {$_POST['name']} Email: {$_POST['email']} Subject: {$_POST['subject']} Comments: {$_POST['comments']} {$checkString} TEXT; $to = 'sendMeHere@example.com'; $subject = $_POST['subject']; $from = $_POST['name']; $fromEmail = $_POST['email']; $header = 'From: ' . $from . '&lt;' . $fromEmail . '&gt;'; // Send the email mail($to, $subject, $message, $header); echo 'Thank you for your Email. We will get in touch with you very soon.'; } ?&gt; </code></pre> <p>jQuery:</p> <pre><code>//On form submit, call validateForm, then post to PHP if validated $("#form").submit(function(e) { if (validateForm()) { $.post( 'sendemail.php', { name: $('#name').val(), email: $('#email').val(), comments: $('#comments').val(), myFormSubmitted: 'yes' }, function(data) { $("#formResponse").html(data).fadeIn('100'); //Place echo in div //Modify HTML &amp; CSS here $('#name, #email, #comments').val(''); //Clear the inputs - I don't understand this line }, 'text' ); } //Prevent event from bubbling e.preventDefault(); e.stopPropagation(); return false; }); </code></pre> <p>JavaScript validate function:</p> <pre><code> $(document).ready(function() { function validateForm() { var validName = false; var validEmail = false; var validSubject = false; var validComments = false; //Perform validation here... if (validName &amp;&amp; validEmail &amp;&amp; validSubject &amp;&amp; validComments) { return true; } else { return false; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload