Making a dynamic drop down menu sticky in php - php

I have a dynamic drop down menu on a PHP form which is working fine in that it retrieves/inputs the right id and will not process the form if no option is collected.
However, I am not sure how to make it sticky. I can do it on a static drop down with no problems but obviously I am missing something, can anyone help?
Below is the drop down menu:
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0] \" <?php if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == '$row[0]') {echo 'selected=\"selected\"';} ?> >$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';
Below is the validation code
if (isset($_POST['dealership_id'])) {
$dealer_id = (int) $_POST['dealership_id'];
} else {
$dealer_id = 0;
}
if ($dealer_id > 0) {
$query = "SELECT dealership_id FROM dealership WHERE dealership_id=$dealer_id";
$result = mysql_query ($query); }
else {
echo '<p><font color="red">Please select your Dealership</font></p>';
}
BTW, row 0 is the primary key, row 3 is the name.

I don't think there should be single quotes around $row[0] in the following:
$_POST['dealership_id'] == '$row[0]'
By using single quotes you are literally comparing the string $row[0] instead of the variable value
Here's your code with some changes that are at least valid syntax; I didn't test to see if it works, but it should. It would be helpful for you to research string concatenation in php, some useful info here: http://www.php.net/manual/en/language.operators.string.php
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\"";
if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == $row[0]){
echo ' selected=\"selected\"';
}
echo ">$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';

Related

How to dynamically get the value of a select tag from MySQL table

Thanks for dropping by and I'm sorry if this question seems to be easy as I'm kinda new to php. Im trying to do a school project and my problem now is that I cant seem to get the values of the dropdown. I have been trying to search in every forum and some similar questions in here but they seem to only show how i can populate the dropdown with database values.
The code for populating the dropdown menu is working, but recording the selected value is not working for me.
Here is my php code for the dropdown:
<div class="panel">
<label class="panelname">Category:</label>
<select name="category" class="signup_field">
<option>Select One</option>
<? // retrieve all the categories and add to the pull down menu
$query = 'SELECT category_id, name FROM category ORDER BY name ASC';
$request = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$cat_id = $row[0];
echo "<option value=\"row[0]\"";
//check for stickyness:
if (isset($_POST['category']) && ($_POST['category'] == $row[0])) {
echo ' selected="selected"';
//$_POST[]
} echo ">$row[1]</option>\n";
}
?>
</select>
<? if (array_key_exists('category', $add_product_errors)) echo '<label class="error">' . $add_product_errors['category'] . '</label>'; ?>
</div>
and here is my part of my code for validation: -
// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for a category:
if (!isset($_POST['category']) || !filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
$add_product_errors['category'] = 'Please select a category!';
}
..... // other codes for validation (Eg: if (empty($add_product_errors)) { proceed to query} )
I don't know what seems to be the problem but there are no values recorded in the $_POST['category'] for me to save into MySQL and I keep getting the Please select a category error.
Thank you all for your help. :)
your problem seems to be echo "<option value=\"row[0]\"";. You want to replace it with
echo "<option value=\"$row[0]\"";
echo "<option value=\"row[0]\""; just puts a String row[0] there
Thanks, I seem to have solve the problem ..
I have seemed to have deleted the ($)row in the row[0] for it to be read properly.
just put $ on the row[0]:
echo "<option value=\"row[0]\"";
I used to solve this problem like ... In your case.
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$cat_id = $row[0];
$str = null;
if (isset($_POST['category']) && ($_POST['category'] == $row[0])) {
$str = 'selected="selected"';
}
echo '<option value="'.$row[0].'" '.$str.'>'.$row[1].'</option>';
}

PHP get value under mysql row

I'm trying to get a value that is associated (through mysql data-base row) to a unique Id through an option form:
<form>
<td>
<select name='rolo'>
<option value='$id'>$item</option>
</td>
<td>
$value
</td>
</select>
</form>
So, when I choose an option, I get the correspondent $value associated with it in the next .
What I have tried so far:
$query = "SELECT * FROM rolostock WHERE id='$id' ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo $row['value'];
}
I am not a php expertise, and I thought this would do it? Thanks in advance!
EDIT:
What i'm trying is to (html form) SELECT > OPTION $id > (php) get $value of $id in the next (html) TD . :\
Try with this:
$query = "SELECT * FROM `rolostock` WHERE id = '$id';";
$result = mysql_query($query);
if (!$result) exit("The query did not succeded");
else {
while ($row = mysql_fetch_array($result)) {
echo $row['value'];
}
}
If running this prints The query did not succeded then you have an error in your query. Try running it via PhpMyAdmin.
Also use:
<option value='<?php echo $id;?>'><?=php echo $item;?></option>
instead of:
<option value='$id'>$item</option>

Display checked checkbox record from database

I have looked through similar problems and solution but somehow only half way help me with my problem. I'm trying to make a form to checked more than one record from MySQL database and display the checked record to another page. Somehow I managed to do the page with check boxes but I don't know how to display the record checked. It can only display the first row of the record or all the records regardless which box are checked.
This is checkbox page
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
{
$rows[] = $row['IllNo'];
}
foreach($rows as $value);
echo "";
echo " ";
echo $row['IllNo'];
echo "";
}
echo "";
?>
This is display record checked
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$rows[]=$row['IllNo'];
foreach($rows as $value);
if ($rows= 'checked') {
echo "";
echo $value;
}
Any help are welcome. Thank you.
There's actually a lot of problems with that script including syntax errors, calling the wrong variable name, form not opening where it should, invoking PHP after you already have, etc...
To get a good answer to you, you should share what make $row['IllNo'] should equal to indicate if it should be checked or not.
I reformatted it a bit and this may give you a good start.
<form NAME ="form1" METHOD ="POST" ACTION ="dari.php">
<table>
<?php
$columns = count($fieldarray);
//run the query
$result = mysql_query("SELECT * FROM request_item ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error()) ;
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
echo "<tr><td>";
echo "<Input type = 'Checkbox' Name ='ch1' value ='ch1'";
// check checked if it is. this will be checked if $row['IllNo'] has a value
// if there were a condition to make it checked, you would put the condition
// before the ?
echo $row['IllNo'] ? ' checked' : '';
echo ' />';
echo $row['IllNo'];
echo "</td></tr>";
}
?>
</table>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Choose your books">
</FORM>

Problem populating a dropdown box with MySQL query results (PHP/MySQL)

so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.
here is my code:
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>
The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.
I would appreciate anyones thoughts :D
thanks
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
add . before = and initiate $aa = ''; before while loop
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
$options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>
should work. You forgot a . in your while loop

Need help pulling into multiple columns from one database table - mysql_query

Afternoon all,
A very quick question... a friend has set up a form for me using mysql_query. Since he wrote this, I have added an extra column into the database, which I want to pull through into the form.
However I can't seem to get this extra column to appear (labelled Currency). The reason I need it is the query below will pulls back a value and the £ symbol. Because I want to display not only £, but also € prices, I need this extra column to pull through (obviously I will have to remove the £ from the echo below too).
I've tried adding the extra column (Currency) to the code, e.g. "SELECT Room, Currency, Price FROM Spa_Upgrades
but this hasn't worked.
The code is:
<?php
if (isset($id))
{
$query2 = mysql_query("SELECT Room, Price FROM Spa_Upgrades WHERE Spa_Upgrades.Spa_Id = '".$id."' AND Spa_Upgrades.Id = '".$pack."' order by Spa_Upgrades.Order");
$rows = mysql_num_rows($query2);
if($rows==0) {echo "disabled='disabled'/>";}
else
{
echo "/>";
echo "<option value='Default'>Please Select</option>";
for($i=0; $i<$rows; $i++)
{
$result2 = mysql_fetch_array($query2);
echo "<option value='".$result2[0]." £".$result2[1]."pp'>$result2[0] £$result2[1]pp</option>";
}
}
}
Hugely grateful if someone can solve this!
Thanks,
Motley
Alter the query as follows:
SELECT Room, Price, Currency FROM Spa_Upgrades ...
Alter the line beginning echo inside the for loop: replace £ with $result2[2] wherever it appears. (Or if the Currency column doesn't contain the HTML entity for the currency symbol, then replace £ with appropriate code to obtain the symbol from the Currency column entry.)
You also need to add the column to the output... I would also switch to an associative array otherwise if you add a column and its not at the end you have to change all the indexes.
if (isset($id))
{
$query2 = mysql_query("SELECT Room, Price, Currency FROM Spa_Upgrades WHERE Spa_Upgrades.Spa_Id = '".$id."' AND Spa_Upgrades.Id = '".$pack."' order by Spa_Upgrades.Order");
$rows = mysql_num_rows($query2);
if($rows==0) {echo "disabled='disabled'/>";}
else
{
echo "/>";
echo "<option value='Default'>Please Select</option>";
for($i=0; $i<$rows; $i++)
{
$result2 = mysql_fetch_assoc($query2);
$value = $result2['Room'] . ' ' . $result2['Currency'].$result2['Price'].'pp';
echo sprintf('<option value="%s">%s</option>', $value, $value);
}
}
}
Use mysql_fetch_assoc() instead of mysql_fetch_array()
A good practice as well is to separate data retrieving from display logic
Try this:
<?php
$results = array();
if (isset($id))
{
$resource = mysql_query("SELECT room, currency, price FROM Spa_Upgrades
WHERE Spa_Upgrades.Spa_Id = '".intval($id)."' AND Spa_Upgrades.Id = '".intval($pack)."'
order by Spa_Upgrades.Order");
while($row = mysql_fetch_assoc($resource))
$results[] = $row;
}
?>
<select name="..." <?php echo (count($results) > 0 ? '' : 'disabled') ?>>
<option value="Default">Please Select</option>
<?php
foreach($results as $result)
{
$value = $result['room'].' '.$result['currency'].$result['price'].'pp';
echo '<option value="'.htmlspecialchars($value).'">'.htmlspecialchars($value).'</option>'."\n";
}
?>
</select>

Categories