Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Execute PHP without leaving page (Contact form)

$
0
0

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


Viewing all articles
Browse latest Browse all 13200

Trending Articles