2
Contributors
5
Replies
24
Views
3 Years
Discussion Span
Last Post by diafol
0

If you want to pass data from php to js, then you can use either xml or json. json is easy as jquery ajax has a shortcut method $.getJSON.

The nuts and bolts involved storing the data from php into a keyed array and encoding that as json before echoing, e.g.

echo json_encode(array("my_name"=>$myname,"mobb"=>$mobb,"email"=>$email));

That's all that's required in the php file.

Your jquery...

$.getJSON( "ajax_find.php", { id: formVal } ) .done(function( data ) 
{
     $('#my_name').val(data.my_name);
     $('#mobb').val(data.mobb);
     $('#email').val(data.email);
});
0

Sir, to understand JSON I wrote some codes and applied your above codes but it does not work, may there is some little mistake. please help again like past

json_dialfol.php

<body>
        <div id="mypopup">
            <div id="header">Search Data</div>
                <div style="margin-top:80px;">
                    <form name="form1" action="" method="post">
                        <table border=0; cellpadding="1" cellspacing="1" bgcolor="#CCFFFF" align="center" >
                            <tr>
                                <td>Code</td>
                                <td width="50px"><input type="text" name="txtsno" id="txtsno" value="" title="Enter product code" /></td>
                            </tr>
                            <tr>
                                <td>Product</td>
                                <td><input type="text" name="txtpro" id="txtpro" value="" title="Enter product name" ></td>
                            </tr>

                                                        <tr>
                                <td>Weight</td>
                                <td><input type="text" name="txtwet" id="txtwet" value="" title="Enter product name" ></td>
                            </tr>


                        </table>
                         <input type="reset" name="button2" value="Clear" >
                    </form>
                </div>
            </div>

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$("#txtsno").change(function() {
var formVal = $("#txtsno").val();
$.getJSON( "json_dialfol2.php", { id: formVal } ) .done(function( data ) 
{
     $('#txtpro').val(data.packing);
     $('#txtwet').val(data.weight);
});
});
</script>
</body>

json_dialfol2.php

<?php
    require_once("connect.php");
    if (isset($_GET['id']))
    {
        $sno =$_GET['id']; 
        $query ="SELECT * FROM test WHERE sno = '$sno' ";
        $result=mysqli_query($con, $query);
        $row = mysqli_fetch_array($result); 
        echo json_encode(array("txtpro"=>$packing,"txtwet"=>$weight));
    }
?>
0
$row = mysqli_fetch_array($result); 
echo json_encode(array("txtpro"=>$packing,"txtwet"=>$weight));

Where are $packing and $weight set? I'm assuming that you mean:

$row = mysqli_fetch_array($result); 
echo json_encode(array("txtpro"=>$result['packing'],"txtwet"=>$result['weight']));
0

sir there are only 3 textboxes in form as
txtsno,txtpro,txtwet

I am sending id from txtsno

so this line is not working still

echo json_encode(array("txtpro"=>$result['packing'],"txtwet"=>$result['weight']));

I could not understand what is wrong now?

0

Ok to get to the bottom of this...

$.getJSON( "json_dialfol2.php", { id: formVal } ) .done(function( data ) 
{
     alert(JSON.stringify(data));
});

That'll give you the output of the php file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.