Multiple Ajax Live Search

Sir,
I am using these codes.

[php]
[/php]

And ajax_find.php has these codes

[php]<?php
require_once(“connect.php”);
if (isset($_GET[‘id’]))
{
$sno =trim($_GET[‘id’]);
$record_check ="SELECT * FROM contacts WHERE moba = ‘$sno’ ";
$result=mysqli_query($con, $record_check);
$row = mysqli_fetch_array($result);
echo (!$row) ? “” : $row[‘name’];
echo (!$row) ? “” : $row[‘mobb’];
echo (!$row) ? “” : $row[‘email’];
}
?>
[/php]

It shows data as
[email protected]
(3 fields data in FIRST textbox only)

But I want to display data in 3 textbox which has id’s as

$(’#my_name’).val(data);
$(’#mobb’).val(data);
$(’#email’).val(data);

It must display data as

my_name=Eric
mob=00924587582
email= [email protected]

How to modify both files?

PHP file:
Your SQL is vulnerable to sql injection, change to using parameterized queries.

Instead of echoing directly I would add the values to an array and echo it JSON encoded. JSON is very easy to convert to JavaScript objects, so it’s an easy way to transfer data from server side to the front end.

[php]$user = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo json_encode($user);[/php]

This should echo something like this:

{"name":"JimL","mobb":"123456789","email":"[email protected]"}

Or pretty printed:

[php]{
“name”: “JimL”,
“mobb”: “123456789”,
“email”: “[email protected]
}[/php]

[hr]

JS file:
Jquery should hopefully parse it automatically as JSON, if not you can always force it setting the datatype in the request.

[php] $.get( “ajax_find.php”, { id: formVal } ) .done(function( user )
{
$(’#my_name’).val(user.name);
$(’#mobb’).val(user.mobb);
$(’#email’).val(user.email);
});[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service