•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 391,667 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,926 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1155 | Replies: 9
![]() |
•
•
Join Date: Oct 2007
Location: England
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
God i'm having a terrible time trying to do this. Does anyone know how to set a javascript variable as a query result from a MySQL database.
As an example, like this:
Set this query:
into a variable:
i just can't do it. I've tried doing it like this:
But the thing stops my <body onLoad=""> function from working. I'm no expert in Javascript.
Cheers guys.
As an example, like this:
Set this query:
$query4=mysql_query("select price from pricelist where item='$result3[item]'");
$result4=mysql_query($query4);var price
i just can't do it. I've tried doing it like this:
function updateTotalPrice(){ //show the price of the sub category
var amount = document.getElementById('amount');
var total = document.getElementById('totalPrice');
<?
$query4=mysql_query("select price from pricelist where item='$result3[item]'");
$result4=mysql_query($query4);
//echo ="total.value == $result4[price];";
?>
total.value = <?$result4['price'];?>;
}But the thing stops my <body onLoad=""> function from working. I'm no expert in Javascript.
Cheers guys.
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
First of all, this isn't the solution. I saw some errors in the script, so, here they are.
You have already executed the query and assigning the resource_id of the query to $query4. Then, again, you are trying to execute the query,
You have to fetch the row by using mysql_fetch_array or mysql_fetch_row. $result4['price'] will not hold any value(unless $result4 is an array and price is the key!). And, is wrong. You have to echo the value to assign it to a variable. For example,
•
•
•
•
$query4=mysql_query("select price from pricelist where item='$result3[item]'");
$result4=mysql_query($query4);
$result4 = mysql_query($query4); You have to fetch the row by using mysql_fetch_array or mysql_fetch_row. $result4['price'] will not hold any value(unless $result4 is an array and price is the key!). And,
•
•
•
•
total.value = <?$result4['price'];?>;
var variable1; variable1 = <?php echo $val; ?>;
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Oct 2007
Location: England
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
Ok, cheers.
Right i've done this:
But the textbox comes up with "undefined". Is this the right way to do it?
Cheers for all the help!
Right i've done this:
function updateTotalPrice(){ //show the price of the sub category
//var amount = document.getElementById('amount');
var total = document.getElementById('totalPrice');
var price = <? echo $result4['price'];;?>
<?
$query4=mysql_query("select price from pricelist where item='$result3[item]'");
$result4=mysql_fetch_row($query4);
//echo ="total.value = "$result3[price]";";
?>
total.value = price;
}But the textbox comes up with "undefined". Is this the right way to do it?
Cheers for all the help!
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
•
•
Join Date: Oct 2007
Location: England
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
ARRRGHH, i hate this thing. Right I've tried that, but it isn't working.
It's supposed to populate a textbox based on a select box. For example the select box is showing a couple of options, like "computer", "book" etc. How would i get the price of the selected item.
What about getting the ID, which is the primary key, of the selected option. So like this:
Where $result3 is another query which populated the drop down box.
I've been looking at the Firefox extension FireBug, and when i look at the DOM of the drop down box, it has no value? Which i don't understand because i've set it up to give it the value of the ID, like this:
This is the addOption function:
Any ideas? Cheers for all your help man.
It's supposed to populate a textbox based on a select box. For example the select box is showing a couple of options, like "computer", "book" etc. How would i get the price of the selected item.
What about getting the ID, which is the primary key, of the selected option. So like this:
$query4=mysql_query("select id, price from pricelist where id='$result3[id]'");
$result4=mysql_query($query4);I've been looking at the Firefox extension FireBug, and when i look at the DOM of the drop down box, it has no value? Which i don't understand because i've set it up to give it the value of the ID, like this:
addOption(document.getElementById('SubCat'),'$result3[id]', '$result3[item]' + ': £' + '$result3[price]');This is the addOption function:
function addOption(selectbox, value, text ){
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}Any ideas? Cheers for all your help man.
•
•
Join Date: Oct 2007
Location: England
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
Hmmm, i've come up with a solution; instead of giving the select box option's a value of their ID, i have given them a value of their price instead, so to access the price all i need to do is:
This is a bit of a work around, and this might mean that it is going to be much harder to save it to a database at the end. But what do you reckon about this?
function updateTotalPrice(el){ //show the price of the sub category
var amount = document.getElementById('amount').selectedIndex;
var total = document.getElementById('totalPrice');
var price = el.value
<?
//$query4=mysql_query("select * from pricelist where id='$result3[id]'");
//$result4=mysql_query($query4);
//echo ="total.value = "$result3[price]";";
?>
total.value = price * amount;
}This is a bit of a work around, and this might mean that it is going to be much harder to save it to a database at the end. But what do you reckon about this?
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
What exactly are you looking for ? You have a select box with some options. When the user selects an option, you want the value to be populated to a textbox ?
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Oct 2007
Location: England
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
Yea. Sorry i'm not very good at explaining things.
Right, firstly theres a text box in which the user can write the quantity.
Secondly theres a select box, displaying Category, which is populated off a MySQL database.
Thirdly theres another select box which displays all the Sub Categorys that are linked to the Category that has been chosen, this is again populated from a MySQL database.
Fourthly i need a text box that is populated with the price of the Sub Category that has been chosen, multiplied by the figure that is in the quantity text box.
It sounds really easy but im not that immense at Javascript.
Cheers.
Right, firstly theres a text box in which the user can write the quantity.
Secondly theres a select box, displaying Category, which is populated off a MySQL database.
Thirdly theres another select box which displays all the Sub Categorys that are linked to the Category that has been chosen, this is again populated from a MySQL database.
Fourthly i need a text box that is populated with the price of the Sub Category that has been chosen, multiplied by the figure that is in the quantity text box.
It sounds really easy but im not that immense at Javascript.
Cheers.
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
You can't use javascript variable in php script. But you can use php variable in javascript. ie., is possible. But this wouldn't work.
But, if you are passing the price as the value of the select box, then getting it with an onchange event and assigning the calculated value to a textbox shouldn't be difficult.
eg.
Or, if you are passing an id as the value of the select box, you can do it this way. Submit the page using an onchange event. When the page is submitted, the quantity, the id will be posted. With the Id, query the table, get the price, multiply it with quantity and echo the value in total.
php Syntax (Toggle Plain Text)
z="<?php echo '100'; ?>"; alert(z);
php Syntax (Toggle Plain Text)
var z; z="<?php echo '100'; ?>"; <?php echo $z; ?>
But, if you are passing the price as the value of the select box, then getting it with an onchange event and assigning the calculated value to a textbox shouldn't be difficult.
eg.
php Syntax (Toggle Plain Text)
<html> <head> <script type="text/javascript"> function calculate(amount) { var quantity ; var price; var total; quantity = document.getElementById('quantity').value; price = document.getElementById('article').value; total = quantity * price; document.getElementById('total').value = total; } </script> </head> <body> <form name="form" method="post"> Enter quantity: <input type="text" name="quantity" id="quantity"><br /> Select an article: <select name="article" id="article" onchange="javascript: calculate(this.value);"> <option value=''>Select one</option> <?php mysql_connect('localhost','root'); mysql_select_db('test'); $q = "select amount from orders"; $r = mysql_query($q); while($row = mysql_fetch_array($r)) { echo "<option value=".$row['amount'].">".$row['amount']."</option>"; } ?> </select><br /> Your total : <input type="text" id="total" name="total"> </form> </body> </html>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Previous Thread: Scope issue with javascript.
- Next Thread: GetElementsByTagName Problem



Linear Mode