susan
susan

Reputation: 1

show value in the texbox using ajax - php

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        function getvalues() {
            var selname = $("input[name='names']:text").val();
            $.ajax({
                url: "process.php",
                data: {
                    "selname": selname
                },
                type: 'post',
                dataType: "json",
                success: function (output) {
                    console.log(output);
                    $("#aic").val(output[0]);
                    $("#batchcode").val(output[1]);
                }
            });
        }
    </script>
    </script>
</head>

<body>
    <form method="post">
        <input type="text" name="names" id="query" onblur="getvalues()" />
        <input type="text" name="aic" id="aic" />
        <textarea name="batchcode" id="batchcode" rows="4" cols="50"></textarea>
    </form>
</body>

</html>

process.php

<?php

if(isset($_POST['selname']))
{
    $response = array('0001', 'short info : john 29 years old - technician  '); // add return data to an array

    echo json_encode($response); // json encode that array

    exit;
}

?>

when I type something on input type="text" name="names" I get on input text "aic" >>> 1; but in the textarea I didn't get "short info : john 29 years old - technician ", please tell me where is the mistake!!

Upvotes: 0

Views: 134

Answers (2)

siraj pathan
siraj pathan

Reputation: 1495

try this

function getvalues() {
            var selname = $("input[name='names']:text").val();
            $.ajax({
                url: "process.php",
                data: {
                    "selname": selname
                },
                type: 'post',
                dataType: "json",
                success: function (output) {
var output = jQuery.parseJSON( output );
                    console.log(output);//use output.property
                    $("#aic").val(output[0]);
                    $("#batchcode").val(output[1]);
                }
            });
        }

Upvotes: 1

Shijin TR
Shijin TR

Reputation: 7768

Convert json to array

    var array = JSON.parse(output)
    $("#aic").val(array[0]);
    $("#batchcode").val(array[1]);

Upvotes: 1

Related Questions