Forum Moderators: coopster
[webmasterworld.com...]
"PHP popups needed - Thank You popup needed after submission"
My situation differs somewhat. My client has a tiny contact request form on each page. If a visitor uses the form, they must get a popup window saying "Thanks!" The browser is not to leave the current page (if necessary, the browser can *reload* the current page).
But the kicker is that this form is CAPTCHA validated. Which means that in addition to JavaScript field validation, the CAPTCHA is validated via PHP.
So ... after the CAPTCHA code is validated on the server, I want to call a client-side popup window to say "thanks" or "wrong code." But I cannot seem to do this. Inserting JS code into a PHP echo statement results in the JS code being literally displayed in the browser window instead of being executed.
And attempting to open the popup first (via JS) and THEN run the PHP validation in that window also fails, presumably because PHP thinks a new session has started(?)
Any thoughts on this?
-Aaron
Edit: here is how to do it with prototype.js [prototypejs.org]
At the header HTML element of all of the pages, which contains the contact form, you should add this javascript code:
<script type="text/javascript" language="JavaScript" src="js/prototype.js"></script>
<script type="text/javascript" language="JavaScript">function js_contact(form) {
//submit the form
$(form).request({
onComplete: function(o){
if(o.responseText == 'ok'){
window.open('thankyou.php', 'thankyou_window', 'width=300,height=150');
}else{
alert(o.responseText);
}
}
});
}
</script>
Lets suppose this is your contact form. Here is without the CAPTCHA, just for the example.
<form method="post" id="contact_form" action="process_contact_form.php">
<fieldset>
<legend>Contact form</legend><label for="client_name">Your name:</label>
<input type="text" name="client_name" id="client_name" /><br /><br />
<label for="client_email">Your email address:</label>
<input type="text" name="client_email" id="client_email" /><br /><br />
<label for="client_message">Your message:</label>
<input type="text" name="client_message" id="client_message" /><br /><br />
<input type="button" name="contact_form_submit" value="Submit" onclick="return js_contact('contact_form');" />
</fieldset>
</form>
Lets suppose this is the PHP script, which handle the contact form submitions. Of course, you must filter and validate the posted values, prior to use them!
if(empty($_POST['client_name'])){
echo "Type your name, please!";
}elseif(empty($_POST['client_email'])){
echo "Type your email address, please!";
}elseif(empty($_POST['client_message'])){
echo "Type your message, please!";
}else{
echo "ok";
}
What the js_contact() function realy do? At the first line it submit the form with callback function. Than, the PHP response is parsed and if it return "ok", the thankyou window will be opened. Esle, an error message will be displayed.