Jump to content

Recommended Posts

I have the following code. I want to make it so if any fields are not filled in it will not send the message.

 

$subject ="$subject";
$message="$detail";
$mail_from="$customer_mail";
$header="from: $name <$mail_from>";
$to ='my-email@site.com';
$send_contact=mail($to,$subject,$message,$header);
if($name == '') {
echo 'Name is missing<br>';
}
if($mail_from == '') {
echo 'E-Mail is missing<br>';
}
if($subject == '') {
echo 'Subject is missing<br>';
}
if($message == '') {
echo 'Message is missing<br>';
}
else {
echo "Message was sent";
}

 

I have to move $send_contact=mail($to,$subject,$message,$header); to somewhere else. and make it so it dies or stops but idk how.

Link to comment
https://forums.phpfreaks.com/topic/142246-solved-send-message-stop/
Share on other sites

an easy way is to create an array with error messages if the error array is empty then don't do the action

 

for example

$errorMessages=array();
if(isset($_POST['submit'])){
  //check field 1
  if($_POST['field1']=="")
    //add error to array
    $errorMessages[]="field1 is empty";
  }
  //check field2
  if($_POST['field2']=="")
    //add error to array
    $errorMessages[]="field2 is empty";
  }
}
//the error array is empty i guess its safe to do something now
if(sizeof($errorMessages)==0){
  //Do something
}

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.