Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

How to display values returned by JSON

Codes here !

Moderators: macek, egami, gesf

How to display values returned by JSON

Postby jagjittcl » Thu Jul 04, 2013 7:41 am

Hi

I have written this . I want to display the values of firstname,lastname in HTML form

$.ajax({
url: 'getdata.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var id = data[1]; //get id
var vname = data[2]; //get name
//alert (data[0]);

}
});

GetData.Php

<?php
include("db.php");
$query = "select firstname,middlename FROM emp_master WHERE id = 1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$temp = array();
$temp['firstname'] = $row['firstname'];
$temp['middlename'] = $row['middlename'];
echo json_encode($temp);
?>

HTML Form
<div class="myclassname" id = "Update">
<form method="post" action="getdata.php">
<input type="hidden" name="id" value="" />

<strong>First Name: *</strong> <input type="text" name="firstname" id="firstname"
/><br/>
<strong>Middle Name: *</strong> <input type="text" name="middlename" id="middlename"
/> <br/>

<p>* required</p>
<input type="button" name="submit" id="submit" value="Submit" />
</div>
jagjittcl
New php-forum User
New php-forum User
 
Posts: 9
Joined: Wed Jul 03, 2013 10:14 pm

Re: How to display values returned by JSON

Postby johnj » Thu Jul 04, 2013 8:31 pm

I did not understand what you are trying to do as the script looks incomplete.

Usually json encoded values have to be passed through json_decode to decode it. Take a look at the manual.
http://php.net/manual/en/function.json-decode.php
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.