...

View Full Version : How To Retrieve PHP Variable



harlequin2k5
09-05-2008, 03:31 PM
I'm using an external javascript file to pop updates on my php page

there is a variable on the php page that I need to pass back into the javascript page ($number_fee) - I understand that if I use something like



<script type="text/javascript">
var numberingFee = "<?php echo $number_fee; ?>";
</script>


my first question is where do I place this in my php page?

my second question is how does the external js page then get the value for var?

php code:


<?php
$conn=doconnect();
$order_query="SELECT * FROM products WHERE productid='" . $_GET['productid'] . "'";
$order_result=mysql_query($order_query) or die (mysql_error());
$order_row = mysql_fetch_array($order_result);

$order_cat_query="SELECT * FROM categories WHERE id='" . $order_row['categoryid'] . "'";
$order_cat_result=mysql_query($order_cat_query) or die (mysql_error());
$order_cat_row=mysql_fetch_array($order_cat_result);
$retail_price=$order_row['price']*$m;
echo '<p>Product: <span style="color:#3333ff;font-weight:bold;">' . $order_cat_row['name'] . '</span></p>';
echo '<p>No. of Parts: <span style="color:#3333ff;font-weight:bold;">' . $order_row['shortdesc'] . '</span></p>';
echo '<p>Size: <span style="color:#3333ff;font-weight:bold;">' . $order_row['name'] . '</span></p>';
echo '<p>Quantity: <span style="color:#3333ff;font-weight:bold;">' . $order_row['quantity'] . '</span></p>';
echo '<p>Price: <span style="color:#3333ff;font-weight:bold;">$' . number_format($retail_price,2) . '</span></p>';

echo '<form action="process_order.php" method="post">';
if ($order_row['categoryid'] == "5")
{
echo '<hr />';
echo '<h3>Additional Options</h3>';
echo '<h4>Ink Options</h4>';
echo '<p>Forms are printed with Black Ink standard at No Charge. An $8.00 per job fee is added to replace Black Ink with any of the following colors:</p>';
echo '<p>Select Ink (Default is Black): ';
echo '<select id="inkSelect" name="inkSelect" onchange="changeInk();">';
echo '<option id="black_ink" name="1" value="Black Ink">Black Ink</option>';
echo '<option id="process_blue" name="2" value="Process Blue">Process Blue</option>';
echo '<option id="reflex_blue" name="3" value="Reflex Blue">Reflex Blue</option>';
echo '</select>';
echo '</p>';
echo '<div id="inkChanged"></div>';


echo '<h4>Numbering Options</h4>';


$order_quantity=$order_row['quantity'];
if ($order_quantity == "500")
{
$number_fee = "$9.00";
}
else if ($order_quantity == "1000")
{
$number_fee = "$12.00";
}
else if ($order_quantity == "2500")
{
$number_fee = "$26.00";
}
else if ($order_quantity == "5000")
{
$number_fee = "$51.00";
}
else if ($order_quantity == "1000")
{
$number_fee = "$96.00";
}
echo '<p>Form numbering is available and printed with Red Ink standard. Black Ink may be substituted for an additional $7.50. Numbering is available for the following fees:</p>';
echo '<table style="width:25%;text-align:left;" cellspacing="0" cellpadding="5">';
echo '<tr><td>0-500 (Add $9.00)</td></tr>';
echo '<tr><td>1,000 (Add $12.00)</td></tr>';
echo '<tr><td>2,500 (Add $26.00)</td></tr>';
echo '<tr><td>5,000 (Add $51.00)</td></tr>';
echo '<tr><td>10,000 (Add $96.00)</td></tr>';
echo '</table>';
echo '<p>';
echo 'Numbering <input type="checkbox" id="numberSelect" name="numberSelect" value="Numbering" onchange="numberBy();" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div id="numberSelected"></div>';
echo '</p>';
echo '<div id="numberStart"></div>';

echo '<h4>Drilling</h4>';



}
echo '</form>';
?>


js function that would use var numberingFee:


function numberBy()
{
var select = document.getElementById("numberSelect");

if (select.checked == "1")
{
document.getElementById("numberStart").innerHTML="<p>Starting number required: <input type=\"text\" name=\"starting_number\" id=\"starting_number\" size=\"20\" /><\/p><p>Blank Ink Substitute <input type=\"checkbox\" id=\"numberBlack\" name=\"numberBlack\" value=\"Black Numbering\" onchange=\"blackSelect();\" \/><div id=\"blackInk\"><\/div>";
document.getElementById("numberSelected").innerHTML="<p style=\"font-weight:bold;color:#ff0000;\">Numbering Selected - Add ==a fee will go here==<\/p>";
}

else
{
document.getElementById("numberStart").innerHTML="";
document.getElementById("numberSelected").innerHTML="";
}
}


any help would be greatly appreciated

Kor
09-05-2008, 03:43 PM
my first question is where do I place this in my php page?


Wherever you want. Better in the head of your document. Make sure that your function is not triggered before that code line (with your variable) will be loaded.


my second question is how does the external js page then get the value for var?

that variable is a global, then use it as a global:



function numberBy()
{
alert(numberingFee);
...
...
}

harlequin2k5
09-05-2008, 03:57 PM
thank you for such a quick reply!

I placed the <script> up in the <head> of the php page and added numberingFee to .innerHTML and I still get nothing...actually I get "numberingFee is not defined"

and is there any reason why this works just fine in ff (albeit the proper variable isn't filling yet) and not in ie?



function numberBy()
{
var select = document.getElementById("numberSelect");

if (select.checked == "1")
{
document.getElementById("numberStart").innerHTML="<p>Starting number required: <input type=\"text\" name=\"starting_number\" id=\"starting_number\" size=\"20\" /><\/p><p>Blank Ink Substitute <input type=\"checkbox\" id=\"numberBlack\" name=\"numberBlack\" value=\"Black Numbering\" onchange=\"blackSelect();\" \/><div id=\"blackInk\"><\/div>";
document.getElementById("numberSelected").innerHTML="<p style=\"font-weight:bold;color:#ff0000;\">Numbering Selected - Add " + numberingFee + "<\/p>";
}

else
{
document.getElementById("numberStart").innerHTML="";
document.getElementById("numberSelected").innerHTML="";
}
}

Kor
09-05-2008, 04:25 PM
Try to place the script code echoed below the lines where the php variable was defined.



else if ($order_quantity == "1000")
{
$number_fee = "$96.00";
}
echo '<script type="text/javascript">var numberingFee = "'.$number_fee.'"</script>';

harlequin2k5
09-05-2008, 04:55 PM
it's all so clear to me now!! :thumbsup:

works perfectly in ff but not in ie - is it because I'm trying to create another function call within the .innerHTML ?

Kor
09-05-2008, 05:08 PM
works perfectly in ff but not in ie - is it because I'm trying to create another function call within the .innerHTML ?
Probably. innerHTML is not a standard DOM method, and it does not insert always the elements as belonging to the DOM tree. Better use DOM methods (createElement(), setAttribute(), appendChild()... and so on) to create and append new HTML elements along with their attributes into the document.

As a note... IE acts otherwise even with DOM in somecases. For instance, if you need to create and attach a new event, you should better use the classical javascript:

element.onchange=function(){blackSelect()}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum