Searchable Option List (jQuery plugin), get selected value in php - php

I'm using
https://pbauerochse.github.io/searchable-option-list/examples.html plugin in my website for searchable dropdown. its working perfectly but i don't know how to get the value of selected items in PHP?
I try to use below code but it return only last selected value in dropdown:
//echo count($_POST['courses']);
// it return only 1 but I select more than 5 items
if (!empty($_POST['courses'])) {
foreach ($_POST['courses'] as $select) {
echo $coursename .= $select. " ";
// only last selected value is displaying
}
}
any one please help...

Related

Get First Option Text on Button Click Jquery

I am having a select field in which I am trying to achieve something like when the dropdown is selected, datatable will be sorted accordingly. Once the user is done with sorting, There is a button to clear all values of dropdown and get the general value to show all rows without sorting. The values are taken fine except the text of the value remains the same.
//dropdown
<select value='Deliver To' name="_location" id="_location" class="SlectBox form-control">
<?php echo location(); ?>
</select>
//PHP to get results to dropdown
function location(){
global $con;
$output.= '<option value="_allCity">All Results</option>';
$_selectquery= "SELECT * FROM _tableone";
$result = mysqli_query($con, $_selectquery);
while($row = mysqli_fetch_array($result)){
$output.= '<option value = "'.$row["name"].'">'.$row["name"].'</option>';
}
return $output;
}
//Button to reset dropdown
$('#reset').click(function(){
$('#_location').val('_allCity').change();
$('#_location').text('All Results').change();
});
When I reset the value is taken correctly but the text is still the manually selected one.

PHP Edit form for MySQL database <option> value is not correct after a fetch

I have a Edit form in php and everything work great. The only issue i have is when i click edit it returns all the data except in the Select drop down. it does not have the chosen category it always shows the first value in the list. But i then can click on the drop down and choose a new category and it works.
//Query the category table
$resultSet = $con->query("SELECT * FROM schedule_category");
<select id="schedule_category" name="schedule_category" class="custom-select">
<?php
while($rows = mysqli_fetch_assoc($resultSet))
{
?>
<option value = "<?php echo($rows['schedule_category'])?>">
<?php echo($rows['schedule_category']) ?>
</option>
<?php
}
?>
</select>
I would like to have it show the correct select option record not the first one in the drop down list. Here is an image of what happens https://imgur.com/a/XVXQ2Sa
You'll need to have your code compare each to the selected value, and add the appropriate keyword:
$previous_selection = // whatever it is, from your data
while($rows = mysqli_fetch_assoc($resultSet))
{
$thisone = $previous_selection == $rows['schedule_category'] ? " selected " : "";
echo '<option value = "';
echo ($rows['schedule_category']) . '"' . $thisone . '>';
echo($rows['schedule_category']) . '</option>';
}
What you're doing here is comparing your previously-selected value to each row, when it matches, the variable $thisone is set to "selected", otherwise it's empty. You then add that to each option line after the value and before the close-tag for the option, and it will add "selected" when the value matches.
Also I personally don't like switching in and out of PHP for no good reason, makes it really difficult to read, hence I echo the various bits of HTML here.
ETA - actually that could be simplified further, if your selection value is the same as the text displayed in the list, there's no need to actually specify the value in the option tag. That is only required when the value is different to the display, for example if you want the user to see your category names, but you want to submit the category ID.

How can save drop down selected values. dropdown located in table column as shown in the image

I want to save each row by getting the ID of selected value in the dropdown like this:
This is supposed to be a comment, but i have a low reputation here
Hi Khan i do not really understand what you mean.
A dropdown is basically a select option. The attribute "value" is what gets posted as the selected value.
To save the value, simply have this:
$selected_value = mysqli_real_escape_string($connection, $_POST['name_of_dropdown_from_html_form']);
To get the value on an update form, do this:
<select id="id_of_field" name="name_of_field" class="form-control">
<?php
#i assume your select dropdown list is from a database.
#SO in that table, we have two columns, id_of_table and list_type.
#For example a list of different types of cars,
#id(1) and list_type(BMW).
#SO now we check if the id of the car is in the current dropdown list
$list = $class->get_list();
foreach ($list as $key => $value) {
if ($value['id_of_table'] == $value['list_type']) {
$selected = "selected='selected'";
echo "<option value=\"{$value['id_of_table']}\" {$selected}>{$value['list_type']}</option>";
} else {
echo "<option value=\"{$value['id_of_table']}\">{$value['list_type']}</option>";
}
}
?>
</select>
try and integrate this with your project and let me know what you get

Cannot return the values of forms

I am new to php and web programming.
I am programming a website that collects essays. In the submission form, I want the date to be entered by 3 select dropdown forms (day-month-year). I used to add them in a text field and save them as strings in database. I added already hundreds of essays so I am not changing the type of data.
I did the following:
1- I made a dynamic dropdown using php:
function get_dropdown_options( $name, array $options, $selected=null ) {
$dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n";
$selected = $selected;
foreach( $options as $key=>$option )
{
$select = $selected==$key ? ' selected' : null;
$dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
$dropdown .= '</select>'."\n";
return $dropdown; }
2- then I made a function to show three dropdown menus for days, months and years.
function show_date_dropdown_row($name, $label, $option1, $option2, $option3, $value = "") {
echo "<tr class=\"".get_row_bg()."\" valign='top'>\n<td><p class=\"rowtitle\">".$label."</p></td>\n";
echo "<td><p>\n";
echo get_dropdown_options($name1, $option1, $value1).get_dropdown_options($name2, $option2, $value2).get_dropdown_options($name3, $option3, $value3);
return $name = $name1." - ".$name2." - ".$name3;
}
3- in the submission form page, I put the following code:
show_date_dropdown_row("release_date", "Release Date", $days_list, $months_list, $years_list, "");
4- the name "release_date" is then added to the database.
The page shows the three dropdown lists perfectly. But the problem is that the "release_date" in database don't store any value.
I tried the function in step1 and it works perfectly. I know that the problem is in step two, but don't know where.
First your return statement inside show_dropdown_row will not provide you with the date you want. That function will run only once when user request for the page.
So instead of $name1,$name2 and $name3 in your get_dropdown_options function you should put values like. day,month and year as names for your select elements.
When submiting your form at on your php script you should catch those value using post or get.
like.
$dates=$_POST['year']."-".$_POST['month']."-".$_POST['day'];

Getting a value from my sql database in Javascript?

I've been having a bit of trouble with this for a while, please can somebody shed some light onto this?
What's happening so far: When a button is clicked, a search statement occurs and an AJAX call is made and brings back a set of results (displayed as a table) these results coming from a table in mysql database called 'ticket. The values displayed from the ticket table are 'venue' 'date' 'time' 'tPrice'.
This 'tPrice' is a number which has been set i.e. 15/20/25 to represent the price of a ticket. This works and displays fine but I'm having a problem trying to get the value of the price of the ticket in Javascript, does anyone know how to refer to a number in a mysql table in JS?
What I'm wanting to do with this value is multiply it to the value of whatever number is selected from a drop-down menu. (this drop-down menu is also returned by AJAX as part of the results of the user's search, this search result page is written in php and echoes the drop-down menu as a column within the results table)
What I have so far is this:
function quantityChange() {
//Select the value of the drop down list
var quantity = $('.summary').html($('#showQuantity option:selected').val());
//get the value of the number from the tPrice column in 'ticket' table
var ticket = parseFloat($('#ticketprice').val());
//multiply them together
var total = quantity * ticket;
return (total);
After debugging in Firebug, it's giving me no errors but it's also not giving me any results...please, any help would be greatly appreciated! Thanks
EDIT:
The php code:
$postCodeSQL = "SELECT * FROM ticket WHERE locationID IN (SELECT locationID FROM location WHERE postCode LIKE '$pcSearch') ";
$postCoderesult = mysql_query($postCodeSQL) or die(mysql_error());
while ($pcRow = mysql_fetch_assoc($postCoderesult)) {
$venue = $pcRow['venue'];
$ticketPrice = $pcRow['tPrice'];
$date = $pcRow['date'];
$time= $pcRow['time'];
echo "<tr>\n";
echo "<td>$venue</td>\n";
echo "<td id=\"ticketprice\">&pound$ticketPrice</td>\n";
echo "<td >
<select name =\"showQuantity\" id=\"showQuantity\" class =\"showQuantity\" onchange=\"quantityChange()\" >
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>\n";
echo "<td>$date</td>\n";
echo "<td>$time</td>\n";
echo "</tr>\n";
}
Try this (change the trigger function)
$(document).ready(function() {
function quantityChange() {
//Select the value of the drop down list
var quantity = $('#showQuantity option:selected').val();
//get the value of the number from the tPrice column in 'ticket' table
var ticket = parseFloat($('#ticketprice').val());
//multiply them together
var total = quantity * ticket;
//console.log(total);
//console.log(ticket);
//console.log(quantity);
return (total);
}
// Need a simple trigger
$('#showQuantity').change(function() {
quantityChange();
});
});
As said , you have more than one element with the same ID.
So when you're writing a code to get the value by elemeny's id = troubles.
Try:
1.Change the next line:
onchange=\"quantityChange()\"
to:
onChange=\"quantityChange(this)\"
2.Change your function defining line to:
function quanitityChange(selElem){
3.change the next line:
var quantity = $('.summary').html($('#showQuantity option:selected').val());
to:
var quantity = parseFloat($(this).find(":selected").val());
4.Change:
echo "<td id=\"ticketprice\">&pound$ticketPrice</td>\n";
to:
echo "<td id=\"ticketprice\">&pound$ticketPrice<input type='hidden' id='ticketPriceHidden' value='".$ticketPrice."'></td>\n";
5.In your function again:
var ticket = parseFloat($(this).parent().find("input#ticketPriceHidden").val());
Works? What the output of quantity and ticket now?

Categories