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\">£$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\">£$ticketPrice</td>\n";
to:
echo "<td id=\"ticketprice\">£$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?
Related
I tried to write codes which display value in textbox based on value selected in combobox, but I have one problem I puted combobox and texbox in loop but only first row can display value of selected item in combobox where I want each row to display value based on value selected in its combobox
Below are my codes
<?php
require_once('connection.php');
for($counter=0;$counter<=1;$counter++)
{
$queItem= mysqli_query($connect,"select * from oitm");
echo"<select name=\"segment_id\" class=\"form-control\" id=\"segment_id\" STYLE=\"width: 300px\" >";
echo"<option value=\"\" selected=\"selected\">Select a product</option>";
while($rowStockIn = mysqli_fetch_array($queItem))
{
$ItemCode2=$rowStockIn['ItemCode'];
$ItemName2=$rowStockIn['ItemName'];
$ItemPrice2=$rowStockIn['ItemPrice'];
echo "<option value=\"$ItemCode2\" data-itemprice = \"$ItemPrice2\">
$ItemCode2 $ItemName2";
}
echo"</select>";
?>
</span>
<?php
echo"<input id=\"title\" name=\"title\" type=\"text\" value=\"$ItemPrice2\"
><br/>";
}
?>
<script>
$(document).ready(function(){
$("#segment_id").on('change', function () {
var id = $(this).val();
var itemprice = $('option:selected',this).data('itemprice');
$("#title").val(itemprice);
});
});
</script>
I think I have to edit jQuery in order to allow each row to work
Please anyone can help me
Ok, well, if you are using a combo box then clearly the user should be able to select more than one option. Your first problem is that $itemCode2 in your while loop is just a variable not an array. Therefore, as you described in your issue you'll only ever get one value because it will be overwritten each time in the loop.
Make $itemCode2 an array - for example:
$itemCode2[] = $rowStockin['itemCode']
Then you should be able to output the various options that the user can select.
Also, be careful using select * in your queries. Try to be specific in your column selections. select * could leave your script exposed to a sql injection attack.
I have a bootstrap table which has a form in it, with each row containing a column where the value can be changed from a drop-down box. On clicking the 'Save changes' button, all the rows will be updated with the new values.
The form/table works as intended in normal cases. But if I use the search functionality of the bootstrap table to filter out a few of the rows and then try to update rows with the values, the wrong rows are getting affected.
So from the example in the above image, if I filter out to view just the second row like in the picture below, then the changes, or the 'Update' query is executed on the actual first row, that is to the row which had 'Bob' as the 'technician'.
I'd like to know how to solve this issue.
Here is the relevant code:
foreach($tickets as $tickets)
{
$users = $app['database']->selectAll('users');
echo ("<input type='text' style = 'display:none' value = '$tickets->id' name = 'ticketid[]'>");
echo "<td class = '$technician->color'>$technician->name</td>";
echo "<td>";
echo '<select name = "user[]" id="user" class="form-control">';
echo "<option value = $technician->id>$technician->name</option>";
foreach($users as $users)
{
echo "<option value = $users->id>$users->name</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
}
For the database part, I am calling a function transferTask which accepts first the table name, then two arrays, one array containing updated usernames from the dropdown fields and one containing corresponding ids.
transferTask('tickets', $user[$i], $ticketid[$i])
The above function is executed for each row in the table. I think it's an issue wit the name array being passed from the form to this function but I'm not sure. Help is appreciated!
Its a bit hard to express what I want to do but let me give it a shot.
First of all I have a database with a table. One of the columns from that table I will be showing in a select dropdown menu in HTML, to be more precise: this outputs the column 'name' in the dropbown menu:
<?php
$query = mysql_query("SELECT name from table");
echo "<select name='select1'>";
while ($row = mysql_fetch_array($query))
{
echo "<option value'" . $row['name'] ."'>" . $row['name] . "</option>;
}
echo "</select>";
?>
Then when a value is selected in that dropdown, that value needs to be saved in a variable.
if (isset($_POST['button']))
{
$select = $_POST['select1'];
}
As last bit, the value in variable $select needs to be used to make a SELECT query. I want to use that variable in the SELECT statement to look for the corresponding row (in the same table) that is related to the value in the dropdown menu and pick a column and output that value to a textbox or checkbox, depending on as what the value needs to be outputted.
Example:
TABLE
id - name - street - number - ...
row 1: 0 - test1 - teststreet1 - 1 - ...
row 2: 1 - test2 - teststreet2 - 2 - ...
row 3: 2 - test3 - tesstreett3 - 3 - ...
1) I select test2 from the dropdown menu (dropdown menu is filled with the column name from database)
2) test2 is saved as a variable in $name
3) select query searches for value of street column where $name = test2
SELECT street from table where ?
row 2: 1 - test2 - tesstreett2 - 2 - ...
So I want teststreet2 from street to be outputted in a textbox
<input type='text' value='?'>
I need help with the select query or how to call this.
Thanks in advance and taking time to read this!
Kind Regards
Found it!:
if(isset($_POST['select1']))
{
$select = $_POST['select1'];
$street_select = mysql_query("SELECT street FROM table WHERE name = '" .&select. "'");
$street = mysql_fetch_array($street_select);
$resul_street = $street['street'];
}
Now I can do this for every value in each column that is asked via the dropdown menu!
You can not change a state of any loaded dropdown or text as it is.
You must use jQuery/Ajax here.
When you select on Dropdown value it will than trigger Ajax/jQuery and call another page where your select query will run and return value, which you can display in textbox using jQuery.
<select name = 'select1' onchange='return get_street()'>
<option name='test1'>test1</option>
<input type="text" name ="street" id="street" value="" />
<script>
function get_street()
{
var selected_val = $('option:selected', this).val();
$.ajax({ //ajax call
type: "POST", //method == POST
url: "street.php", //url to be called
data: "name="+selected_val, //data to be send
success: function(data){
$('#street').val(data); // here we will set a value of text box
}
});
}
</script>
And your street.php will look like
<?php
$name = $_POST['name'];
$res = mysql_query("select street from table where name = '".$name."'");
$row = mysql_fetch_assoc($res);
return $row['street']; // this will return a name of street
?>
Few things you need to make sure is
give appropriate path of your file in ajax
give id to your text input and same should be in ajax success
you must connect database in your street.php
<?php
$sql = "SELECT `docid`, `fullname` FROM `doctors` ORDER BY fullname ASC";
$result = $database->query($sql) or die('cannot connect to the database');
while($row = mysql_fetch_array($result))
{
echo"<option value= ".$row['docid'].">".$row['fullname']."</option>";
}
?>
This is my select box which i am populating through datbase table.
$('.sheUpdate').click(function(){
var deptment = $(this).attr('dptname');
var deptid = $(this).attr('dptid');
i want to make this one as SELECTED ITEM ON THE Select box
$("#Deptmentselectbox").prepend('<option value='+deptid+'>'+deptment+'</option>')
});
This is my jquery code. When i click ".sheUpdate" link i have couple of values that i am grabing using attr function. i am grabing depatment name and department value which i want to show as the SELECTED ITEM ON THE SELECT BOX
I think we need to see more code to be able to properly help. What is the html code of the element with the .sheUpdate class?
Does this link element actually have attributes dptname and dptid?
What is the code you use on #Deptmentselectbox?
The code you added is working properly.
just need to set the new added element as selected.
please check this fiddle
Also please let me know what value you get if you write this
alert($(this).attr('dptname'));
alert($(this).attr('dptid'));
$('.sheUpdate').click(function(){
var deptment = $(this).attr('dptname');
var deptid = $(this).attr('dptid');
$("#Deptmentselectbox").prepend('<option value='+deptid+'>'+deptment+'</option>')
$("#Deptmentselectbox").get(0).selectedIndex = 0;
// OR $("#Deptmentselectbox").val(deptid);
});
Assuming that your initial select box is like this:
<select id="Deptmentselectbox">
<option value=''> Select a department </option>
</select>
To remove the placeholder on first option selected:
$('.sheUpdate').click(function(){
var deptment = $(this).attr('dptname');
var deptid = $(this).attr('dptid');
if($("#Deptmentselectbox").val() == '')
$("#Deptmentselectbox").html('<option value='+deptid+'>'+deptment+'</option>');
else
$("#Deptmentselectbox").prepend('<option value='+deptid+'>'+deptment+'</option>');
$("#Deptmentselectbox").val(deptid);
}
Here is a Solution Hope it Helps
$(document).ready(function(){
$("#test1").click(function(){
var data = $("#t").val();
var data1 = $("#r").val();
$("#try").prepend('<option value='+data+' selected="selected">'+data1+'</option>');
});
});
Click Here For Demo
Please help, I have a dynamic JS table, so you can add and delete rows by clicking a button. One field that I need to add within the row is a select box, no problem so for and I use the following:
var cell2 = row.insertCell(1);
var sel = document.createElement('select');
sel.name = 'selRow' + rowCount;
sel.options[0] = new Option('text zero', 'value0');
sel.options[1] = new Option('text one', 'value1');
cell2.appendChild(sel);
So the above creates a select box, named based on the row ID using rowCount. Where I’m stuck is I need to dynamically populate the options, I can do this with php using the following, but it is linking the JS and PHP that I’m stuck on:
$getProd_sql = 'SELECT * FROM products';
$getProd = mysql_query($getProd_sql);
$prov = "<select name=\"product\" id=\"product\" onChange=\"testajaxprod();\">";
$prov .= " <option value=\"0\" selected=\"selected\">Select Product</option>";
while($row = mysql_fetch_array($getProd))
{
$prov .= "<option value=\"$row[product_id]\">$row[product_name]</option>";
}
$prov .= "</select>";
Your help is very much appreciated.
Thanks,
B.
You can add PHP in the middle of your JS. PHP will run first and leave you with a file containing the values as if my magic.
To populate from PHP instead of producing your data in text you can do this.
$getProd_sql = 'SELECT * FROM products';
$getProd = mysql_query($getProd_sql);
while($prod = mysql_fetch_object($getProd))
{
?>
var text = documentElement.createElement('select');
documentElement.value = <?=$prod->value?>
<?
}
Notice the way you can break in and out of PHP with . Also saves you from writing "echo" or anything. You will have to replace the simple text box with your dropdowns or whatever.
Hope this helps