<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName from category");
while($drop = mysql_fetch_array($cat))
{
echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] . '</option>';
}
?>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'></select>
</select>
i am trying to use this form to create a search engine from my database but just cant get the value from the drop down menu.
$submit = $_POST['submit'];
if($submit)
{
$search = $_POST['search'];
$catval = $_POST['Category'];
echo $catval ;
$searchval = mysql_query("select * from item where iname like '%$search%'and cId in (select cId from category where cName = '$catval')");
while($info = mysql_fetch_array($searchval))
{
echo "Item Name: " . $info['iName'];
}
}
so when i try to search using this method i get no results.
You have placed the selects closing tag wrong :) and I assume you remembered to close your form aswell?
<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName, Category from category");
while($drop = mysql_fetch_array($cat))
{
echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] .'</option>';
}
?>
</select>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'>
</form>
<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName from category");
while($drop = mysql_fetch_array($cat))
{
echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] .
'</option>';
}
?>
</select>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'>
</form>
That should do the trick. Your </select> was in the wrong place.
In the meantime, you might want to look into PDO and bound values instead of using mysql() as it's depreciated (I.E. don't use it anymore) and insecure.
Related
I have a dropdown list that is filled with some values from a MySQL table.
The issue i'm facing is that I cannot place the ID from the selected value into a variable.
This is my code up until now:
<?php
mysql_connect('localhost', 'confidential', 'confidential');
mysql_select_db('mydb');
$sql = "SELECT zone_naam FROM zone";
$result = mysql_query($sql);
echo "<select name='zone_1' id='zone_1'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . " </option>";
}
echo "</select>";
?>
I would think that I could get the value from $row['idzone'] by using the code below.
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
I've tried several things, but I cannot come to a solution.
If I want to do the same thing in HTML with self set data like below it always works, but whenever I want to use PHP for this purpose I seem to fail.
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
I hope you all understand what I mean and can help me to find the cause of this problem.
Best regards,
Rudibwoyyy
#Rudi
Then David is correct. If you submit the HTML from your post
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
you can use, according the select name "zone", the variable $_POST['zone'], assuming you are using <form method="post">, (otherwise it's $_GET['zone']) will contain the value (1, 2 or 3 respectively) of the selected entry.
If this doesn't work for you, check if the generated HTML Code hat the correct name.
Below is my code that works!! Thanks for all the tips
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('confidential');
$sql = "SELECT * FROM zone";
$result = mysql_query($sql);
echo "<form type='submit' class='form-inline' action='' method='POST'>
<select name='zone_1' id='zone_1' class='form-control mb-2 mr-sm-2 mb-sm-
0'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<form type=\"submit\" method=\"POST\"> ";
echo " <select name=\"richting\" class='form-control mb-2 mr-sm-2 mb-sm-0'> ";
echo " <option value=\"<-->\"> <--> </option> ";
echo " <option value=\"<--\"> <-- </option> ";
echo " <option value=\"-->\"> --> </option> ";
echo "</select> ";
$result2 = mysql_query($sql);
echo "<select name='zone_2' class='form-control mb-2 mr-sm-2 mb-sm-0'>";
while ($row = mysql_fetch_array($result2)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<br><br>";
echo "<input type='submit' value='Submit' name='submit' id='submit' class='btn btn-primary btn-sm'>";
echo "</form>";
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
?>
I can't pass the selected value to the listing page. When I select an option and click search, a default value passing to listing page. (Default value: last added value)
$sql = mysqli_query($con, "select * from jobregestation order by id");
while ($row = $sql->fetch_assoc()) {
$id = $row['id'];
$name = $row['JobName'];
echo "<option value=" . $id . ">" . $name . "</option>";
}
Thanks in advance.
Are you looking forward something like that ?
<?php
if(isset($_POST['submit'])){ // worked when click on serarch button
echo $_POST['exampleName'];
}
?>
<form action="" method='post'>
<select name="exampleName">
<?php
$sql = mysqli_query($con, "select * from jobregestation order by id");
while ($row = $sql->fetch_assoc()) {
$id = $row['id'];
$name = $row['JobName'];
echo "<option value=" . $id . ">" . $name . "</option>";
}
?>
</select>
<input type="submit" name="submit" value="Search"/>
</form>
<select>
<?php
foreach ($sql->fetch_assoc() as $key=>$value)
{
echo "<option value = " .$value['id'] . ">" .$value['Jobname]."</option>";
}
?>
</select>
This should work.
I'm trying post 3 variables in order to insert them in table2. As you can see I'm using a SELECT to get 3 variables from table1 to insert into a table2 but can't see the input value when looking at the source.
The loop works. I get results in the loop but the input post echo's nothing. I've tried many different ways but can't seem to get it to work. Can someone help?
<?php
include('theconnection.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('cannot connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thebooks");
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
?>
<input type="hidden" name="name" id="name" value="<?php echo "{$_POST['name']}"; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo "{$_POST['cover']}"; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo "{$_POST['pageno']}"; ?>">
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
I don't see why there are two queries. You can just drop the first one. Also, I'd like to suggest you this-
Instead of echoing like this
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
echo it like this-
while($row = mysqli_fetch_array($result))
{
echo "<input type='text' value='" . $row[name] . "' name='name' id='name'>";
echo "<input type='text' value='" . $row[cover] . "' name='cover' id='cover'>";
echo "<input type='text' value='" . $row[pageno] . "' name='pageno' id='pageno'>";
}
In your last bit of code, please make these small changes-
<input type="hidden" name="name" id="name" value="<?php echo $_POST['name']; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo $_POST['cover']; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo $_POST['pageno']; ?>">
Please clarify- why would you echo input controls in a loop? You could potentially get hundreds of input controls rendered on your page. Now you don't want that do you? Hope my answer helps.
i have a search form whose action attribute contains an id to be passed in the URL and form is submitted from get method for user to get the search results to be shared or bookmarked.
Now the problem i am facing is, that id i have passed in action is not displaying in the URL but all other input field's values are appearing fine.
This is my form:
<form action="trails.php?pname=<?php echo $getName;?>" id="filter" method="GET" name="filter">
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='1'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='2'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
<select class="multiselect form-control" multiple="multiple" name='activity[]' id="activity">
<?php
$toaSql = mysql_query("select * from type_of_activity where cat_id='3'");
while ($toaRow = mysql_fetch_array($toaSql)) {
echo "<option value='" . $toaRow['typeOfActivity'] . "'><img style='width: 21px; margin-left: 5px;' src='home/images/" . $toaRow['catImg'] . "'> " . $toaRow['typeOfActivity'] . "</option>";
}
?>
</select>
</form>
trail.php page:
if(isset($_GET['pname'])){
$getName = $_GET['pname'];
// print_R($getName);
if($getName=='video'){
$queryVideo = "SELECT * FROM `contributevideo` WHERE status='active' " ;
//echo $queryVideo;
}elseif($getName=='album'){
$queryAlbum = "SELECT * FROM `contributeimage` WHERE status='active' AND category='album'" ;
}
}
Trails.php page run the sql queries according to the page name it gets from the url, but in url its not finding any pname variable so the queries are not executing.
Where i am doing wrong please guide.
Thanks
Why are you describing parameter as URL-encoded?
<form action="trails.php?pname=<?php echo $getName;?>" id="filter" method="GET" name="filter">
// Existing form
Since your form's method is 'GET', you can just describe a hidden input.
<form action="trails.php">
<input type="hidden" name="pname" value="<?php echo $getName; ?>"/>
// Existing form
It should work.
The issue with your code is that you set the form to submit the values using the GET method. This means that the values from the form fields will be passed in the URL, overwriting (removing) your pname value.
To fix it just change the method of the form to POST and the PHP code to access activities through $_POST['activities'], it should work then.
I'm new to PHP... I want to know how to populate a form from mySQL. I'm stumped at the input section where I am trying to allow the user to select choices for radio button /checkbox for each record. thanks in advance for anyone's help!
TC
Here's a snippet of my code:
<?php
$mQuantity = 1;
$con = mysql_connect("localhost","t","c");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("tc", $con);
/*
if request.form("itemname")<>" " then
cSQLAcct = "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%' ORDER BY FOODTYPE, ITEM"
else
cSQLAcct = "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' ORDER BY FOODTYPE, ITEM"
end if
*/
// retrieve form data
$input = $_POST['itemname'];
echo $_POST['ITEM'];
$mItem = $_POST['ITEM'];
$mPrice = $_POST['PRICE'];
$mQuantity = $_POST['QUANTITY'];
$mOrderTotal = 0;
$mSide0 = '1';
$mOil = '1';
$mStarch = '1';
$mSalt = '1';
// use it
echo "You searched for: <i>$input</i>";
echo $mSessionID;
mysql_query("INSERT INTO OrderQueue (SessionID,item,quantity,price,no_oil,no_starch,no_salt,side0) VALUES ('$mSessionID','$mItem','$mQuantity','$mPrice','$mOil','$mStarch','$mSalt','$mSide0')");
/*
$sql="SELECT * FROM Restaurant_Menus
WHERE
Item like '%$input%'";
*/
$sql="SELECT * FROM OrderQueue
WHERE
SessionID = '$mSessionID'";
$result = mysql_query($sql);
//('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
//echo $input;
//echo $sql;
/*
if ($input!=" "){
$result = mysql_query($sql);
}
// cSQLAcct = "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%' ORDER BY
else
{ $result = mysql_query("SELECT * FROM Restaurant_Menus");
}
*/
$c=1;
$class[1] = 'odd';
$class[2] = '';
$array_no_oil = array();
$array_no_salt = array();
$array_no_starch = array();
$array_rice = array();
echo "<table border='1' width=500px>
<tr>
<th></th>
<th align=left>Item</th>
<th align=right>Price</th>
<th align=right>Quantity</th>
</tr>";
//<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">
while($row = mysql_fetch_array($result))
{
//echo '<tr class="'.$class[$c].'" onMouseOver='#F3EB49' onMouseOut ='#DDDDDD' bgcolor="#DDDDDD" >';
//echo "<tr class='odd'>";
//echo '<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">'
?>
<TR onMouseover="this.bgColor='#FFC000'"onMouseout="this.bgColor='#DDDDDD'">
<?php
/*
<form action="Orders.asp" method="post" target="_top" name="LogonForm">
<td><font size = 2>
<!--<%= mQuantity %>-->
<INPUT TYPE="TEXT" NAME="QUAN" VALUE="1" SIZE=2>
</font></td>
<td width=50px><font size = 2>
<INPUT TYPE=HIDDEN NAME=ITEM VALUE="<% =mItem %>">
<INPUT TYPE=HIDDEN NAME=PRICE VALUE=<% =mPrice %>>
<INPUT TYPE=HIDDEN NAME=RID VALUE= <% =mRestaurant_ID %>>
<INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
<input id="Choices" class="findit" type="submit" value ="Order" />
</form>
*/
?>
<?php
// Obtain list of images from directory
//$img = getRandomFromArray($imgList);
}
?>
<?php
if ($row['Picture']!=" "){
echo "<td><a><img src='images/".$row['Picture'].".JPG' height=50px></a></td>";
}
else{
echo "<td></td>";
}
echo "<td width=200><b>" . $row['Item'] . "</b><br>
<input class='dropwidth' type='radio' name='$array_rice' value='1' selected>White Rice<br>
<input type='radio' name='$array_rice' value='2'>Pork Fried Rice<br>
<input type='radio' name='$array_rice' value='3'>Brown Rice<br>
<input type='checkbox' name='$array_no_oil' value='1' />No Oil
<input type='checkbox' name='starch' value='no oil' />No Starch
<input type='checkbox' name='salt' value='no salt' />No Salt
</td>";
echo "<td width=50 align=right>" . number_format($row['Price'],2) . "</td>";
$mQuantity = "'" . number_format($row['Quantity'],0) . "'";
$mPrice = "'" . number_format($row['Price'],2) . "'";
$mLineItemTotal = $row['Quantity'] * $row['Price'];
$mOrderTotal = (number_format($mOrderTotal,2) + number_format($mLineItemTotal,2));
echo $mOrderTotal;
$mLineItemTotal2 = "'". number_format($mLineItemTotal,2) . "'";
//echo "<td>" . $mQuantity. "</td>";
?>
<form action="orders.php" method="post" target="_top" name="LogonForm">
<td width="50" align=right><font size = 2>
<!--<%= mQuantity %>-->
<!--<INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php $mQuantity; ?>>-->
<INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php echo $mQuantity.";" ?>/>
</font></td>
<?php echo "<td width=50 align=right>" . $mLineItemTotal . "</td>";?>
<!--<td width=50px><font size = 2>-->
<!--<INPUT TYPE="TEXT" NAME="LINEITEMTOTAL" VALUE=<?php echo $mLineItemTotal.";" ?> WIDTH=10/>-->
<INPUT TYPE=HIDDEN NAME=ITEM VALUE=<?php $mItem ?> />
<INPUT TYPE=HIDDEN NAME=PRICE VALUE=<?php $mPrice ?>/>
<INPUT TYPE=HIDDEN NAME=RID VALUE=<?php $mRestaurant_ID ?>/>
<INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
<!--<input id="Choices" class="findit" type="submit" value ="Order" />-->
</form>
<?php
echo "</tr>";
if($c==2) $c=0;
$c++;
}
echo "</table>";
echo "<div>".$mOrderTotal."</div>";
mysql_close($con);
?>
You should name your HTML form elements as such: MyElement[] when using them as arrays. For example:
<form method="post" name="myForm" action="myForm.php">
<select multiple name="myFormSelectMultiple[]">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
In this select multiple example, when the first two items are selected and then posted this form would produce similar output to the following:
array('myFormSelectMultiple' => array(1,2));
Hope this helps!