0

so i'm making a contact form for some guy and I want it to give me an error message when some fields are empty. now it works and stuff, but it goes to blank page everytime. I've tried searching and couldn't find an answer. my php code:

<?php
$from = $_POST['email_adress'];
$subject = $_POST['subject'];
$select = $_POST['select'];
$message2 = $_POST['message2'];
$name = $_POST['firstname'];
$name2 = $_POST['lastname'];
if ($_POST['firstname'] == "")
{
    echo "<script>
             alert('A First Name is Mandatory'); 
     </script>";
}
elseif  ($_POST['subject'] == ""){
    echo "<script>
             alert('A Subject is Mandatory'); 
     </script>";
}
elseif ($_POST['email_adress'] == "") {
    echo "<script>
             alert('An Email Adress is Mandatory'); 
     </script>";
}
elseif ($_POST['message2'] == "") {
    echo "<script>
             alert('A Message is Mandatory'); 
             <a href='contactus.html'>;
     </script>";

} else {
$email_subject = "A submittion form";
$to ="sudaiguy1@gmail.com";
$headers = "From: sudaiguy1@gmail.com";
$email_body = 'You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the general subject of $select and more specifically $subject and the message is $message2';
mail($to,$email_subject,$email_body, $headers);
}

?>

guy
  • 7
  • 4

1 Answers1

1
<?php header("Location: yourpage"); ?>

After sending the email there's no content on your page, so you have to redirect the user to a Page with some content, or add content bellow your mail send code.

Devian
  • 817
  • 1
  • 12
  • 22
  • works but it doesnt activate the alert – guy Dec 17 '16 at 14:35
  • have you tried to access the page without posting anything? – Devian Dec 17 '16 at 14:36
  • what do you mean? without putting anything in the form? – guy Dec 17 '16 at 14:38
  • nvm, try using !isset($_POST['firstname']) instead of checking $_POST['firstname'] = "" – Devian Dec 17 '16 at 14:39
  • and also try to access this new page, through your browser. If this code is for example in sendmail.php. open it with your browser. – Devian Dec 17 '16 at 14:41
  • it works well, but the problem is the error message doesnt show up anymore – guy Dec 17 '16 at 14:41
  • maybe you have suppressed alerts from your browser accidentally. Try with a different browser, Also check your page source if the script is writted on your html. – Devian Dec 17 '16 at 14:42
  • no, when i remove the header() it works – guy Dec 17 '16 at 14:42
  • You should place the header location, INSIDE your ELSE code block – Devian Dec 17 '16 at 14:43
  • but then it just does what i dont want it to do.... whenever the alert opens up and i press ok it goes to blank page – guy Dec 17 '16 at 14:47
  • Then you should make the redirection through javascript window.location.href = "yourpage"; – Devian Dec 17 '16 at 14:47
  • I need to put it inside a script right? – guy Dec 17 '16 at 14:50
  • yes after the alert. – Devian Dec 17 '16 at 14:51
  • what im trying to do is give it the alert then stay in the same page, i dont really want to redirect it – guy Dec 17 '16 at 14:53
  • When you post something on a different page, it's always a blank page... You should merge your 2 files into 1 You should make the top of your file to check if (isset($_POST['subject']) ... or something like that... And then place in the if codeblock all your code. Then you should place an ELSE statement open brackets, close php tag ?> Place your html code, open php tags Also you should consider on your input fields to add on their value="" – Devian Dec 17 '16 at 14:57
  • Also you should consider making a Client Side javascript validation before posting the page... If you want to avoid this... So it can't post to the new page when insufficient data entered. – Devian Dec 17 '16 at 14:58
  • nevermind i found something else, ty anyway – guy Dec 17 '16 at 15:09
  • echo ''; works well – guy Dec 17 '16 at 15:09