Niall Paterson
Niall Paterson

Reputation: 3580

html select not working

Hi I am absolutely stuck here, i'm a PHP programmer and i would consider my HTML to be excellent but for some reason i'm stuck on this:

I have a form and it has two select options :

<select name="pays">
 <option value ="ireland">Ireland</option>
 <option value ="us">US</option>
</select>
<br>
<select name="carrier">
  <option value="ireland">Irish Network</option>
  <option disabled="disabled"><b>US<b></option>
  <option value="verizon">Verizon</option>
  <option value="sprint">Sprint</option>
  <option value="att">AT&T</option>
  <option value="cellularone">Cellular One</option>
  <option value="nextel">Nextel</option>
  </select>

My PHP file is this:

$country= $_POST['pays'];
$carrier= $_POST['carrier'];

....

$sql="INSERT INTO members (cname, cnumber, country, carrier)
VALUES ('$name', '$number', '$country', '$carrier')";

The strange thing is that the values $name and $number work fine (they are input fields in the form aswell) but the two select options are empty. I've echoed out the $country and $carrier but they're blank, making me think there is a problem with the form.

Any ideas??

P.S no errors come up

P.P.S if you don't speak french, pays=country in french

Thanks Niall

Upvotes: 1

Views: 4641

Answers (7)

Ranjith
Ranjith

Reputation: 219

You can use $_POST['name'] to get value from post method.

 <?php
if($_POST){
print_r($_POST);
echo "<br/>";
echo $_POST['pays'];
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="pays">
  <option value ="ireland">Ireland</option>
  <option value ="us">US</option>
</select>
<input type="submit">
</form>

Hope it helps you!!!

Upvotes: 0

ianm
ianm

Reputation: 101

EDit Comment : a) I realsed that this is way off topic and b) my code includes CSS for the fieldset. I do apologise for the red herring. ianm

I have a PHP CSS, and HTML application online at www.toursbw.net . Using the DEMO selection gains access to the FULL SYSTEM.

When I run this using my Samsung Android Smartphone my selection boxes/fields display the first line as blank BUT when the select downarrow is touched the selection options are displayed in the same way as the keyboard and all works fine. It needs to be triggered by the down arrow.

select_book :

function select_book($sid = 0, $debug = false) {

$db = "  ";
$sql = "select confirmed, bkname, bkid, custid, reftype
        from t_book order by confirmed, bkname, bkid"; // id to sequence dups
if ($debug) { echo "<br />fb1030 sql: $sql |<br />"; show_session(); }    
$arr = tdbquery( $sql );
$src =  $_SESSION['rowdb'];
if ($src > 0) {      
     $db .= "<fieldset>     
        <legend>Select a Booking by NAME ($src):</legend>
        --- Sequence is Unconfirmed (Ux) first then Confirmed (Cx). Where 
        x is N for NORMAL I for Person Of Interest C for CLIENT and S for SUBBY.<br />        
        <label for=\"controlinfo\">Booking Ref. Name :</label>         
        <select class=\"formSelect\" name=\"byname\" size=\"10\" >           
           ";
     while ($recs = mysqli_fetch_array($arr)) {
         $id = $recs[bkid]; 

         $st = "("; 
         if ($recs[confirmed] > 0) { $st .= "C"; } else { $st .= "U";};  
         if ($recs[reftype] == 1) { $st .= "I)";}
         elseif ($recs[reftype] == 2) { $st .= "C)";} 
         elseif ($recs[reftype] == 3) { $st .= "S)";} 
         else { $st .= "N)";} 

         $db .= "<option value=\"$id\" ";
         if ($id == $sid )  {                   
            $db .=  " selected > $st -- $recs[bkname] -- (ID:$recs[bkid]) </option>";  
         } else {     
            $db .=  "> $st -- $recs[bkname] -- (ID:$recs[bkid]) </option>";}
         }
    $db .= "
    </select>
    </fieldset>  ";         
} else {
        $db .= "<h4>fb1058 There are no bookings available: </h4>
        ";}
return $db;
}  // end select_book

This is the code that produces the selection box / field. (The $debug code is set ON externally and showed no abnormalities when run on the Smartphoe.)

Hope this is of some use, ianm

Upvotes: 0

OZZIE
OZZIE

Reputation: 7348

Did you use the right method (post)? If you don't specify $_GET will be used. Also to get both "pays" and "carrier" in one form-post they should both be in the same <form> see below example code:

<form method="post">
    <select name="pays">...</select>
    <select name="carrier">...</select>
</form>

Upvotes: 0

DarkMantis
DarkMantis

Reputation: 1516

Try this:

$sql= sprintf("INSERT INTO members (cname, cnumber, country, carrier) VALUES ('%s', '%s', '%s', '%s')", $name, $number, $country, $carrier );

That's because when you exectue an SQL query with single quotes inside, it will not evaluate the variable, it will literally use the value inside the single quotes.

Upvotes: 0

Flask
Flask

Reputation: 4996

<option disabled="disabled"><b>US<b></option>

is not valid.. should be

<option disabled="disabled"><b>US</b></option>

Upvotes: 0

Devraj
Devraj

Reputation: 3065

Two things come to mind, print_r($_POST) and see if the values are in post.

Are the select boxes inside the tag, stating the obvious but could be the issue?

Upvotes: 1

James
James

Reputation: 13501

The form looks OK. Are you performing any validation on the $country and $carrier variables? If you are (for example) running mysql_real_escape before connecting to the database then that can cause the problem you describe, where the variables are blank.

Upvotes: 0

Related Questions