I have a javascript function setup, which calls a PHP query which returns information based on what object value is going into the javascript function. I would like to define a variable inside of this function based off what the PHP query pulls. This works to some degree because the source code shows everything as it should... but it seems that global variables cannot be defined inside of a function, because outside of the function it is undefined.
Is there any way I can get the a_cost variable out of the disp_info() function and into the multi() function? What happens is when the user selects an option on a form, it calls the disp_info() function and runs the PHP script to pull information from the database and display it dynamically, which is where a_cost becomes defined, and can be re-defined is the function gets called again.
PHP:
<script language="javascript" type="text/javascript">
function disp_info(obj) {
<?php
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {
$part = $row['name'];
$description = $row['description'];
$price = $row['price'];
$info = "<div><h2>$brand Description</h2>$description</div>";
echo "
if (obj.value == \"$part\") {";
if (isset($_SESSION['d_part3'])) {
$new_cost = $current_cost + $price - $cost[3];
} else {
$new_cost = $current_cost + $price;
}
//Below is variable a_cost I want to turn into a global
echo "
var a_cost = \"$new_cost\";";
echo "
}
document.getElementById('proc_info').innerHTML = \"$info\"
document.getElementById('new_cost').innerHTML = \"Current Cost: <b style='color:#00ff00;'>$$current_cost</b><br />Cost with Selection: <b style='color:#00ff00;'>$$new_cost</b>\";";
}
?>
}
function multi(obj, a_cost) {
if (obj.value == 2) {
mult_cost = a_cost * obj.value;
alert("Obj value is " + a_cost + " test"); /*Shows undefined */
} else {
mult_cost = a_cost;
}
document.getElementById('proc_info').innerHTML = "<?php echo $info; ?>";
document.getElementById('new_cost').innerHTML = "Current Cost: <b style='color:#00ff00;'>$<?php echo $current_cost; ?></b><br />Cost with Selection: <b style='color:#00ff00;'>" + mult_cost + "</b>";
}
</script>
Is there any way I can get the a_cost variable out of the disp_info() function and into the multi() function? What happens is when the user selects an option on a form, it calls the disp_info() function and runs the PHP script to pull information from the database and display it dynamically, which is where a_cost becomes defined, and can be re-defined is the function gets called again.








