How To Charge Json Information Amongst Jquery, Php Together With Mysql ?

This shipping shows how to populate a pick out box based on the value of the another, past times getting JSON information amongst jQuery from a PHP script that gets the information from a MySQL database.

HTML Code
The initial HTML code looks similar this:
 <form>     Fruit:    <select name="name" id="fruitName">       <option>Apple</option>         <option>Banana</option>         <option>Orange</option>         <option>Pear</option>     </select>     Variety:     <select name="variety" id="fruitVariety">     </select> </form> 
The laid of fruit names was already populated on the server-side as well as the default laid of varieties could endure too; I convey chosen to populate them amongst Javascript inwards this example.

jQuery Code
The jQuery code needs to initially populate the diverseness drib downwardly based on the value of the fruit drib down. It as well as hence needs to update the diverseness if the fruit drib downwardly changes.

Assuming the PHP script to fetch the fruit is at /fruit-varieties.php practice this:
 business office populateFruitVariety() {         $.getJSON('/fruit-varities.php', {fruitName:$('#fruitName').val()}, function(data) {          var pick out = $('#fruitVariety');         var options = select.prop('options');         $('option', select).remove();          $.each(data, function(index, array) {             options[options.length] = novel Option(array['variety']);         });      });  }  $(document).ready(function() {    populateFruitVariety();  $('#fruitName').change(function() {   populateFruitVariety();  });   }); 
The "$.getJSON('/fruit-varities.php', {fruitName:$('#fruitName').val()}" business is what retrieves the information as well as it passes the currently selected fruit mention value every bit component of the kicking the bucket string to the PHP script.

PHP Code
The PHP script connects to the database, retrieves the information for the selected fruit mention as well as and hence returns it every bit a JSON encoded string.
 $dsn = "mysql:host=localhost;dbname=[DATABASE NAME HERE]"; $username = "[USERNAME HERE]"; $password = "[PASSWORD HERE]";  $pdo = novel PDO($dsn, $username, $password);  $rows = array(); if(isset($_GET['fruitName'])) {     $stmt = $pdo->prepare("SELECT diverseness FROM fruit WHERE mention = ? ORDER BY variety");     $stmt->execute(array($_GET['fruitName']));     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); } echo json_encode($rows);  
Downloadable example
Influenza A virus subtype H5N1 downloadable instance tin endure institute here. This contains a PHP file which connects to the database as well as returns information when the fruitName parameter is specified.
All you lot involve to practice is add together inwards your database name, login mention as well as password to the PHP file as well as practice the fruit table. The SQL for doing this is included inwards a comment at the halt of the PHP file.
The HTML file should as well as hence piece of job as-is, every bit long every bit you lot run the whole affair through a spider web server (e.g. don't merely opened upwards the HTML file inwards your spider web browser as well as aspect it to work).

Source: Load JSON information amongst jQuery, PHP as well as MySQL
Sumber http://developer-paradize.blogspot.com

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?