I am trying to get PHP $_POST
to show if its equaled or not from a form selection. I have searched/tried different things but can seem to make it work.
Form:
<html>
<body>
<form action="sig_action.php" method="post">
Phone Lines:<br />
<select name="phone_op_1">
<option value="T ">T: Office Phone</option>
<option value="F ">F: Office Fax</option>
<option value="TF ">TF: Toll Free Office Phone</option>
<option value="C ">C: Cell Phone</option>
<option value="none">none</option>
</select><br />
<input type="submit" value="Submit">
</form>
</body>
</html>
Form Handler:
<html>
<?php
if ($_POST['phone_op_1'] == 'none') {
echo 'found none';
} else {
echo 'not found none';
}
?>
</html>
You code should be:
<html>
<?php
if ($_POST['phone_op_1'] == 'none' || empty($_POST['phone_op_1'])) {
echo 'found none';
} else {
echo 'not found none';
}
?>
</html>
and:
<html>
<body>
<form action="sig_action.php" method="post">
Phone Lines:<br />
<select name="phone_op_1">
<option value="none" selected>none</option>
<option value="T ">T: Office Phone</option>
<option value="F ">F: Office Fax</option>
<option value="TF ">TF: Toll Free Office Phone</option>
<option value="C ">C: Cell Phone</option>
</select><br />
<input type="submit" value="Submit">
</form>
</body>
</html>
If nothing is selected, then the value will not be set. Additionally, if you don't want them to be able to reselect the "none" option, then also add the disabled attribute.
While there is technically nothing wrong with checking the value like this in PHP, it can be considered bad practice. A good practice would be to have an array of all the possible values for this option and then check if $_POST['phone_op_1']
existed in that array with in_array()
. Then, if it does not exist in that array, assume the value is "none"
$my_array = array(
'T ',
'F ',
'TF ',
'C '
);
$result = 'none';
if (in_array(strtoupper($_POST['phone_op_1']), $my_array)) {
$output = strtoupper($_POST['phone_op_1']);
}
echo $output;
Technically the $_POST is empty, if you specify that none is selected then it'll have 'none' in the variable
<html>
<body>
<form action="sig_action.php" method="post">
Phone Lines:<br />
<select name="phone_op_1">
<option value="T ">T: Office Phone</option>
<option value="F ">F: Office Fax</option>
<option value="TF ">TF: Toll Free Office Phone</option>
<option value="C ">C: Cell Phone</option>
<option selected value="none">none</option>
</select><br />
<input type="submit" value="Submit">
</form>
</body>
</html>
Try something like this:
if(!isset($_POST['phone_op_1']))
{
// To determine if the var has a value
// you can handle an error here
}
else
{
// Do you logic here
}
Or try debugging printing the value like this:
print_r($_POST);
die();