Jump to content

Execute PHP without leaving page (Contact form)


rec0il

Recommended Posts

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 by rec0il
Link to comment
Share on other sites

Choices - in ascending order of work and complexity:

  1. have your contact.php echo out the success message on success with link and stuff
  2. have contact.php redirect to whatever page you like on success
  3. 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
Link to comment
Share on other sites

 

Choices - in ascending order of work and complexity:

  1. have your contact.php echo out the success message on success with link and stuff
  2. have contact.php redirect to whatever page you like on success
  3. 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 by rec0il
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by rec0il
Link to comment
Share on other sites

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;
Link to comment
Share on other sites

 

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 by rec0il
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.