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.

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 will not hold any value(unless $result4 is an array and price is the key!). And,

total.value = <?$result4;?>;

is wrong. You have to echo the value to assign it to a variable. For example,

var variable1;
variable1 = <?php echo $val; ?>;

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!

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.

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?

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 ?

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.

You can't use javascript variable in php script. But you can use php variable in javascript. ie.,

z="<?php echo '100'; ?>";
	alert(z);

is possible. But this wouldn't work.

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.

<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>

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.

I am not sure, but I think this should work with no problems:

<?php
echo '<script language="Javascript">';
echo "var z1 = $php_var;";//one way
echo 'var z2 = '.$php_var2.';';//another
echo '</script>'
?>

Best regards.

This article has been dead for over six months. Start a new discussion instead.