-1

I am doing a php form validation program, however I bumped into a strange problem which I cannot seem to solve, even through I declared all the variables it still gives me an Notice: Undefined index: restaurants in C:\Users\andrew\Desktop\CPSC 2030\Xampp\htdocs\Assignment3\VisitForm.php on line 28 error, it simply does not fill in the field if I ignore the error, the drop box does not work.... Here's my main file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width-device-width, intial-scale-1.0">
        <link rel="stylesheet" href="styles/main.css">
        <title>Find rest - add Visit</title>
    </head>
    <nav>
        <a href="index.html">Home</a>
    </nav>
    <div class="left"></div>
    <div class="right"></div>
    <div class="wrapper">
        <body>
        <?php 
        //php invalid.
            if($_SERVER["REQUEST_METHOD"] == "POST"){
                $error_rest = "";
                echo "There is invalid data on your form \r\n";
                if(!$validName){
                    echo "Restaurant must be selected!! \r\n";
                    $error_rest='border: red 5px solid';
                }
        ?>
        <form method="post" action="VisitForm.php">
            <table border="1px">
                <tr class="repeat_example">
                    <th>Field</th>
                    <th>Type</th>
                    <th>Description</th>
                </tr>
                <tr class="repeat_example">
                    <td>restaurant <br>
                    <select id="restaurants" value="<?php echoIfExists($_POST,"restaurants","")?>" 
                            style="<?php echo $error_rest; ?>">
                            <option value="556">556(Burger King)</option>
                            <option value="778">778(A&W)</option>
                            <option value="666">666(McDonalds)</option>
                        </select>
                    </td>
                    <td>Drop down</td>
                    <td>Contains restaurant ID as the value and name of the
                    restaurant is displayed on the page.</td>
                </tr>
            </table>
            </table>
            <br><br>
            
                <label> Write new Form!</label><input type="submit">
            </form>
            <img src="img/Sitemap.PNG" alt="Map of site">
        </body>
    </div>
</html>

Here's my other file, notice I tried to produce a minimal example so if there's an information missing, just ask....

<?php

//constants
define("rest", "restaurants");

//Helper function
function valueIfExists($array, $key, $default){
    if(array_key_exists($key,$array)){
        return $array[$key];
    } else {
        return $default;
    }
}

//validation logic
$isValid = true;
$validName = true;

if($_SERVER['REQUEST_METHOD'] == "POST"){
    if($_POST[rest] == ""){
        $isValid = false;
        $validName = false;
    }
}else {
    $isValid = false;
}

I would appreciate it if anyone can help me...

thanks,

Yunfei Chen
  • 630
  • 1
  • 8
  • 20

1 Answers1

1

You are missing name attribute in select tag in your main file.
So that line of code will be -

<select name="restaurants" id="restaurants" value="<?php echoIfExists($_POST,"restaurants","")?> 

So this is not an strange problem ;)

carl johnson
  • 415
  • 3
  • 10