I am making a product recommender in my shopping cart, something similar to when you go to the grocery store and pickup candy as an impulse.
It looks like below. When I click on the dropdown menu, it does not dropdown with the data I am looking for.
I am using the following code that I would think would input this data:
<?php foreach($Fluoros['Fluoro'] as $Fluoro) {
echo $Fluoro['Fluoro'], '<br>';
}
?>
<select title="Add Fluoro To Your Cart" id="<?php echo $Fluoro['Description']; ?>" class="selectpicker white-drop crimp-part" data-width="auto">
<select title="Select Fluoro" id="<?php echo $Fluoros['Product']; ?>" class="selectpicker white-drop crimp-part" data-width="auto">
<?php foreach($Fluoros['Product'] as $Fluoro){
echo '<option ' . 'value = ' . $Fluoro['Fluoro'] . ' </option>';
}
?>
</select> ```
[enter image description here][1]
[1]: https://i.stack.imgur.com/AXygI.png
The data is being pulled in my "Cart " class. The Select statement below works and brings the correct results
```php
public function fluoroCart() {
$getFluoroProducts = $this->_db->query("SELECT i.Part_Number, i.Description
FROM Inventory i
INNER JOIN Product_Details pd on pd.Part_Number = i.Part_Number
WHERE pd.Category_ID = 15");
$fluoroProducts = $this->_db->results();
foreach($fluoroProducts as $Fluoro){
$Flouros [] = array( 'Fluoro'=>$Fluoro->Part_Number, 'Description'=>$Fluoro->Description);
}
}```
In HTML, select option values need to be surrounded by quotes. Also, in addition to indicating a value, you need to put some text between the opening and closing option tags for it to actually show up in the menu.
<select>
<option value="some value">Some Value Text</option>
</select>
You need to modify your PHP like this
echo '<option value = "' . $Fluoro['Fluoro'] . '">'.$Fluoro['Fluoro'].' </option>';
Related
I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?
I've got a table that populates data from a MYSQL database and populates a drop-down menu from the same database. I have the drop down menu and table just fine, I would like to be able to choose which data I show in the table however.
<select name = 'peer-id' method='post' style = 'position: relative'>
<?php
while ($content = mysql_fetch_array($peer)) {
echo "<option value='" . $content['Peer'] . "'>" . $content['Peer'] . "</option>";
}
$results = mysql_query("SELECT Destination FROM rate ");
?>
</select>
That's what I have for the select box. How can I get the choice from that and save that as a variable and refresh the table data?
I need to clarify that this will change that current data
#Data#Data#Data
#Data#Data#Data
#Data#Data#Data
Then choose drop down choice and I want it to show new data
#Data2#Data2#Data2
#Data2#Data2#Data2
#Data2#Data2#Data2
So it's going to need to load a new page or refresh some how because it's changing via PHP and not javascript.
I think form may be better, for example
<form id="myform" method="post">
<select name = 'peer-id' style = 'position: relative' onchange="change()">
<option value="1">12</option>
<option value="2">15</option>
<option value="3">16</option>
<option value="4">18</option>
</select>
</form>
<script>
function change(){
document.getElementById("myform").submit();
}
</script>
In the above code, whenever you change the value of select, it will post to the backend, then according to the posted value, you can do want you want, to get the peer-id in php, you can use the following code
$peer-id = $_POST['peer-id'];
Hope helps!
apply this code in select tag hope this works
<select onchange="location = this.options[this.selectedIndex].value;" style="text-decoration:none;">
<option value="customers.php"></font></option>
</select>
insted of the static options, you can do it like this :) here you get all the options from the database. Just replace it with the static options
$peer = mysql_query("SELECT Peer FROM rate Group By Peer Where peer = 'variable'");
$result_peer = mysql_query($peer);
if($result_peer){
while($row_peer = mysql_fetch_array($result_peer)){
echo'<option value='.$row_peer['Peer'].'>'.$row_peer['Peer'].'</option>';
}
I agree in using form, and with this you can echo back onto the page with a submit button (code tested):
<form id="myForm" method="POST">
<select name="select" onchange="<?php echo $_SERVER['PHP_SELF'];?>">
<option value="N">No</option>
<option value="Y">Yes</option>
</select>
<input type="submit" name="formSubmit" value="Submit" >
</form>
<?php
if(isset($_POST['formSubmit']) ){
$var = $_POST['select'];
$query = "SELECT * FROM table_name WHERE DesiredField='$var'";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$var2 = $row['FieldName'];
echo "First field: " . $var2 . "<br>";
// and so on for what you want to echo out
}
}
?>
I'm back and this one might be a little tricky but we will find out! :D
Okay I am using php to self populate my select fields so I don't have to continuously update the options when new agents join. I am using ajax (javascript) for this part of the page and I have had no problems with this except with the company select field.
Live site
All my select fields work with out any problems but the Company field which will display nothing only on companies that seem to have "&" in the name. (Two companies still work with "&" in the name)
List of non-functioning Companies:
-Anthem
-Bogart
-Burnham
-Church Insurance
-Fawcett
-JRM
-Kenneth B. Brown
-Newton
-Sam F.
-Sherrill
-Wallace & Turner
PHP Company select code: (The if/else statement in the companies field would ideally be if ($row['Company'] !== NULL) {
echo '<option value="'.$row['Company'].'">'.$row['Company'].'</option>';
} but for some reason it will echo in a blank space in the option dropdown.)
<label for="company">Company</label><br />
<select id="company" name="users" onChange="showUser(this.value)">
<?php include 'login.php';
$result = mysqli_query($con, "SELECT DISTINCT Company FROM `roster` ORDER BY Company ASC;");
echo '<option value="">' . 'Select a Company' .'</option>';
while ($row = mysqli_fetch_array($result)) {
if ($row['Company'] == NULL) {
}
else {
echo '<option value="'.$row['Company'].'">'.$row['Company'].'</option>';
}
}
?>
</select>
PHP for other select fields:
<label for="last">Last Name</label><br />
<select id="last" name="Last_Name" onChange="showUser(this.value)">
<?php include 'login.php';
$result = mysqli_query($con, "SELECT DISTINCT Last_Name FROM `roster` ORDER BY Last_Name ASC;");
echo '<option value="">' . 'Select an Agent' .'</option>';
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['Last_Name'].'">'.$row['Last_Name'].'</option>';
}
?>
</select>
Any Ideas on this one? If you need to see the process.php page which echos in the results then just ask! Thanks a million to anyone who helps out.
How the HTML looks when populated through php:
<select id="company" name="users" onchange="showUser(this.value)">
<option value="">Select a Company</option><option value="A.R.T. Group">A.R.T. Group</option><option value="ALPHA Benefits">ALPHA Benefits</option>
</select>
I only included a few since 80ish of them is one massive line of html.
You need to urlencode the parameter that is being passed via ajax. The ampersand is telling PHP that $_GET['q'] is complete and a new $_GET variable is starting.
Notice the %26 for the ampersand returns the desired result.
http://healthbenefitsohio.com/process.php?q=Anthem%20Blue%20Cross%20%26%20Blue%20Shield
Your company options should look like this:
echo '<option value="'.urlencode($row['Company']).'">'.$row['Company'].'</option>';
For good measure, I would also encode the display
echo '<option value="'.urlencode($row['Company']).'">'.htmlspecialchars($row['Company'],ENT_QUOTES).'</option>';
How to display ticked/checked data in a JQuery Dropdown list.
Basically my problem is that i want to display back checked value in a jquery dropdown list which is store in the database.
i tried using attr selected in my javascript,something like this.
<script type="text/javascript">
$(document).ready(function() {
$("#<?php echo $bnum; ?>").dropdownchecklist({width: 300});
//the option value suppose to be dynamic,but for the purpose of this question,
the value will be someothing like this.
$("#<?php echo $bnum; ?> option[value='2']").attr("selected", true);
});
</script>
Value for the checked box that i retrive from a sql procedure is something like this:
For room 501: 2
What i currenty trying to do, is when a user clicked on the page. The dropdown checklist is checked with the value retrived from the database
Below this is the code for the rows of room which contains a Jquery for each of the row
<select id="<?php print $bnum; ?>" multiple="multiple" name="status[]" >
<option value="0"></option>
<?php
//query drop-down list
$sqlakh = "select diet_id,diet_name from bmsdb.diettypes";
$resultakh = mysql_query($sqlakh);
while ($rowsakh = mysql_fetch_array($resultakh)) {
?>
<option value='<?php echo $rowsakh['diet_id'] . '|' . $bnum; ?>' "selected" : ''>
<?php echo $rowsakh['diet_name']; ?></option>
<?php } ?>
</select>
Thanks in advance, im new in javascript. Can someone guide me on how display back the checked item in the Jquery dropdown list which retrived from the database.
you can refer to this question: jQuery - setting the selected value of a select control via its text description
But if you use php to build the option html, why not set the selected option with php.
Something like this in your loop (just an example!)
<?php
$sel = '';
if($rowsakh['diet_id'] == $_POST['someVarName'])
{
$sel = 'selected';
}
echo ' <option value="' . $rowsakh['diet_id'] . '|' . $bnum . '"' . $sel . '>Audi</option>';
?>
I personnaly wouldn't use echo statements in the while loop. I would concatenate it as a long string and finally echo once at the end.
I created a drop down list in PHP and it's selecting the last item of the list and I can't figure out why it's doing this.
When I echo the event_id (the value that is returned), it's the last item every time. The list is populated correctly.
$forms = mysql_query("select events.event_title, events.event_id, saved_forms.id from events
INNER JOIN saved_forms on saved_forms.id = events.event_id
where saved_forms.form_type = 'e' and events.event_by = '$my_username'");
while($form = mysql_fetch_array($forms)){
$form_id = $form['event_id'];
$selection.="<OPTION VALUE=\"$form_id\">".$form['event_title']."</OPTION>";
}
?>
<div id="saved_forms">
<tr><td><select name ="saved_form" value ="<? echo $form_id; ?>" onchange="showUser(<? echo $form_id; ?>)">
<Option value="$form_id"><? echo $selection; ?></Select></td><td>Select Existing Form</td></tr>
</div>
Your problem is here
<select name="saved_form" value="<? echo $form_id; ?>" onchange="showUser(<? echo $form_id; ?>)">
By giving the select tag a value of $form_id you have a high chance of overriding the value set in the options. And you shouldn't hardcode the value into the onchange event. Try removing the value from select and let the option tags do the work.
<select name="saved_form" onchange="showUser(this.value)">