Reputation: 23206
The first snippet takes data from the two text fields and sends to action script.php
. The problem is both the if
statements evaluate to true even if I do not enter anything in the text fields. Why is that ?
try.php
<form method='get' action='./action_script.php'>
<input type="text" id="text_first" name="text_first" /> <br />
<input type="text" id="text_second" name="text_second"/> <br />
<input type="submit" id="submit" />
</form>
action_script.php
<?php
if(isset($_GET['text_first'])) {
echo "Data from the first text field : {$_GET['text_first']} <br>";
}
if(isset($_GET['text_second'])) {
echo "Data from the second text field : {$_GET['text_second']} <br>";
}
echo "After the if statement <br />";
Upvotes: 3
Views: 5840
Reputation: 1
This worked for me :
if ( (isset($_POST['DEVICEINPUT'])) && (!empty($_POST['DEVICEINPUT'])) && (trim($_POST['DEVICEINPUT']) !== null) ) {
....
}
Upvotes: 0
Reputation: 2104
When you are submitting the form without any values, empty values are posted for variables text_first and text_second, but in the $_GET array a key is set for them with null values,
i.e $_GET['text_first']=''
and
$_GET['text_second']=''
,but the values are set for the two .
So if u want to check emptiness use empty() ,because isset() returns true as they are set
Upvotes: 0
Reputation: 4301
If the fields are present in the form it will evaluate to true. To check if a form has been properly submitted and has a value I use the following if statement:
if (isset($_GET['text']) && !empty($_GET['text'])) { // If the value is set and is not empty
// Processing code here..
}
Upvotes: 0
Reputation: 47667
Because they both are set - the variables exist in the $_GET
array. Even if their values are empty strings.
Try to check for emtpiness as well
if( isset($_GET['text_first']) && $_GET['text_first'] !== '' )
or
if ( ! empty( $_GET['text_first'] ) ) {
Note that you don't need to use isset()
because empty()
does not generate a warning if the variable does not exist.
Upvotes: 8