0

i have attached a php file to my website so that users can send me query mails with their details, it is sending all the data to my mail but shows an internal server error whenever it is being load after sending the message. This is my code:

This form is on every page on website

<form style="line-height:15px"  name="contactform"  action="send_form_email.php" method="post">
<input type="text" placeholder="Full Name" maxlength="50" class="txt" name="name" required><br><br>
<input type="text" placeholder="E Mail" class="txt" maxlength="80" name="email" required><br><br>
<textarea onkeyup="adjust_textarea(this)" placeholder="Message" class="txt" maxlength="1000" name="message" required></textarea><br><br>
<input type="text" placeholder="Phone No." class="txt" maxlength="30" name="phone" required><br><br><br>
<input type="submit" value="Send" class="btn">
</form> 

send_form_email.php

<?php 
$errors = '';
$myemail = 'legaladvisory@shagunmarriage.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
empty($_POST['email']) || 
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$phone = $_POST['phone'];
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
 $email_address))
{
 $errors .= "\n Error: Invalid email address";
 }

if( empty($errors))
{
$to = $myemail; 
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n   $message\n $phone"; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
   alert('Thankyou for contacting us');
header('Location: index.php');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Contact form handler</title>
</head>

<body style="window.location='index.php'">
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>
  • Would this really be considered a duplicate? I feel like the question was more related to the internal server error that was caused by the user trying to call an alert() function in PHP that didn't exist. It kind of evolved into the header discussion after that, but the root of the problem was the "alert()" method which a couple of us caught and answered properly. Thanks! – Robert Wade Aug 31 '16 at 00:53

2 Answers2

0

It's very possible that your error is coming from the alert function near the bottom before the header for location is set. That looks like you're attempting to use a javascript style alert, unless of course you have some php method somewhere named 'alert()' that you've not included in your code sample. It's worth mentioning to that in PHP any time you call header you must do so before any output takes place or you'll have an error.

Robert Wade
  • 4,918
  • 1
  • 16
  • 35
  • yep...it worked.,,thanks – Gitesh Sawariya Aug 30 '16 at 23:25
  • how can i add an alert after it then – Gitesh Sawariya Aug 30 '16 at 23:28
  • if you're wanting to show a browser alert like you do with javascript, you'd have to actually write it to the page with PHP. But since you're using a php's header method to set the location, you should simply do that on the page that you're redirected to. You may consider an approach where you redirect to a 'success.html' page and show them the message there. If not, then you can use Javascript's onload event to call your alert after the redirect. But PHP really shouldn't be handling your alerts. – Robert Wade Aug 30 '16 at 23:32
  • ok. i think i'm gonna go with a success page. thanks – Gitesh Sawariya Aug 30 '16 at 23:34
  • or... header('Location: index.php?ty=1'); //or something, and add code to index.php to check if $_REQUEST['ty'] ==1. if so show thanks – Duane Lortie Aug 30 '16 at 23:38
0

There is no "alert()" function defined in your code, and it's not a built-in PHP function that I am aware of. I suspect commenting that line out will solve it.

//alert('Thankyou for contacting us');

even if alert() is defined, you do a redirect immediately after. and you can't send output to a browser prior to using the header() unless your server is configured to buffer output. If that's not it, a look at your .htaccess file might help.

Duane Lortie
  • 1,285
  • 1
  • 12
  • 16