IF condition within input

1

Code:

<input type="date" id="data" name="data" value="<?php echo $dataPost = $_GET['data'];  ?>" required>

The question is: Can you make a condition if before value within input ?

For example:

if($x == $a){
    //mostra o value dentro do input igual acima.
}else{
    //mostra o value vazio.
}

Would you do it? Or some other way?

    
asked by anonymous 01.04.2016 / 19:02

2 answers

3

Inside the php tags you can write PHP code. In this case to be more compact you can use a ternary:

value="<?php echo $dataPost = $x == $a ? $_GET['data'] : '';  ?>" required>

In this example it creates a if , and if true gives the value that is in GET , if it gives false gives an empty string.

    
01.04.2016 / 19:05
1

In case your if and else does not have to be before the value, it can stay inside the value, as follows:

<input type="date" id="data" name="data" value="<?php 

if($x == $a){
//mostra o value dentro do input igual acima.
   echo $_GET['data'];  
}
else{
//mostra o value vazio.
}

?>" required>

Have you understood ?? If the comparison returns TRUE in the if , then it will echo the information you, and if it fades it goes to the else that has no echo, so it will be left blank.

And not in need of using $dataPost = $_GET['data']; or use only $_GET['data']; or else define $dataPost = $_GET['data']; before and use only $dataPost; in echo.

But I think the way I did it will be better for you.

Test and return here! :)

    
01.04.2016 / 19:09