Results 1 to 4 of 4

Thread: Writing javascript inside PHP Echo

  1. #1
    Join Date
    Jun 2014

    Writing javascript inside PHP Echo

    i'm trying to write javascript inside a PHP Echo but it isin't working. Here's the Javascript which i'm trying to embed in PHP:

    <form id="myForm" action="" method="post">
    <h4 >Samsung Option: 
     <select id="products" onchange="document.getElementById('price').innerHTML='$'+this.value">
      <option value=""> Select </option>
      <option value="400"> Galaxy S5 </option>
      <option value="300"> Galaxy S4 </option>
      <option value="200"> Galaxy S3 </option>
      <option value="100"> Galaxy S2 </option>
    <h4 id="price">Price: $</h4>

    Here's my PHP Code:

    PHP Code:

    mysqli_query($con,"SELECT * FROM quantity_price where brand='$brand_name' AND category='$category' AND product_id='$code' ORDER BY quantity");


    '<p style="font-size:0.71em">Available Packaging: <select name="product_qty" style="margin-left:4px; font-size:1.02em;" onchange="document.getElementById("price").innerHTML="$"+this.value">';

    $row_quantity mysqli_fetch_array($result_quantity)) {
    '<option value="'.$row_quantity['price'].'">'.$row_quantity['quantity'].' '.$row_quantity['quantity_unit'].'</option>';


    //echo '</p>';


    $result_price mysqli_query($con,"SELECT * FROM quantity_price where brand='$brand_name' AND category='$category' AND product_id='$code'");


    ' <p style="font-size:0.71em" id = "price">Price: Rs '

    Basically, the values for the select button loads from the Database. What i want is the price of those items to change through javascript.

    Any ideas on how to resolve this? thanks!

  2. #2
    Join Date
    Aug 2004
    May just be a quoting issue:
    PHP Code:
    echo '<p style="font-size:0.71em">Available Packaging: 
    <select name="product_qty" style="margin-left:4px; font-size:1.02em;" 

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

  3. #3
    Join Date
    Jun 2014
    thanks thats working!

  4. #4
    Assuming your echo uses single quotes (hence your ability to use doubles) you have to escape your single quotes inside it.

    echo '
      <select id="products" onchange="document.getElementById(\'price\').innerHTML = \'$\' + this.value">';
    THOUGH, any script WORTH using shouldn't be using the onchange element, it should be attaching it from an external script so that across page-loads it can be cached. I'd also not use innerHTML since it forces a reparse of the whole document which can have undesired side-effects and be slower than using more code (strange as that sounds) to manipulate the DOM instead.

    Though you have one BIG error in there. A select's value is the 'index' of the OPTION inside the select. To get the option's value, you have to go through that selects child OPTION.

    = '$' + this.options[this.value].value;

    Which put altogether the markup would simply be gutted down to:
    <select id="products">
    But the JS gets a bit more complex, but this would run faster and be the 'proper way' -- and despite being more code COULD result in the site using less bandwidth thanks to being able to put this in a file outside the markup where it can be cached. (depends on your traffic and how often you use this)
    (function(d) { /* anonymous function to prevent scope conflicts */
    	/* First some handy functions */
    	function addEvent(e, event, handler) {
    		if (e.addEventListener) e.addEventListener(event, handler, false);
    			else e.attachEvent('on' + event, handler);
    	function flush(e) {
    		while (e.firstChild) e.removeChild(e.firstChild);
    	function nodeAdd(e, data) {
    		e.appendChild(typeof data == 'object' ? data : d.createTextNode(data);
    	function nodeReplace(e, data) {
    		nodeAdd(e, data);
    	/* the meat and bones */
    		products = d.getElementById('products'),
    		price = d.getElementById('price');
    	if (products) addEvent(products, 'change', function(e) {
    		nodeReplace(price, products.options[products.value].value);
    /* I pass document to make the code smaller/faster inside the anon */
    A bit more complex -- I have equivalents to those 'handy functions' in my elementals library, which using elementals would go like this:

    (function() {
    		products = _d.get('#products'),
    		price = _d.get('#price');
    	if (products) products.addHandler('change', function(e, t) {
    Hope this helps. Any questions fire away.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Recent Articles