Insert selected item from dropdown list?

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
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Insert selected item from dropdown list?

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Insert selected item from dropdown list?

Post 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);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Insert selected item from dropdown list?

Post by nitediver »

Thanks, will try that.
Post Reply