Jump to content

Struggling to get PHP to handle <select> tags :o(


zaniac

Recommended Posts

I'm in the the processs of creating a dummy form. This one involves using <select> tags. I can create the form ok, but the problem for me is with the handling of <select> tag. Firstly, I can't seem to get my php handling form to recognise when a <select> tag has been selected by the user. Secondly, I need the form to ensure that an option within the <select> tag has been chosen, if a checkbox above it has been set.Below is an example of the HTML form....HTML Form-------------<h3>Select Player</h3><p><input type="checkbox" name="als"/> Alan Smith</p><p><input type="checkbox" name="ianw"/> Ian Wright</p><p><input type="checkbox" name="beto"/> Beto</p><p><input type="checkbox" name="rom"/> Romulo</p><p><input type="checkbox" name="dper"/> Dean Peer</p> <p><select name="shirtnumb"> <option value=""> Choose Shirt Number </option> <option value="1"> Black </option> <option value="2"> Navy </option> <option value="3"> Green </option></select></p>Below is my PHP handle example.........PHP Script------------<?phpif (isset($_POST['submit'])){$problem = FALSE;$als = $_POST['als'];$ianw = $_POST['ianw'];$beto = $_POST['beto'];$rom = $_POST['rom'];$dper = $_POST['dper'];$shirtnumb = $_POST['shirtnumb'];$playershirterror = (isset($als) OR ($ianw) OR ($beto) OR ($rom) OR ($dper))+(!isset ($shirtnumb)); if ($playershirterror = TRUE){ $problem = TRUE; echo'<p>Please select a shirt number for the player.</p>';} ;}?>I think the problem is the line..............$playershirterror = (isset($als) OR ($ianw) OR ($beto) OR ($rom) OR ($dper))+(!isset ($shirtnumb));...... I'm not sure if I'm using the OR operator correctly. Did I almost get the coding right for what I want to achieve?I think <select> tags are the only part of the HTML form I struggle to get php to handle. I can get php to recognise all the other inputs of the form fine. So guys, any help or ideas will very much be greatly appreciated :)

Link to comment
Share on other sites

<input type="checkbox" name="als" checked="checked"/> thats how to checkto check simply have the basic first<input type="checkbox" name="als" <? if (isset($_POST['value here'])){checked="checked"}?>/> thats how to check

Link to comment
Share on other sites

I think the problem is the line..............$playershirterror = (isset($als) OR ($ianw) OR ($beto) OR ($rom) OR ($dper))+(!isset ($shirtnumb));
Yeah, what's going on with that? I'm not sure what you're trying to do with it, but the syntax definately isn't right. The syntax is legal, it's just not doing what you want. You can try printing the value of $playershirterror if you want, I'm not even sure what the result is when you add two boolean values. What are you trying to check with that?There's nothing special about how a normal select list gets sent to PHP, this will retrieve the value they selected:$shirtnumb = $_POST['shirtnumb'];The value will either be "", "1", "2", or "3". You can print out all of $_POST if you want to see everything that gets submitted:
echo "<pre>";print_r($_POST);echo "</pre>";

Secondly, I need the form to ensure that an option within the <select> tag has been chosen, if a checkbox above it has been set.
I'm not sure what you mean by that, but if you want to check if a certain checkbox was selected and a certain value chosen from the select list, you could do this:
if ($shirtnumb == "1" && isset($_POST['als'])){  // they chose "black" and checked the Alan Smith box}// or..if ($shirtnumb == "1"){  //they chose black  if (isset($_POST['als']))  {	//they also checked Alan Smith  }  if (isset($_POST['ianw']))  {	//they also checked Ian Wright  }}

Link to comment
Share on other sites

Hi Teng84, thanks for the info you provided on having inputs checked. It gives me something to try out out in my next form.justsomeguy - With regards to the php line ......... $playershirterror = (isset($als) OR ($ianw) OR ($beto) OR ($rom) OR ($dper))+(!isset ($shirtnumb));........I write this realising I didn't use the 'if' statement. Anyway, I'm trying to make the line say "if either $als, $ianw, $beto, or $rom is selected and a $shirtnumb value has not been selected, then create the $playershirterror variable."I would then write an echo message saying something like...{echo'<p>Please select a shirt number for the player.</p>';}I think your have answered my questions though with your examples. A biiiiiiiiiiiiiig thank you for your help on this one :)

Link to comment
Share on other sites

It always has a value, the empty string ("") is still a value, it's just an empty string.To do this

Anyway, I'm trying to make the line say "if either $als, $ianw, $beto, or $rom is selected and a $shirtnumb value has not been selected, then create the $playershirterror variable."
I guess a good way to make the $playershirterror variable true if that is the case, and false otherwise might be like this:
$playershirterror = ((!empty($als) || !empty($ianw) || !empty($beto) || !empty($rom)) && $shirtnumb == "");

That will make the variable equal to true if at least one of the checkboxes was selected and the $shurtnumb is blank, and false otherwise. The || and && operators are pretty much the same as OR and AND.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...