/    Sign up×
Community /Pin to ProfileBookmark

isset doesnt work.

Im trying to make form and I want if the field is empty it doesnt show it at all, but it shows anyway.

<form action=”form.php” method=”get”>
<fieldset style=”width: 250px;”>
<legend>Register Form</legend>

<!– name, surname, email, password –>
<label>First name: <input type=”text” name=”firstname”></label><br>
<label>Last name: <input type=”text” name=”lastname”></label><br>
<label>E-mail: <input type=”text” name=”email” size=”24″></label><br>
<label>Password: <input type=”password” name=”password”></label><br>

<!– gender –>
<label for=”gender”>Gender:
<input type=”radio” name=”gender” value=”M” /> Male
<input type=”radio” name=”gender” value=”F” /> Female</p>
</label>

<!– drop down menu –>
<label>Age:
<select name=”age”>
<option value=”0-29″>Under 30</option>
<option value=”30-60″>Between 30 and 60</option>
<option value=”60+”>Over 60 </option>
</select>
</label><br>

<!– comment –>
<label>Comments: <textarea name=”comments” rows=”3″ cols=”40″></textarea>

</fieldset>
<!– submit button –>
<input type=”submit” value=”Submit” name=”submit”>

</form>

and

<?php

$firstname = $_REQUEST[‘firstname’];
$lastname = $_
REQUEST[‘lastname’];
$email = $_REQUEST[’email’];
$password = $_
REQUEST[‘password’];
$gender = $_REQUEST[‘gender’];
$age = $_
REQUEST[‘age’];

if (isset($_REQUEST[‘firstname’]))
echo ‘Firstname is: ‘ . $firstname;
if (isset($_
REQUEST[‘lastname’]))
echo ‘<br>Lastname is: ‘ . $lastname;
echo ‘<br>Email is: ‘ . $email;
echo ‘<br>Password is: ‘ . $password;
echo ‘<br>Gender is: ‘ . $gender;
echo ‘<br>Age is: ‘ . $age;

Why it shows lines under if? And when I use chrome it shows me Notice: Undefined index: gender in C:xampphtdocsform.php on line 7. but there is nothing wrong. :/

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmFeb 20.2014 — Don't understand your question. Can you be more exact about "under if"?

Also - you should not use GET to pass all this data. Use POST and never check the REQUEST array for your input values.

<form method="POST">

and

$firstname = $_POST['firstname'];

would be the right way.
Copy linkTweet thisAlerts:
@NogDogFeb 20.2014 — Here's a little trick you can do with variable variables:
[code=php]
$fields = array(
'firstname',
'lastname',
'email',
'password',
'gender',
'age'
);
foreach($fields as $field) {
${$field} = '';
if(isset($_POST[$field])) {
${$field} = trim($_POST[$field]);
}
}
// each of those fields should now exist as a variable, e.g.:
echo "Email: $email";
[/code]
Copy linkTweet thisAlerts:
@TownsheriffauthorFeb 20.2014 — Don't understand your question. Can you be more exact about "under if"?

Also - you should not use GET to pass all this data. Use POST and never check the REQUEST array for your input values.

<form method="POST">

and

$firstname = $_POST['firstname'];

would be the right way.[/QUOTE]


I changed to $_POST, but still it doesnt work.

if (isset($firstname))

echo 'First name is: ' . $firstname;

It shows anyway if its empty or if it has a value.
Copy linkTweet thisAlerts:
@NogDogFeb 20.2014 — Remember that isset() just means the variable is defined, not whether or not it is an empty string. Use empty() for that test (but remember that a value of 0 is empty).
Copy linkTweet thisAlerts:
@TownsheriffauthorFeb 20.2014 — Thanks you guys for helping. Got what I wanted.

Final Result ?

<?php

$fields = array(

'firstname',

'lastname',

'email',

'password',

'gender',

'age'

);

foreach($fields as $field) {

${$field} = '';

if(isset($_POST[$field])) {

${$field} = trim($_
POST[$field]);

}

}

// each of those fields should now exist as a variable, e.g.:

if (!empty($firstname))

echo "Firstname: $firstname<br>";


if (!empty($lastname))

echo "Lastname: $lastname<br>";


if (!empty($email))

echo "Email: $email<br>";


if (!empty($password))

echo "Password: $password<br>";


if (!empty($gender))

echo "Gender: $gender<br>";


if (!empty($age))

echo "Age: $age<br>";

?>
Copy linkTweet thisAlerts:
@TownsheriffauthorFeb 20.2014 — 0 is a value can be fixed: if (!empty($firstname) || $firstname == 0)
Copy linkTweet thisAlerts:
@NogDogFeb 20.2014 — Another test I've used to make sure they entered [i]something[/i]:
[code=php]
if(trim($_POST['foo']) === '') { // note use of "is identical to" operator
// it's blank or just white-space
}
[/code]
×

Success!

Help @Townsheriff spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...