My update statement isn't working correctly, I am attempting to pull the data from the database, populate a dropdown with either "Y" or "N" inside it, on submit the values are entered into the database and the page refreshes.
So far I have my list of items, each with correctly populated dropdown, it is now my submit that is failing to work.
<?php
$updatedFeatProd = $_POST['featuredProduct'];
var_dump($updatedFeatProd);
if ($_POST) {
foreach ($_POST['featuredProduct'] as $key => $val) {
$query = 'UPDATE tblProducts SET featuredProduct = ' . $updatedFeatProd . '
WHERE fldID = ' . $val;
$sql = dbQuery($query);
}
}
$sql = dbQuery('SELECT fldId, fldName, featuredProduct FROM tblProducts');
?>
<form method="post" action="#" name="featuredProd">
<table>
<tr><td><p>Product Name</p></td><td><p>Is a featured product?</p></td></tr>
<?php
$products = dbFetchAll($sql);
foreach ($products as $product) {
//var_dump($product['fldName']);
?>
<tr>
<td>
<p><?php echo $product['fldName']; ?></p>
</td>
<td>
<select name="featuredDropdown">;
<?php
if ($product['featuredProduct'] == 'Y') {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">N</option>
<?php
} else {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">Y</option>
<?php
}
?>
</select>
</td>
</tr>
<?php
}
?>
The presentaton here does not make much sence. You have a dropdown with the ProductName in one slot and a 'N' in another.
Once the user has selected 'N' for a product they have no idea what they have said NO to, as they can no longer see the product name that they have selected NO for.
It would make more sence to provide a <label> containing the product name and a YES/NO dropdown beside it for them to select from.
However the reason your update code is not working is that you have called the dropdown featuredDropdown
<Select name="featuredDropdown">
and you are trying to process a field called featuredProduct in the update code
foreach ($_POST['featuredProduct'] as $key => $val) {
Your next problem will probably be that you are oututting more than one <Select name="featuredDropdown"> so you need to make that into an array as well like this:
<Select name="featuredDropdown[]">
Then you will have an array of featuredDropdown in the $_POST array. $_POST['featuredDropdown'][]
Related
I have a function having two dropdown list and one submit button, the user must choose a value from dropdown list A and a value from dropdown list B so he can submit them.
I knew that I need to have an attribute in the value section as follows
<select name="students" id="students">
<?php
while($data = mysqli_fetch_array($selectstudent1)) {
$name=$data['name'];
$idname =$data['user_id_student'];
?>
<option name="studentdd" value="<?php echo $idname; ?>"><?php
echo $name; ?></option>
<?php } ?>
</select>
and the second dropdown list here
<select name="groups" id="groups">
<?php
while($data1 = mysqli_fetch_array($selectgroup1)) {
$groupschoose = $data1['group_number']; ?>
<option name="nogroup" value="<?php echo $groupschoose; ?>">
<?php echo $groupschoose; ?></option>
<?php }//while ?>
</select>
so, i need to get the "php echo $idname" (student id) from A dropdown list
and "php echo $groupschoose; " (group id) form drop downlist B
so I tried this PHP code as follows:
<?php
if(isset($_POST['addmember'])){
//addmember is id for dropdownlists form
$groupnum= $_POST['nogroup'];
$stuid=$_POST['studentdd'];
echo $groupnum;
echo $stuid ;
$sq="SELECT user_id_student FROM student WHERE
user_id_student=$stuid ";
$sq1 = mysqli_query($con,$sq);
while ($sq2 = mysqli_fetch_array($sq1)) {
$tes = $sq2['user_id_student'];
if( $tes == $stuid){
$sqlmem="UPDATE student
set group_id= $groupnum
WHERE user_id_student=$stuid";
$resultmem = mysqli_query($con,$sqlmem);}
}
}
?>
But it won't work, and I'm not sure where is the missing section
I hope my Q is clear and I'm sorry if it's little bit long, but i'm looking for your help, thank you!!
I am printing a simple select list out by looping through an associative array which is populated elsewhere.
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This select list gives the options available from the array. However, I am trying to pre-populate the list with the selected value, for example if a user is updating their user information, then I would like to set that choice at the top of the list (ideally without the choice appearing further down the list).
I have access to this data in a variable which I have queried:
$this->selected_choice //value
$this->selected_id //key
Now I have been testing some solutions
<select id="choice" name="choice">
<option name="<?php echo $this->selected_choice; ?>" value="<?php echo $this->selected_id;?>">"<?php echo $this->selected_choice; ?></option>
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This solution does display the users choice on top of the list, but the foreach loop will also display this option as there is no indication that it shouldn't.
This is my question to you guys, how can I do this so the the selected_choice appears at the top of the list, but will then not appear in the foreach array.
I am hoping there is some simple solution to this problem, as it is not so important, but something I come across quite a lot.
Option have no "name". Name is attribute for SELECT tag.
If option is selected, you need to pass "selected" attribute in body of option
Ex: <option value="1" selected>Tramtada</option>
Do it this way:
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option <?php if($key == $this->selected_id) {?> selected <?php } ?> value=".$key.">" .$value. "</option>" ;
}
?>
</select>
I'm having issues getting the user selected text from the select tag. I've looked at the other similar questions but none really helps.
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<select name="Select" id="vals" onchange="this.form.submit()" class="btn btn-warning">
<option value="">-- Select Country --</option>
<?php
$user = new User();
$sql5 = DB::getInstance()->get('country', array('user', '=', $user->data()->username));
if (!$sql5->count()) {
echo 'No data';
} else {
foreach ($sql5->results() as $sql5) {
?>
<option value="country"><?php echo $sql5->name;?></option>'; <?php
}}
?>
</select>
</form>
</div>
The dropdown has a list of countries assigned to the user but when I click on it to output details on the country, nothing shows.
<?php
if (isset($_POST['Select']) && !empty($_POST['Select'])) {
$userSelection = $_POST['Select'];
$sql = DB::getInstance()->get('country',array('country', '=', $userSelection));
if (!$sql->count()) {
echo 'no data';
} else {
foreach ($sql->results() as $sql) {
echo 'data'; }
The query gets the data of the country selected from the country table. When I change the $userSelection in the query to a specific country name, the data for that country shows. I think the problem might be the option value within the select tag but I'm not sure.
The value is the same on all requests. Update to
<option value="<?php echo $sql5->country ?>"><?php echo $sql5->name;?></option>
This is assuming the identifying column is country since that is your where clause in the select query.
I have a dropdown showing options from mysql, no problem there. The problem is on the update page when i want to show the option already selected previously.
The dropdown selects options from the margins table and puts the value into a field in the products table.
This is the query that selects the product record :
<?php
$recordID = $_GET["recordID"];
$product_result = mysqli_query($con,"SELECT * FROM products WHERE product_code='$recordID'") or die(mysqli_error($con));
$product = mysqli_fetch_array($product_result);
$checked_special = $product['product_special'];
$checked_publish = $product['product_publish'];
$checked_frontpage = $product['product_display_frontpage'];
$checked_facebook = $product['display_facebook'];
{
?>
And this is the part of the form that gets the options from the margins table and displays them on page.
<tr>
<td>Display Facebook</td>
<td><input type="checkbox" name="display_facebook" id="display_facebook" value="y" <?php if ($checked_facebook == 'y') echo 'checked'; ?> /></td>
<td><strong>Margin Group :</strong></td>
<td>
<select name="margin_group" id="margin_group"><?php
$resul2 = mysqli_query($con,"SELECT * FROM margins");
while($row2 = mysqli_fetch_array($resul2))
{
?> <option value="<?php echo $row2['margin_group']; ?>"> <?php echo $row2['margin_group']; ?></option>
<?php } ?> </select></td>
</tr>
How can i get the $product['margin_group'] value from the products table show as selected option in the dropdown, so that the user doesn't have to reselect every time they update the page.
Thanks :)
MsKazza
The idea is to add the word selected in the desired option tag like this :
<option value="x" selected>x</option>
This way it will be selected in the form Check this
in order to do that we will make a conditional statement for every option value in the while loop. If the value meets the condition, we will echo the word selected
<?php while($row2 = mysql_fetch_array($resul2): ?>
<option value="<?= $row2['margin_group']; ?>"
<?php if($row2['margin_group']) == $products_table_variable) : ?>
selected
<?php endif; ?>
><?= $row2['margin_group']; ?></option>
<?php endwhile ?>
<select name="margin_group" id="margin_group">
<?php
$datasource = mysqli_query($con,"SELECT * FROM margins");
while($getdata= mysql_fetch_array($$datasource)){
?>
<option value="<?=$row2['margin_group']?>" <?php if($getdata['colume_name']==$row2['margin_group']) echo "selected";?>> <?=$row2['margin_group']?></option>
<?php } ?>
</select>
Hope it will help you :)
I have a parser which extracts information into array $item. From my database, I am pulling all results where I store "Types" into array $getFormats.
I am then displaying $item into a big form which the user can modify (to fix any errors from the parser). In partiular, I am forcusing on $item[3] which contains "types" extracted by the parser.
My form displays $item[3] next to my dropdown list containing possible "types" contained in the database. The goal here is to allow the user to match or change the "type" the parser finds with a "type" contained in the database.
Obviously, I want the default dropdown value to be a value which matches one already in the database. If there is no match, then the default would be blank for the user to choose an appropriate type himself.
The code below demonstrates what I have. Obviously, I am going about it wrong as it doesn't work.
<form>
<?php
foreach($articles as $item) {
?>
Type: <input type="text" name="typeparsed" value="<?php echo $item[3]; ?>">
<select name="typechoose" value="<?php echo $value; ?>">
<?php
foreach ($getFormats as $format) {
$selected = '';
if($item[3] == 'entry form' && $format->name == 'Entry Form'){
$selected = 'selected';
}
echo "<option value='".$format->name."' ".$selected.">".$format->name."</option>";
}
?>
</select>
<?php }?>
</form>
$item[3] is the array containing "type" extracted by the parser.
$format->name is the array containing the dropdown options pulled from the database.
The above code works but my problem is that $format->name contains two instances of the same value.
My database has the following "types":
Entry Form
Facebook
Entry Form
This is because of the way wordpress stores hierarchical options. On my site, these values are actually as follows:
Entry Form
Facebook
- Entry Form
As you can see, it is because Entry Form is a top menu item AND a submenu of Facebook.
So, as I am trying to match a type of entry form with Entry Form, I run into a problem when trying to determine WHICH Entry Form is the right one to select.
As my code is presently, the second instance of Entry Form is selected.
How can I adapt the code so that if $item[3] == 'entry form' (as it is currently), then the FIRST Entry Form will be selected?
First thing is I don't understend why You use this:
<?php
foreach($articles as $item) {
if ($item[3] == 'entry form') {
$value = 'entry form';
} else {
$value = $item[3];
}
?>
You can simply use the code brlowe and it will do exacly the same:
<?php
foreach($articles as $item) {
$value = $item[3];
?>
To match selected element do that:
Type: <input type="text" name="typeparsed" value="<?php echo $item[3]; ?>">
<select name="typechoose" value="<?php echo $value; ?>">
<?php
foreach ($getFormats as $format) {
$selected = '';
if($value == $format->name){ $selected = ' selected="selected" '; }
echo "<option value='".$format->name."' ."$selected".>".$format->name."</option>";
}
?>
</select>
<?php }?>
Becous You have change Your question I will put another answer witch is simple: You will not determine that as long as both "Entry Form's" have the same name. If You can check them for they database ID's please try to use it, if not then You should change their names.
<?php // $value is actualy ID of the element; ?>
Type: <input type="text" name="typeparsed" value="<?php echo $item[3]; ?>">
<select name="typechoose" value="<?php echo $value; ?>">
<?php
foreach ($getFormats as $format) {
$selected = '';
if($value == $format->id){ $selected = ' selected="selected" '; }
echo "<option value='".$format->id."' ."$selected".>".$format->name."</option>";
}
?>
</select>
And as value pass ID of the selected element.