rec0il Posted August 1, 2013 Share Posted August 1, 2013 (edited) Heya guys, i've been struggling with this for a while now, as I'm still a beginner in PHP and also Javascript / jQuery. Could anyone please help me out, would be much appreciated. I have made an Contact form using HTML and used "<form action="contact.php" to execute my php script, now whenever i click on my submit button, i get redirected to a blank page (contact.php) which is not what i wanna, i would like it to on the same page give me an alert saying whether the mail was sent or not. Basicly what I'm asking for is an solution to run a php script on same page, without being redirected. My HTML Code: <form action="contact.php" method="post" id="ContactForm"> <div> <div class="wrapper"><input name="cf_name" class="input" type="text" value="Navn" onblur="if(this.value=='') this.value='Navn'" onFocus="if(this.value =='Navn' ) this.value=''" ></div> <div class="wrapper"><input name="cf_email" class="input" type="text" value="Email" onblur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" ></div> <div class="textarea_box"><textarea name="cf_message" cols="1" rows="1" onBlur="if(this.value=='') this.value='Besked'" onFocus="if(this.value =='Besked' ) this.value=''" >Besked</textarea></div> <input type="submit"> <input type="reset"> </div> </form> My PHP Code: (Not sure whether i should or not post this as well, but i do anyway.) <?php $field_name = $_POST['cf_name']; $field_email = $_POST['cf_email']; $field_message = $_POST['cf_message']; $mail_to = 'my-email@live.dk'; $subject = 'my message thingy '.$field_name; $body_message = 'From:\n '.$field_name."\n"; $body_message .= 'E-mail:\n '.$field_email."\n"; $body_message .= 'Message:\n '.$field_message; $headers = 'From: '.$field_email."\r\n"; $headers .= 'Reply-To: '.$field_email."\r\n"; $mail_status = mail($mail_to, $subject, $body_message, $headers); if ($mail_status) { ?> <script language="javascript" type="text/javascript"> alert('Thank you for the message. We will contact you shortly.'); </script> <?php } else { ?> <script language="javascript" type="text/javascript"> alert('Message failed. Please, send an email to my-email@gmail.com'); </script> <?php } ?> Thanks in advance Edited August 1, 2013 by rec0il Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/ Share on other sites More sharing options...
Muddy_Funster Posted August 1, 2013 Share Posted August 1, 2013 Choices - in ascending order of work and complexity: have your contact.php echo out the success message on success with link and stuff have contact.php redirect to whatever page you like on success copy the contents of one page into the other page and have the PHP parser check if the form has been submitted and perform action depending on result of check Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1442915 Share on other sites More sharing options...
rec0il Posted August 1, 2013 Author Share Posted August 1, 2013 (edited) Choices - in ascending order of work and complexity: have your contact.php echo out the success message on success with link and stuff have contact.php redirect to whatever page you like on success copy the contents of one page into the other page and have the PHP parser check if the form has been submitted and perform action depending on result of check Thanks for the answer Muddy_Funster Regarding the choice number 1: I don't seem to quite understand what you mean, can you explain it further? About number 2, the problem is that, I don't want it to change page at all. So that nothing goes loss. And about 3, didn't quite understand this one neither, but sounds like i would have to edit twice as much if i ever decided to change the site abit. So i might turn that one off. I did some research and I believe my issue can be "fixed" using something called AJAX. Which is javascript i believe, but i couldn't figure it out, as I'm not very good at javascript. Hope to hear from someone with more knowledge :-) Edited August 1, 2013 by rec0il Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1442919 Share on other sites More sharing options...
Muddy_Funster Posted August 1, 2013 Share Posted August 1, 2013 AJAX can do it without a page change, but you would still need to add some form of output on the contact.php page for the AJAX to work with. It also meens you will need to learn the javascript (and prefferably JQuery) for processing AJJAX requests and responces on top of the PHP. Just to check, contact.php is showing as a blank page? Neither the success or the failure message are being displayed? Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1442932 Share on other sites More sharing options...
rec0il Posted August 1, 2013 Author Share Posted August 1, 2013 (edited) AJAX can do it without a page change, but you would still need to add some form of output on the contact.php page for the AJAX to work with. It also meens you will need to learn the javascript (and prefferably JQuery) for processing AJJAX requests and responces on top of the PHP. Just to check, contact.php is showing as a blank page? Neither the success or the failure message are being displayed? oh damn, not sure what to do then.. Will try to read up some javascripting and jQuery, but this will most likely take a while. Heres a link to a live beta of my site, try the contact form - then you can see what i mean :-) http://beta.apartheid.dk/#!/Kontakt Edited August 1, 2013 by rec0il Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1442944 Share on other sites More sharing options...
Muddy_Funster Posted August 1, 2013 Share Posted August 1, 2013 yeah, that white page meens that PHP has hit a parse error and error reporting / display errors is not set to show this on the screen. For development always set error_reporting to E_ALL and display_errors to on. I have it set in the ini file on my development server, so I'm not sure on the runtime config, but I think sticking this at the top of the page, just under the first <?php should do it. error_reporting('E_ALL'); display_errors = 1; Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1442965 Share on other sites More sharing options...
fastsol Posted August 1, 2013 Share Posted August 1, 2013 I distribute a fully loaded and functional contact form that submits using ajax with confirmation messages here http://amecms.com/article/Easy-to-use-contact-form-with-validation Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1442983 Share on other sites More sharing options...
rec0il Posted August 1, 2013 Author Share Posted August 1, 2013 (edited) yeah, that white page meens that PHP has hit a parse error and error reporting / display errors is not set to show this on the screen. For development always set error_reporting to E_ALL and display_errors to on. I have it set in the ini file on my development server, so I'm not sure on the runtime config, but I think sticking this at the top of the page, just under the first <?php should do it. error_reporting('E_ALL'); display_errors = 1; These are already toggled on, so I believe it's not that which is the problem. :-( I distribute a fully loaded and functional contact form that submits using ajax with confirmation messages here http://amecms.com/article/Easy-to-use-contact-form-with-validation Thanks fastsol, I will have a look right away :-) EDIT: Damn, looks like just what i need but the download link is dead.. Any chance you can re-upload it? I be getting this error when trying to download source code: Sorry the file requested is not available. Edited August 1, 2013 by rec0il Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1443037 Share on other sites More sharing options...
fastsol Posted August 1, 2013 Share Posted August 1, 2013 The page needs to be fully loaded before the link will work. I did just change one thing to help avoid this issue in the future. Quote Link to comment https://forums.phpfreaks.com/topic/280700-execute-php-without-leaving-page-contact-form/#findComment-1443050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.