Jump to content

$_POST issues..?


woolyg

Recommended Posts

Hi all,

 

I'm trying to gather data from a dropdown menu to be processed and added into a table

 

Here's the dropdown menu code that displays a list of selectable items:

<select name="prop_area" size="1" class="boxes">
        <?PHP
$places = mysql_query ("SELECT * FROM places order by name");
$ordered = array();
while ($placelist = mysql_fetch_row($places)) {
	$id = $placelist[0];
	$name = $placelist[1];
	$ordered[$id] = $name;
}
natsort($ordered);
while (list($id,$use) = each($ordered)) {
	$placeid = current($ordered); 
	$option = "<option value=\"$id\"";
	if ($id == $area) {
		$option .= " selected";
	}
	$option .= ">";
	$option .= "$use</option>";
	print $option;
}
?>
    </select>

 

 

Then I use

$prop_area = $_POST['prop_area'];

 

 

Then I try to write the data to a table using:

 

$query = "INSERT INTO table (area) ".
"VALUES ('$prop_area')";

 

 

Because there are a few possible entries from the dropdown selection, should I be using some sort of FOREACH statement? If so, how can I implement it? Still nOObing it up here:)

 

Cheers,

Woolyg

 

 

 

 

Link to comment
Share on other sites

<select name="prop_area" size="1" class="boxes">
<?php
$places = mysql_query("SELECT * FROM places ORDER BY name");
while($place = mysql_fetch_assoc($places))
{
$selected = $place['id']==$area ? " selected='selected'" : null;
echo "<option value='{$place['id']}'{$selected}>{$place['name']}</option>\n";
}
?>
</select>

Is better... I supposed that the fields were named id and name.

 

I believe that if multiple selections are made, then it will be an array. Try to use print_r() and then die(). Then you can see what it does. Try with both a single value and multiple values selected, then you'll see.

Link to comment
Share on other sites

Hi Daniel0

 

Thanks for that - though the information displays fine in the dropdown the query created, what I'm really trying to do is process the information that a user selects from it.

 

In other words:

Say I had a dropdown menu with info about "Number of Rooms"

Selectable options:

1

2

3

4

5

 

...and a user selects '4'

 

How would I tell the PHP file to use this selection and post it to a MySQL table? When I use $_POST, the info simply does not enter onto the table!

 

Hope this is understandable - Woolyg.

Link to comment
Share on other sites

You can also try this stuff:

<select name="prop_area" size="1" class="boxes">

<?php

$places = mysql_query("SELECT * FROM places ORDER BY name");

while($place = mysql_fetch_assoc($places))

{?>

$selected = $place['id']==$area ? " selected='selected'" : null;

 

<option value=<?php $place['id']?>><?php $place['id']?></option>

<?php

}

?>

</select>

Link to comment
Share on other sites

On the next page do this:

 

$something=$_POST['name_of_selection_list'];

 

What ever the value is assigned to the element of the list ;will be posted to this value.

Note that the action of the firstpage should be set to the next page to recieve the values by POST and GET commands.

I think this helps!

Link to comment
Share on other sites

I believe that if multiple selections are made, then it will be an array. Try to use print_r() and then die(). Then you can see what it does. Try with both a single value and multiple values selected, then you'll see.

Nevermind this. I confused it with a select with multiple='multiple'... Besides, I found out that it doesn't return arrays anyways (unless you name it something like test[])...

 

Thanks for that - though the information displays fine in the dropdown the query created [...]

I know... I just gave you a shorter (and probably more efficient since your contains multiple loops) way of doing it.

 

[...] what I'm really trying to do is process the information that a user selects from it.

 

In other words:

Say I had a dropdown menu with info about "Number of Rooms"

Selectable options:

1

2

3

4

5

 

...and a user selects '4'

 

How would I tell the PHP file to use this selection and post it to a MySQL table? When I use $_POST, the info simply does not enter onto the table!

 

You can access it using $_POST...

 

You can see in this little test script how the data will be like in $_POST:

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<select size='4' name='test'>
	<option value='1'>one</option>
	<option value='2'>two</option>
	<option value='3'>three</option>
	<option value='4'>four</option>
</select><br />
<button type='submit'>Submit</button>
</form>

<?php
print_r($_POST);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.