www.webdeveloper.com
Results 1 to 4 of 4

Thread: Writing javascript inside PHP Echo

  1. #1
    Join Date
    Jun 2014
    Posts
    6

    Writing javascript inside PHP Echo

    Hello,
    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:

    Code:
    <form id="myForm" action="" method="post">
    
    <div>
    <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>
     </select>
    </h4>
    </div>
    
    <div>
    <h4 id="price">Price: $</h4>
    </div>
    
    </form>

    Here's my PHP Code:


    PHP Code:

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

                         
    $num_of_rows=mysqli_num_rows($result_quantity);

                         
    $count=0;
                         
    echo 
    '<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">';

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

                         echo 
    '</select></p>';

                         
    //echo '</p>';

                         
    $count=0;

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

                         
    $num_of_rows_2=mysqli_num_rows($result_price);

                         echo 
    ' <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
    Location
    Ankh-Morpork
    Posts
    19,176
    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;" 
    onchange="document.getElementById(\'price\').innerHTML=\'$\'+this.value">'

    "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

    eBookworm.us

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

  4. #4
    Join Date
    May 2014
    Posts
    683
    Assuming your echo uses single quotes (hence your ability to use doubles) you have to escape your single quotes inside it.

    Example:
    Code:
    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:
    Code:
    <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)
    Code:
    (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) {
    		flush(e);
    		nodeAdd(e, data);
    	}
    
    	/* the meat and bones */
    
    	var
    		products = d.getElementById('products'),
    		price = d.getElementById('price');
    	if (products) addEvent(products, 'change', function(e) {
    		nodeReplace(price, products.options[products.value].value);
    	});
    
    })(document);
    /* 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:

    Code:
    (function() {
    	var
    		products = _d.get('#products'),
    		price = _d.get('#price');
    	if (products) products.addHandler('change', function(e, t) {
    		price.nodeReplace(products.options[products.value].value);
    	});
    })();
    Hope this helps. Any questions fire away.
    Java is to JavaScript as Ham is to Hamburger.

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