Reputation: 117
Working on a small website that contains a form, one of the options is a drop-down option(menu) that look like this
adduser.html
<label for="username">Username: <input type="text" id="username" name="username" value="" /><br />
<select name="level">
<option value="1">Select Level...</option>
<option value="2">Boss</option>
<option value="3">Employee</option>
</select><br />
<label for="password">Password: <input type="text" id="password" name="password" value="" /><br />
And I'm doing this to validate if not option is selected
uservalidation.php
if(empty($_POST['username']))
{
die("Enter a username<br><a href='adduser.php'>back</a>");
}
if(empty($_POST['password']))
{
die("Enter a password<br><a href='adduser.php'>back</a>");
}
if(isset($_POST['level']) && ($_POST['level'] == '1'))
{
die("Select level<br><a href='adduser.php'>back</a>");
}
But this is not working any idea why, or how to fix this?
Thanks in advance
Upvotes: 2
Views: 109
Reputation: 6411
Use form
tags around the HTML and make sure you use method="post"
in the form
tag in order for your POST
request to work in PHP
.
The action
attribute of the form
tag just specifies the URL that the form will submit the data to.
Try this:
<form action="#" method="post">
<label for="username">Username: <input type="text" id="username" name="username" value="" /><br />
<select name="level">
<option value="1">Select Level...</option>
<option value="2">Boss</option>
<option value="3">Employee</option>
</select><br />
<label for="password">Password: <input type="text" id="password" name="password" value="" /><br />
<input type="submit">
</form>
<?
if(isset($_POST['level']) && ($_POST['level'] == '1'))
{
die("Select level<br><a href='adduser.php'>back</a>");
}
?>
UPDATE
As mentioned in the comments, I added an isset($_POST)
check in there to avoid Undefined index
.
Upvotes: 1
Reputation: 860
It doesn't look like you're validating whether or not the form was posted.
if(isset($_POST['level']) && ($_POST['level'] == '1'))
{
die("Select level<br><a href='adduser.php'>back</a>");
}
Upvotes: 0