User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

javascript and a MySQL query

  #1  
Apr 15th, 2008
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:
$query4=mysql_query("select price from pricelist where item='$result3[item]'");
$result4=mysql_query($query4);
into a variable:
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: javascript and a MySQL query

  #2  
Apr 15th, 2008
C'mon guys, there's gotta be a way!
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: javascript and a MySQL query

  #3  
Apr 16th, 2008
First of all, this isn't the solution. I saw some errors in the script, so, here they are.
$query4=mysql_query("select price from pricelist where item='$result3[item]'");
$result4=mysql_query($query4);
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, $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'];?>;
is wrong. You have to echo the value to assign it to a variable. For example,
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*
Reply With Quote  
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: javascript and a MySQL query

  #4  
Apr 16th, 2008
Ok, cheers.

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!
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: javascript and a MySQL query

  #5  
Apr 16th, 2008
Try total.value = '<?php echo $result4['price']; ?>';
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*
Reply With Quote  
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: javascript and a MySQL query

  #6  
Apr 16th, 2008
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:
$query4=mysql_query("select id, price from pricelist where id='$result3[id]'");
$result4=mysql_query($query4);
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:
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.
Reply With Quote  
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: javascript and a MySQL query

  #7  
Apr 16th, 2008
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:
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?
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: javascript and a MySQL query

  #8  
Apr 17th, 2008
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*
Reply With Quote  
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: javascript and a MySQL query

  #9  
Apr 17th, 2008
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.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: javascript and a MySQL query

  #10  
Apr 17th, 2008
You can't use javascript variable in php script. But you can use php variable in javascript. ie.,
  1. z="<?php echo '100'; ?>";
  2. alert(z);
is possible. But this wouldn't work.
  1. var z;
  2. z="<?php echo '100'; ?>";
  3. <?php
  4. echo $z;
  5. ?>

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.
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function calculate(amount) {
  5. var quantity ;
  6. var price;
  7. var total;
  8. quantity = document.getElementById('quantity').value;
  9. price = document.getElementById('article').value;
  10. total = quantity * price;
  11. document.getElementById('total').value = total;
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. <form name="form" method="post">
  17. Enter quantity: <input type="text" name="quantity" id="quantity"><br />
  18. Select an article: <select name="article" id="article" onchange="javascript: calculate(this.value);">
  19. <option value=''>Select one</option>
  20. <?php
  21. mysql_connect('localhost','root');
  22. mysql_select_db('test');
  23. $q = "select amount from orders";
  24. $r = mysql_query($q);
  25. while($row = mysql_fetch_array($r)) {
  26. echo "<option value=".$row['amount'].">".$row['amount']."</option>";
  27. }
  28. ?>
  29. </select><br />
  30. Your total : <input type="text" id="total" name="total">
  31. </form>
  32. </body>
  33. </html>
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.
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*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 1:53 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC