-1

I am inserting and updating some values through a form with radioboxes and textarea. Basically the query works fine but if user does not select any radio boxes or leaves the text area empty, I want an error message to pop up, instead of real php errors of undefined.... blah blah.

Sorry for this weird layout, but this text editor is super problematic with editing..

    $var1 = $_POST['var_1'];
    $var2 = $_POST['var_2'];
    $var3 = $_POST['var_3'];
    

    if(isset($_POST['button_name'])) {
    
    if(query1 && query2) {
      echo "Successfully inserted and updated"</h3>";
    } elseif(empty($var1) && empty($var2) && empty($var3)) {
      echo "not selected. Please Try Again"</h3>";
    } else {
      echo "failed to sent cause of query issue";
    }
} else {
    echo "nothing was selected";
}
brombeer
  • 8,716
  • 5
  • 21
  • 27
mikey1091
  • 9
  • 2

1 Answers1

1

First check if the variables are defined, then set them to their values:

$var1, $var2, $var3;

if(issset($_POST['var_1']) && isset($_POST['var_2']) && isset($_POST['var_3']) && isset($_POST['button_name'])) {
  $var1 = $_POST['var_1'];
  $var2 = $_POST['var_2'];
  $var3 = $_POST['var_3'];
} else {
  echo "Your custom error message";
}

Alternatively, you can use try and catch:

try {
  $var1 = $_POST['var_1'];
  $var2 = $_POST['var_2'];
  $var3 = $_POST['var_3'];
  $_POST['button_name'];
} catch {
  echo "Your custom error message";
}
Wais Kamal
  • 5,858
  • 2
  • 17
  • 36