0

I'm working on a PHP project, where I have the following form to submit:

<h2>Create Subject</h2>
   <form action="create_subject.php" method="post">
       <p>Subject name:
           <input type="text" name="menu_name" value="" />
       </p>
       <p>Position:
           <select name="position">
               <?php
               $subject_set = find_all_subjects();
               $subject_count = mysqli_num_rows($subject_set);
               for ($count=1; $count <= ($subject_count + 1); $count++) {
                   echo "<option value=\"{$count}\">{$count}</option>";
               }
               ?>
           </select>
       </p>
       <p>Visible:
           <input type="radio" name="visible" value="0" /> No
           &nbsp;
           <input type="radio" name="visible" value="1" /> Yes 
       </p>
       <input type="submit" name="submit" value="Create Subject" />
    </form>`

In the create_subject.php (where the form action takes place), I have some validation, which looks like:

if(isset($_POST['submit'])) {
    // Process the form
    $menu_name = mysql_prep($_POST["menu_name"]);
    $position = (int) $_POST["position"];
    $visible = (int) $_POST["visible"];

    //validations
    $required_fields = array("menu_name", "position", "visible");
    validate_presences($required_fields);

    $fields_with_max_lengths = array("menu_name" => 30);
    validate_max_lengths($fields_with_max_lengths);

    if(!empty($errors)) {
        $_SESSION["errors"] = $errors;
        redirect_to("new_subject.php");
    }

where the validate presence should function as check if the fields are empty and looks like:

function validate_presences($required_fields) {
    global $errors;
    foreach ($required_fields as $field) {
        $value = trim($_POST[$field]);
        if (!has_presence($value)) {
            $errors[$field] = fieldname_as_text($field)." can't be blank";
        }
    }
}

But when I submit the form with missing data, instead of redirecting back to the previous page and listing all the errors stored in a session, I get the following error messages:

  • Notice: Undefined index: visible in /Users/eak/Sites/widget_corp/public/create_subject.php on line 10

  • Notice: Undefined index: visible in /Users/eak/Sites/widget_corp/includes/validation_functions.php on
    line 22

  • Warning: Cannot modify header information - headers already sent by (output started at
    /Users/eak/Sites/widget_corp/public/create_subject.php:10) in
    /Users/eak/Sites/widget_corp/includes/functions.php on line 4

So the output started at where the $_POST["visible"] was detected as undefined. What can be the solution here?

Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28
sklrboy
  • 441
  • 1
  • 6
  • 15
  • 1
    You could have saved you some time by some research; you wouldn't have had to write such a detailed question. If someone does not select any option on the radio box and submits the form, you will see the error you are seeing. Please have a look at http://www.php.net/isset – Hanky Panky Jun 22 '16 at 09:36
  • Yeah, but how can I achieve that instead of stopping the script by error messages, it just simply redirects to the previous page and lists the error messages there? – sklrboy Jun 22 '16 at 09:40
  • Suggestion: Just pre-select Yes or No in the radio element. (And also check serverside if it is set) – Erwin Moller Jun 22 '16 at 09:43
  • I think error is due to (int) which is used in create_subject.php please check once without int – riya Jun 22 '16 at 09:43
  • Tried without the (int), same thing happens... – sklrboy Jun 22 '16 at 09:45
  • Visible: – riya Jun 22 '16 at 09:50

2 Answers2

0

The solution is very simple. As you said $_POST["visible"] is undefined, simply use the following code block to handle the same.

if (isset($_POST["visible"])) {
    //Handle the null condition
}

EDIT: As per your comment, you have a function to handle the said issue, but its called after you have used the value. Check below comments:

    $menu_name = mysql_prep($_POST["menu_name"]);
    $position = (int) $_POST["position"];
    $visible = (int) $_POST["visible"];  // YOU USED THIS VALUE - ERROR ALREADY OCCURRED HERE

    //validations
    $required_fields = array("menu_name", "position", "visible");  //AND THEN YOU PERFORMED VALIDATION-- CALL THIS BEFORE HAND
    validate_presences($required_fields);
Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27
  • There is a presence validator function, which functions as handling the null condition, but the redirection to the previous page to list the errors is interrupted by the error messages. – sklrboy Jun 22 '16 at 09:42
  • Call the validator before you use the value.. I'll edit the answer to explain the same.. – Sujeet Sinha Jun 22 '16 at 09:44
0

Hi can you please replace code with these one.Please do validation first

if(isset($_POST['submit'])) { 
//validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);

if(!empty($errors)) {
    $_SESSION["errors"] = $errors;
    redirect_to("new_subject.php");
}

// Process the form 
$menu_name = mysql_prep($_POST["menu_name"]); 
$position = (int)$_POST["position"]; 
$visible = (int) $_POST["visible"];



$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);

Also update these function validate_presences :-

function validate_presences($required_fields) {
    global $errors;
    foreach ($required_fields as $field) {
        if(isset($_POST[$field])){
              $value = trim($_POST[$field]);
              if (!has_presence($value)) {
                  $errors[$field] = fieldname_as_text($field)." can't be blank";
              }
        } else {
                  $errors[$field] = fieldname_as_text($field)." can't be blank";
        }
    }
}
Dhaval Bhavsar
  • 495
  • 5
  • 17