IF condition inside input

Asked

Viewed 2,518 times

1

Code:

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

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

For example:

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

Could you do it ? Or in some other way ?

2 answers

3


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

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

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

  • And if it’s a fake and I want it for another GET ?

  • 2

    @Kevin. F in this case at '' you can put whatever you need.

1

In your case if and Else do not fear staying before value, it can stay within 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>

Did you understand?? If the comparison returns TRUE on if, soon it will echo the information you, and if it gives FALSE it will go to the Else that has no echo, soon it will go blank.

And not in need of use $dataPost = $_GET['data']; or just use the $_GET['data']; or rather defines the $dataPost = $_GET['data']; and use only the $dataPost; in echo.

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

Forehead and return here! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.