How do I tell my PHP script to run a Javascript function?
I know this has been posted several times, but I just can't seem to figure
it out. Here's an overview of what I'm trying to do:
I have an HTML form located in a PHP file. On form submit, the action
calls the same PHP page where the form is located. In doing this, my
server runs a bit of PHP code at the top of that page that gathers the
information inserted into the form and sends it to me in an email. After
the email is sent, I want to hide the DIV containing my form and replace
it with a hidden one that will appear, thanking the user for submitted the
form, etc.
My question is, how do I get the PHP script to run the Javascript
function? My email sends just fine, I just can't get the DIV to replace.
Here's my code.
HTML
<form id="contact-form" name="contact-form"
action="contact.php" method="post">
<table>
<tr>
<td width="79"><p><font
style="color:#ebe775;">*</font>Name:</p></td>
<td width="309"><input type="text"
name="name" id="name" placeholder="First
Last" size="45" required></td>
</tr>
<tr>
<td><p><font
style="color:#ebe775;">*</font>Email:</p></td>
<td><input type="email" name="email"
id="email" size="45" required></td>
</tr>
<tr>
<td><p><font
style="color:#ebe775;">*</font>Message:</p></td>
<td><textarea name="message" id="message"
cols="42" rows="8"
required></textarea></td>
</tr>
<tr>
<td style="text-align:center;"
colspan="2"><input type="submit"
value="Submit" name="submit"
id="submit"><input type="hidden"
name="parse_var" id="parse_var"
value="contact-form"></td>
</tr>
</table>
</form>
</div>
<div id="hidden_div" style="display:none;">
<h2>Thank You!</h2>
<p>Your form was submitted. We'll review it and get
back with you as soon as we can.</p>
</div>
</div>
</div>
PHP Script
<?php
if ($_POST['parse_var'] == "contact-form"){
$emailSubject = 'New CONTACT Form Submission';
$to = 'my.email@server.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EOD
The following information was submitted using the CONTACT form at
JakesCreativeDesign.com.
Name: $name
Email: $email
Message: $message
EOD;
$headers = "From: contact@jakescreativedesign.com\r\n";
$headers .= "Content-type: text/html\r\n";
mail("$to", "$emailSubject", "$body", "$headers");
} ?>
Javascript Function
<script type="text/javascript">
function showHide() {
var div = document.getElementById("hidden_div");
if (div.style.display == 'none') {
div.style.display = '';
}
else {
div.style.display = 'none';
}
var div = document.getElementById("form");
if (div.style.display == '') {
div.style.display = 'none';
}
else {
div.style.display = '';
}
}
</script>
No comments:
Post a Comment