Ajax fetch multiple values from php code
Send the single value from index.html file through ajax and get multiple values as a reply from ajax.php file using string concatenation and javascript split() function
...................................................................................................................................................................
index.html
...................................................................................................................................................................
<div class="first"> <label >member</label> <select name='member' id='member' onchange='change(this.value);'> <option value=''>----Select----</option> </select> <label>father</label> <input type="text" name='father' id='father' readonly ></input> <label>last name</label> <input type="text" name='father' id='last_name' readonly ></input> </div>
...................................................................................................................................................................
javascript
...................................................................................................................................................................
<script type="text/javascript"> function change(ele) { var element = ele; var info = "id="+element; $.ajax({ type: "GET", url: "ajax.php", data: info, success: function(result){ var split = result.split('@'); var one = split[0]; var two = split[1]; $("#last_name").attr("value",one); $("#father").attr("value",two); } }); } </script>
...................................................................................................................................................................
ajax.php
...................................................................................................................................................................
<?php include "includes/db_connect.php"; $result=''; $a=''; if($_GET) { if(isset($_GET['id'])) { $id=$_GET['id']; $select = mysql_query("select * from member where id='$id'"); while($row = mysql_fetch_array($select)) { $last_name = $row['last_name']; $a=$last_name; } $select = mysql_query("select * from group where id='$id'"); $row1 = mysql_fetch_array($select); $father= $row1['father']; $b=$father; $result=$a.'@'.$b; echo $result; } ?>
...................................................................................................................................................................
Here at ajax.php file get the id and fetch two different value from two different table and send the two values using string concatication function by spliting the value ,here am using @ to slpit the two values
At the index.html file if ajax success the result value ll get and split the values using the @ symbol and assign the value to the corresponding text boxes.
i hope you understand the tutorial .. :)
0 comments