0

As stated above, when I try to send a form using php my if statement is not being triggered and the variable is not being set to my desired value.

Relevant PHP:

if($_SERVER["REQUEST_METHOD"] == "POST"){


    if(isset($_POST['time_pickedm']) == "9:00"){
            $timepicked = "09:00:00";   

        } 
    }

Relevant HTML:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<input type="button" class="<?php echo $buttoncolour ?>" name= 'time_pickedm' value = "9:00">
<input type="submit" class="btn btn-primary" name="Submit">
</form>

Any help would be appreciated

2 Answers 2

1

Your condition is wrong, try:

 if($_SERVER["REQUEST_METHOD"] == "POST"){


        if(isset($_POST['time_pickedm']) && $_POST['time_pickedm']== "9:00"){
            $timepicked = "09:00:00";

        }
    }

EDIT: I think i found it!

in your html you have 2 buttons, so the time_pickedm is not going to your php. try replacing button for text:

<input type="text" class="<?php echo $buttoncolour ?>" name= 'time_pickedm'     value = "9:00">
Sign up to request clarification or add additional context in comments.

11 Comments

I've tried this but the if statement still doesn't get called
@GeorgeSmith can you echo $_POST['time_pickedm'] or var_dump the $_POST before the if?.
echo $_POST['time_pickedm'] before the if produced an undefined index error
@GeorgeSmith and thats why your if is not returning true. Did you var_dump the $_POST, so you can check?
var_dump produces: array(0) { }
|
0

You if condition invalid .If time_pickedm is present in array is always true .

its just like if(true == "9:00")

So Change to separate validate the condition

if(isset($_POST['time_pickedm']) && $_POST['time_pickedm'] == "9:00")

11 Comments

The $a variable in this answer should be replaced by $_POST.
could you evaporate what is not working.And what is problem ?
when I try that, if($_SERVER["REQUEST_METHOD"] == "POST"){ gets called but the if statement doesn't, and my variable doesn't get set to 9:00 and just stays as false
simply check print_r($_POST);exit(); on top of the first line code.See the param is shown or not
that produces "Array()"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.