Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts

    How To Retrieve PHP Variable

    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

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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by harlequin2k5 View Post

    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.
    Quote Originally Posted by harlequin2k5 View Post
    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:
    Code:
    function numberBy()
    {
    alert(numberingFee);
    ...
    ...
    }
    Last edited by Kor; 09-05-2008 at 02:46 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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?

    Code:
    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="";
    		}
      }
    Last edited by harlequin2k5; 09-05-2008 at 03:03 PM.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Try to place the script code echoed below the lines where the php variable was defined.

    PHP Code:
    else if ($order_quantity == "1000")
                        {
                          
    $number_fee "$96.00";
                        } 
    echo 
    '<script type="text/javascript">var numberingFee = "'.$number_fee.'"</script>'
    Last edited by Kor; 09-05-2008 at 03:28 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    it's all so clear to me now!!

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

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by harlequin2k5 View Post
    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()}
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •