tags:

views:

59

answers:

4

Can any one please help me? I am new to php, currently I am trying to use $_POST['item'] to obtain a string (consists of many words) from a dropdown box and then process it. However, when I use $_POST['item'], only first word is returned, the rest of words are missing.

I have a dropdown box, something like:

echo "<form action='process_form.php' method='post'>";
echo "Select an item<br />";
echo "<select name='item' id='item'>";
...
...
...

each item in the dropdown box is a string that has product names like:

dressmaker mannequin size 12
torso mannequin in white color
...

User will then select an item from the dropdown box. When I used $_POST['item'] to obtain this string, I get the first word "dressmaker", all the rests were missing.

How do I get the whole string?

Many thanks in advance.

A: 

This is how the php gets the values from the HTML select item:

<select name="item" id="item">
    <option value="this is option 1">option 1</option>
    <option value="this is option 2">option 2</option>
    <option value="this is option 4">option 3</option>
    ...
</select>

Basically you would do something like:

$var = $_POST['item'];

The $var will be the whatever was entered into the "value" of the option tag. So just make sure you have the proper values entered into the value fields.

Hope that helps.

miki725
i recommand to not pass all your content in your value field but using a ID instead. This ID can be a value in your DB or in a array
racar
@racar That's what `value` is for, specifying the value of an element.
meagar
Hi Guys, thank you all so very much for helping. It's seemingly such an easy task, and I feel I just hit a brick wall and not knowing how to fix it.
Ottoman
+1  A: 

without seeing your select options i think this might help you

if (isset($_POST['submit'])) {
    // Grab the output of the select list
    $Select_Output = mysqli_real_escape_string( $dbc, trim($_POST['item']));
    //What ever else you want to do here...
}

I also would use Miki725's post to make sure your select list is setup correctly

Drewdin
A: 

Hi Guys, thank you all so very much for helping. It's seemingly such an easy task, and I feel I just hit a brick wall 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 ""; 
echo "Select an item"; 
echo ""; //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 "" . $row['style']. "" . ""; 
} 
... ...

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 ...

Ottoman
+1  A: 

I am not sure exactly what you are doing but I would do something like this. For this example I assume that the values "dressmaker mannequin size 12" will correspond to the values of the columns in the database to which I will refer to as "colA, colB, colC, and colD", and in addition I assume you have an "ID" column in your database.

Here is the code I would use to generate the select drop-down list:

//$query is the variable storing the result of the mysql_query();
//assumption is that the result-set is non-empty

echo '<select name="item" id="item">\n'; //\n - new line character

//run through the loop to generate the items inside the list
while ($result = mysql_fetch_assoc($query)) {
    //note the id - it will be used to find data in the
    //database after the POST is complete

    //couple of temp variables (not necessary but makes code cleaner)
    $database_id = $result["ID"];
    $colA = $result["colA"]; //ex:dressmaker
    $colB = $result["colB"]; //ex:mannequin
    $colC = $result["colC"]; //ex:size
    $colD = $result["colD"]; //ex:12

    //add option to the select drop-down
    echo "<option value=\"$database_id\">$colA $colB $colC $colD</option>\n";
}
echo "</select>";

Now to retrieve the data from the POST. I am including the code Drewdin suggested.

//form was submitted already
//assumption is that the database connection is established
$item_id = mysql_real_escape_string(trim($_POST["item"]));

//Now get the info from the database for this id
//table is "table"
$string = "SELECT * FROM table WHERE ID = $item_id";
$query = mysql_query($string) or die("Could not complete the query: $string");

//assumption here is that the result set is non-empty
$result = mysql_fetch_assoc($query);

$colA = $result["colA"]; //ex:dressmaker
$colB = $result["colB"]; //ex:mannequin
$colC = $result["colC"]; //ex:size
$colD = $result["colD"]; //ex:12

//now you can use the values of colA-D to compute whatever you want

Hope this helps. Using database ids is nice for security plus it makes things more manageable.

Regarding using this blog. You can post comments to the answers people post. If you want however to add something to your question, you can edit your original question and just make sure its obvious what was added.

When the issue is resolved and if any answer helped and you liked it, you can pick it as the final answer by clicking the check mark next to it.

Best of luck

miki725
I understand now. All the problems I had was due to my lack of knowledge of (more on) form and database side. Had I used primary key id as the value of the value attribute, my problems would've been more obvious (to meself) in the first place. Instead, I was using style (which is kind of like an item code with multiple words) as the value, which created the problem. After reading many of the answers, I start to understand what the underlying problem was. Instead of getting a quick and temporary fix, I should make proper use of my database and html forms.
Ottoman
Sorry miki725, thank you also for helping me out with how to use this forum. Can I check many answers? Because I find almost all the answers really help. I initially didn't understand how they help, but now I do and I find most of them are very useful, including yours. Could I check many instead of one? :)
Ottoman
I am also new to this forum so don't know if you can check multiple answers, but I agree, usually many answers are very useful. Regarding learning databases, PHP, and HTML forms, when I was learning those things I found this book useful, especially for database things. I would suggest to check it out: "PHP 6/MySQL Programming for the Absolute Beginner" by Andrew B. Harris. Its very simple, yet presents important and useful ideas.
miki725
Thanks heaps miki, I've learned so much in one day :) I've modified my db to make full use of id column :) Thanks for recommend a book, I was thinking to get one but wasn't sure which one to get. I'll definitely look into the one you recommend. It's very handy to have one at hand. Thanks again, I'll be back :)
Ottoman