Luca Ridolfo
Luca Ridolfo

Reputation: 17

form doesnt set variables when echoed - php

I was trying to echo a form that already worked out when I build up before (without echoing it through php). Now that I'm trying echoing this form

echo "<form action=\"timeschedulingphp.php\" method=\"POST\" enctype=\"multipart/form-data\">";
while($row = mysqli_fetch_array($result)) {
echo "<input type=\"hidden\" id=\"FName\" name=\"FName\" value=\"" . $row['FName'] . "\">
<input type=\"hidden\" id=\"FName\" name=\"FName\" value=\"" . $row['LName'] . "\">" ;

}
echo "<input id=\"date\" type=\"text\" size=8 class=\"form-control mb-2\" name=\"Date\"> </input>
                        <script type=\"text/javascript\" src=\"js/jquery.js\"></script>
                        <script type=\"text/javascript\" src=\"js/jquery-ui.js\"></script>
                        <script type=\"text/javascript\" src=\"js/ui.js\"></script>

               <input id=\"appt-time\" type=\"time\" name=\"FromTime\"
                                   min=\"00:00\" max=\"24:00\" required
                                   pattern=\"[0-9]{2}:[0-9]{2}\" class=\"form-control mb-2\"></input>
                            
                        <input id=\"appt-time\" type=\"time\" name=\"ToTime\"
                                   min=\"00:00\" max=\"24:00\" required
                                   pattern=\"[0-9]{2}:[0-9]{2}\" class=\"form-control mb-2\"></input>

                        

                        

                        

                        <select name=\"Class\" class=\"form-control mb-2\">
                            <option value=\"null\"></option>
                            <option value=\"Piano Class\">Piano Class</option>
                            <option value=\"Sing Class\">Sing Class</option>
                        </select>

                        <select name=\"Present\" class=\"form-control mb-2\">
                            <option value=\"WasPresent?\">WasPresent?</option>
                            <option value=\"Present\">Present</option>
                            <option value=\"Absent\">Absent</option>
                            <option value=\"Justified\">Justified</option>
                        </select>

                        <input type=\"text\" placeholder=\" Notes \" name=\"Notes\" class=\"form-control mb-3\">


                        
                        <button class=\"btn btn-success\" name=\"register\">Register</button>
                        <input class=\"btn btn-primary\" type=\"reset\" value=\"Reset\">
                    </form>";

When I go to timeschedulingphp.php and it controls

if(isset($_POST['register']))

it is not isset.. why?

Thank you for replying me and I'm sorry if it's a noob question

Luca

Upvotes: 1

Views: 25

Answers (3)

Ronnie Oosting
Ronnie Oosting

Reputation: 1252

To start with you need some information about the differences you can use.


isset()

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variabble is not null.

Documentation:


empty()

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array() (or []), NULL, 0, and an unset variable.

Documentation:


Related to your code and your issue, it seems $_POST is not set, so you get false in return.

You can do a couple of things to check what is inside $_POST and what to do it it is empty.

While debugging always check what is inside your data. So for that you can use:

echo '<pre>';
var_dump($_POST);
echo '</pre>';

Documentation:

Hopefully, you will receive an array like:

$array = [
        1 => 'one',
        2 => 'two',
        3 => 'three',
        4 => 'four',
        5 => 'five',
];

echo '<pre>';
var_dump($array);
echo '</pre>';
die;

Outcome:

array(5) {
  [1]=>
  string(3) "one"
  [2]=>
  string(3) "two"
  [3]=>
  string(5) "three"
  [4]=>
  string(4) "four"
  [5]=>
  string(4) "five"
}

Related to isset(), and to catch incase the array/string is empty you can do this:"

 $register = (isset($_POST['register']) ? 1 : 0);

In the code above $register will always be set. Read it like this:

  • (isset($_POST['register'])) = 'If $_POST['register'] is set'
  • ? = 'THEN DO THIS' (in our example it will become (int) 1).
  • : = 'IF NOT THEN DO THAT' (In case $_POST['register'] is not set, $register becomes (int) 0)

If $register keeps staying empty, please reload your page. By filling in your format and send actually data within $_POST. It is possible if you press F5, the $_POST data disappears which means the data is empty.


Please let me know if this was somewhat useful or not. I rather to give you information so you can practice while doing, instead of getting clear code and copy paste this. If you have questions, let me know.

Upvotes: 0

Alexander Theodore
Alexander Theodore

Reputation: 34

Okay According To You're Question
Why if(isset($_POST['register']))is not isset or considered have a null value
Because You don't set The Value of the button Try This

<button class=\"btn btn-success\" value="save" name=\"register\">Register</button>

This Code Will Give $_POST['register'] a Save Value or same as $_POST['register'] = 'save'
Hope This Answer Help

Upvotes: 0

Ray A
Ray A

Reputation: 1341

Try to set it as submit type

<button class=\"btn btn-success\" name=\"register\" value=\"Register\" type=\"submit\">Register</button>

Upvotes: 1

Related Questions