开发者

how to get php to read in a string of words from a dropdown box?

thank you all so very much for helping. It's seemingly such an easy task, and I feel I just hit a brick w开发者_如何学Pythonall and not knowing how to fix it.

I'll try to explain what I am doing a bit more and hope it makes sense.

I used a database to populate my dropdown box, dropdown box is something like:

echo "<form action='process_form.php' method='post'>";
echo "Select an item<br />";
echo "<select name='item' id='item'>";
//database was connected and select query was executed succesfully. $result was the select query result.
while($row = mysql_fetch_array($result))
{
//each row contains more than one words, something like "dressmaker mannequin size 12"
echo "<option value=" . $row['style'] . ">" . $row['style']. "</option>" . "<br />";
}
...
...

Dropdown box was populated with correct data.

Then user selects an item from this drop down box, and clicks the submit button.

I need to know which item user has selected in order to calculate its shipping costs, but when I use $_POST['item'] to try to find out which item was selected, I get the first word "dressmaker", the rests were missing!

Please help me to obtain the whole string of words.

Many many thanks in advance.

Sorry I am completely new to forums, and I don't even know how to post or follow up with a question. If you guys can help me on this as well. At the moment, I am struggling to post my follow up questions ... hope this will show up ...


Hopefully, your MYSQL database has a primary key? If it does, set the value of each <option> to the primary key of the item.

For example:

SQL

id  desc
1   "dressmaker thing with mannequin"
2   "dressmaker thing no mannequin"

Form PHP

echo "<option value='".$query['id']."'>".$query['desc']."</option>";

When form is submitted, re-query the database for the desired description. You'll be doing this re-query anyway to retrieve prices and such, yes?

The reason this is happening is that spaces are discouraged in HTML attributes. You shouldn't have an attribute like value='this attribute is spaced'.


If you really have what you posted as the code, then you're missing quotes around the value:

<option value=" . $row['style'] . ">

This would produce:

<option value=dressmaker mannequin size 12>

That's not valid html, and the parser is going to think only the first word is the value. You need to put your value in quotes:

<option value='" . $row['style'] . "'>

GOOD: <option value='dressmaker mannequin size 12'>

Now your value will be everything inside the quotes. Be careful, though, because if you have quotes inside the $row['style'] value, then it will prematurely escape your value.

BAD: <option value='this won't work because of the apostrophe in won't'>

Notice how in this forum the coloring of the code text works to show what the values are, and explains why you keep getting just 'dressmaker'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜