Not passing form Select/options

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Not passing form Select/options

Post by Lonestarjack »

My form has several drop down boxes such as Statecodes, Salutations, etc
It uses the Post method.
All fields are passed with $_POST except the select boxes whether they are changed or not.
Probably some setting in the .ini file, but I am unable to find it.
I am new to PHP after yaers of ColdFusion programming.
Help please.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Not passing form Select/options

Post by Ziq »

First of all, post your code with a HTML form
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Not passing form Select/options

Post by Lonestarjack »

Ziq wrote:First of all, post your code with a HTML form
I would like to post all the code but it is a great deal of space.
There are included javascripts, and ajax coding.
View source will not show the code as it is all generated by ajax.
It forms good HTML code otherwise I would not have it on the display form.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Not passing form Select/options

Post by Ziq »

How we can help you if we don't know anything at all?

This isn't php configuration problem. I think problem in HTML (or javascript, or something else) or php script. Are you sure that select boxes don't pass values? Try to print $_POST variable in a handler script

Code: Select all

<?php
print_r($_POST);
?>
Maybe you can show a link on your script?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Not passing form Select/options

Post by aceconcepts »

Can you just post you form or just event the select element?
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Not passing form Select/options

Post by Lonestarjack »

aceconcepts wrote:Can you just post you form or just event the select element?
OK I have reduced the code to a minimum:
Here is the php code--
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TestSelects.php</title>
</head>
<body>

<P></P>
Test selects to post
<p></p>
<form name="editform" action="testajaxpost.php" method="post">
Record ID:<input name='record_id' value=100 size='4' readonly='Yes'>
<span>Type:</span>
<?php
$type ="B";
echo '<select class="input" name "type" size = "1">';
if($type== 'P'){
echo '<option value="P" selected>Personal</option>';
} else {
echo '<option value="P" >Personal</option>';
};
if($type== 'K'){
echo '<option value="K" selected>Mary Kay</option>';
} else {
echo '<option value="K" >Mary Kay</option>';
};
if($type== 'B'){
echo '<option value="B" selected>Yellow Pages</option>';
} else {
echo '<option value="B" >Yellow Pages</option>';
};
echo "</select>";
?>

<span >Lunch:</span>
<select name 'Lunch' size = '1'>
<option value="Yes" selected>Yes</option>
<option value="No" >No</option>
</select>
<br>
<input type='Submit'>
</form>
</body>
</html>
=======================================
Here are the results of the print_r:
array
'record_id' => string '100' (length=3)

Array ( [record_id] => 100 )
=========================================
Here the results of the code when written in ColdFusion:
array
'record_id' => string '100' (length=3)
'type' => string 'B' (length=1)
'lunch' => string 'Y' (length=1)

Array ( [record_id] => 100 [type] => B [will] => Y )
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Not passing form Select/options

Post by Eran »

You are missing an equality operator after the 'name' property:

Code: Select all

 
<select class="input" name "type" size = "1">
 
Should be:

Code: Select all

 
<select class="input" name="type" size = "1">
 
Also you might write it more compact like this:

Code: Select all

 
$select = '<select class="input" name="type" size="1">';
$options = array('P' => 'Personal','K' => 'Mary Kay','B' => 'Yellow Pages');
foreach($options as $key => $val) {
     $select .= '<option value="' . $key . '"' . ($key == $type ? ' selected="selected"' : '') . '>' . $val . '</option>';
}
$select .= '</select>';
 
 
This way you can add options without repeating the logic.
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Not passing form Select/options

Post by Lonestarjack »

A million thanks for your sharp eyes and the code snippet.

I usually use this for query generated dropdowns:

while(list($id, $tblst)=mysql_fetch_row($stresult)) {
if($id == $statecode)
{ $stselected = ' selected'; }else{ $stselected = '';}
echo '<option value="'.$id.'"'.$stselected.'>'.$id.'</option>';
}

but I haven't developed one for hard coded dropdowns.
Thanks for yours.
I have been spoiled by using Coldfusion for years, but recently decided to switch to PHP for my personal site.
Post Reply