Please help getting the selected item from combobox into var

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
SerbanSpire
Posts: 19
Joined: Fri Dec 14, 2012 11:23 am

Please help getting the selected item from combobox into var

Post by SerbanSpire »

Hello!
I am new to php and i wish to develop a browser based game. Anyway, i encountered a problem. Here is my code:

<div id ="Ship_choice_div">
<?php
print "Select the ship you wish to equip:<br>";
print "<select name ='shipchoice' lengh = '20'>";
$shipinfo = "Select * from ship_inventory where id=$playerid";
$shipinfo2 = mysql_query($shipinfo) or die("Could not select anything from ships table");

while ($shipinfo3=mysql_fetch_array($shipinfo2))
{
print"<option>$shipinfo3[type]</option>";
}

print"</select><br>";
print"</form>";
?>
</div>


<?php
if (isset($_POST['shipchoice']))
{ $ship= $_POST['shipchoice'];
}

switch($ship) {
case 'frigate':
echo "frigate";
break;

case 'destroyer':
echo "destroyer";
break;
}

?>

It doesn't seem to get the value selected from the combo box into the variable $ship. Do i need to make a button to insert $_POST['shipchoice'] into variable $ship? If so, i need to write a new .php script only with this code: if (isset($_POST['shipchoice'])) { $ship= $_POST['shipchoice']; } ???How can i make a button that inserts the variable $ship with $_POST['shipchoice'] all on the same .php page?? What should the action=" " of a button statement be?

I am sorry if i assaulted you with so many questions. The error i get is Undefined variable ship on line case 'frigate': and Undefined variable ship on line case: 'destroyer'. I tried to explain my issue as nice as i could :)
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Please help getting the selected item from combobox into

Post by alexrules01 »

I don't see you declaring the form. Only closing it, unless its further up your code.

You'd need something like this

Code: Select all

print "<form method='selectship.php' action='POST'>";
before your form elements.

Also it doesn't seem you have a submit button. just do this

Code: Select all

print "<input type='submit' value='Submit'>";
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Please help getting the selected item from combobox into

Post by Jackolantern »

Good eyes! If that is missing, that will mess things up in a hurry! :cool:
The indelible lord of tl;dr
SerbanSpire
Posts: 19
Joined: Fri Dec 14, 2012 11:23 am

Re: Please help getting the selected item from combobox into

Post by SerbanSpire »

Ok so the code you see is written on a php script called ship_cutomization.php

And shouldn't it be something like this

Code: Select all

print "<form method='POST' action='selectship.php'>";
instead of this?

Code: Select all

print "<form method='selectship.php' action='POST'>";
I never used your form.
So basically the line it should go into my script is:

Code: Select all

print "<form method='POST' action='ship_cutomization.php'>";
?
I don't know if i can do this, send the form to the same php script i am currently writing into. Maybe your selectship.php is a new php script different from ship_customization.php.

So my final code is something like this just writing down on the browser page the variable $ship which doesn't work. I still get the warning, Undefined variable $ship on the line echo "You chose: $ship"; i don't know where to put

Code: Select all

 print "<input type='submit' value='Submit'>";
either, it sounds incomplete for me.

Code: Select all

<div id ="Ship_choice_div">
<?php
  print "<form method='POST' action='ship_customization.php'>";
  print "Select the ship you wish to equip:<br>";
  print "<select name ='shipchoice' lengh = '20'>";
  $shipinfo = "Select * from ship_inventory where id=$playerid";
  $shipinfo2 = mysql_query($shipinfo) or die("Could not select anything from ships table");

  while ($shipinfo3=mysql_fetch_array($shipinfo2))
  {
   print"<option>$shipinfo3[type]</option>";
  }

  print"</select><br>";
  print"</form>";


  if(isset($_POST['shipchoice']))
  {
  $ship = $_POST['shipchoice'];
  }
  echo "You chose: $ship";
   print "<input type='submit' value='Submit'>";
?>
</div>
SerbanSpire
Posts: 19
Joined: Fri Dec 14, 2012 11:23 am

Re: Please help getting the selected item from combobox into

Post by SerbanSpire »

I just made a last minute modification giving the variable $ship a random value like 'abc' before using it in if statement, but this clearly shows me that the variable $ship is never stored with $_POST['shipchoice']. It writes on the browser just abc.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Please help getting the selected item from combobox into

Post by Jackolantern »

Yes, the action needs to be the script it is posting/getting to, and the method needs to specify get or post. And yes, you can post the form to the same script you are currently displaying :)
The indelible lord of tl;dr
SerbanSpire
Posts: 19
Joined: Fri Dec 14, 2012 11:23 am

Re: Please help getting the selected item from combobox into

Post by SerbanSpire »

Thanks for your solutions, but i managed to get this right. I made another .php page called ship_selection.php with only the code storing (succesfully) $_POST['shipchoice'] into $ship. After that in my main page i included ship_selection.php and the button to validate the storing would look like this:

Code: Select all

print "<form method='post' action=' '>";
//code
print "<input type='submit' value='Submit'>";
  print"</form>";
so the action is empty . It works fine now. :)
Post Reply

Return to “Beginner Help and Support”