Page 1 of 1

Insert selected item from dropdown list?

Posted: Wed Mar 31, 2010 11:36 pm
by nitediver
How to grab selected item from dropdown list, and put into variabel/database?

My code generating this error message, do I really need to use foreach() and htmlentities?
Error message.
[text]Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\spokat\ongkos.php on line 8[/text]

Code.

Code: Select all

<?
include "function.php";
connect();

if(isset($_POST['check']))
{
echo "Cost : ";

foreach($_POST['result'] AS $result){
$result = htmlentities($result);
echo "$result<br />";
}
}

echo "<form name=\"update\" method=\"post\" action=\"$PHP_SELF\">";

echo "<br>Cost :<select name=\"cost\">";
$price = mysql_query("SELECT * FROM price")or die ("Error");
while($result=mysql_fetch_array($price))
{
echo ("
<option value=$result[id]>$result[type] Rp. $result[cost]</option>
");
}
echo "</select>";

echo "<br><br>";

echo "<input type=\"submit\" value=\"Check\" name=\"check\">";
echo "</form>";
?>
Table.
[text]
---------------------------
id | type | cost
---------------------------
1 | Reguler | 50
2 | Express | 70
---------------------------
[/text]

Re: Insert selected item from dropdown list?

Posted: Thu Apr 01, 2010 12:21 am
by s.dot
Your value would be in $_POST['cost'] .. not in $_POST['result'].. and it will be a single value so you do not have to use foreach() on it. htmlentities is a good idea.

Code: Select all

echo htmlentities($_POST['cost'], ENT_QUOTES);

Re: Insert selected item from dropdown list?

Posted: Thu Apr 01, 2010 1:14 am
by nitediver
Thanks, will try that.