-2

I have the following form codes, which doesn't submit, when i click the submit button. It gives error report "Something went wrong". I had debugged several times but can't seems to know why. Please, can somebody help me fix this? Thank you very much for the assistance

<?php
  $errorMSG = "";

  if (empty($_POST["name"])) 
  {
    $errorMSG = "Name is required ";
  } 
  else 
  {
    $name = $_POST["name"];
  }

  if (empty($_POST["email"])) 
  {
    $errorMSG = "Email is required ";
  } 
  else 
  {
    $email = $_POST["email"];
  }

  if (empty($_POST["phone"])) 
  {
    $errorMSG = "Phone is required ";
  } 
   else 
  {
    $phone = $_POST["phone"];
  }

  if (empty($_POST["select"])) 
  {
    $errorMSG = "Select is required ";
  } 
  else 
  {
    $select = $_POST["select"];
  }

  if (empty($_POST["terms"])) 
   {
    $errorMSG = "Terms is required ";
  } 
  else 
  {
    $terms = $_POST["terms"];
  }

  $EmailTo = "fredsoftsolutions@gmail.com";
  $Subject = "New meeting request from Fredsoft Solutions";

  // prepare email body text
  $Body = "";
  $Body .= "Name: ";
  $Body .= $name;
  $Body .= "\n";
  $Body .= "Email: ";
  $Body .= $email;
  $Body .= "\n";
  $Body .= "Phone: ";
  $Body .= $phone;
  $Body .= "\n";
  $Body .= "Package: ";
  $Body .= $select;
  $Body .= "\n";
  $Body .= "Terms: ";
  $Body .= $terms;
  $Body .= "\n";

  // send email
  $success = mail($EmailTo, $Subject, $Body, "From:".$email);
  // redirect to success page
  if ($success && $errorMSG == "")
  {
   echo "success";
  }
  else
  {
    if($errorMSG == "")
  {
        echo "Something went wrong";
    } 
   else 
   {
        echo $errorMSG;
    }
  }
  ?>

Thank you very much for the assistance

Fred
  • 1
  • 1
  • _"I had debugged several times"_ - please show your exact debugging efforts. – El_Vanja May 09 '21 at 10:10
  • ...that,s the given codes above – Fred May 09 '21 at 10:16
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – El_Vanja May 09 '21 at 10:55
  • There's no real debugging there, you only determine that `$success` is `false`. The linked duplicate lists many potential reasons why it might have failed. Other than that, I'd suggest getting to know the [basics of debugging](http://www.phpknowhow.com/basics/basic-debugging/) and checking the values you receive from the form. – El_Vanja May 09 '21 at 11:00

2 Answers2

0

usually in order to send an email, one needs to setup a mail server or incorporate it from an existing one or else there would be no domain to send the email from.

Thus I would recommend you to use PHPMailer class that is available at https://github.com/PHPMailer/PHPMailer.

here is a snippet.

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>
0

Probably you also have an HTML form, I guess somthing like this:

<form action="" method="POST">
<label for="name">Name
    <input type="text" name="name" id="name" />
</label>
<label for="email">Email
    <input type="email" name="email" id="email" />
</label>
<label for="phone">Phone
    <input type="text" name="phone" id="phone" />
</label>
<select name="select">
    <option value="the_value_you_want_to_send_1">1st value</option>
    <option value="the_value_you_want_to_send_2">2nd value</option>
    <option value="the_value_you_want_to_send_2">3rd value</option>
</select>
<input type="submit" name="submit_form" value="Send" />
</form>

As you can see the action="" is empty. Thus, when you submit this form, the page will refresh.. This means, the the submitted values will come to the same page. Moreover, it is better to use another name for "select" options, like this for example: name="select_options" and not name="select"

If you want to send these values to another page, you could also use a form action like this:

<form action="send_mail.php" method="POST">

Inside this PHP file "send_mail.php" you will have the code you havae posted.. but there are some errors too. You need to have something like this:

<?php
if($_POST["submit_form"]) {
    //check your $_POST values here
    if (empty($_POST["name"])) {
        $errorMSG = "Name is required ";
    } 
    else {
        $name = $_POST["name"];
    }

    if(empty($_POST["email"])) {
        $errorMSG = "Email is required ";
    } 
    else {
        $email = $_POST["email"];
    }
    //and so on
    
}
?>

Last but not least, if you do not use another PHP file to send those $_POST values, you need to insert the PHP code above the HTML code, in order to see the error messages first. I would suggest to do the validations via Javascript and/or jQuery, before submitting the form, though About sending an email, you could use PHPmailer like @Ferid Bedru Sherif suggests, but this is another question, that you could find answers Check this complete exmample here

It will be herlpful to understand and see how all these works

  • Thank you. This solves the issue. The
    line was left unreferenced. That's where i got all wrong.
    – Fred Jun 04 '21 at 19:57
  • You are welcome! if this solved your problem and believe it is answered to your question, please mark this post as anwered to help others too – Kiriakos Grhgoriadhs Jun 06 '21 at 13:30