Winston E
Winston E

Reputation: 13

Cant get PHP value in jquery

my Php code;

$sql = "SELECT * FROM login_speak";
if ($result = mysqli_query($con, $sql))
{
    $resultArray = array();
    $tempArray = array();

    while($row = $result->fetch_assoc())
    {
               $tempArray[] = $row;
    }            
$resultArray = json_encode($tempArray);
echo $resultArray; 

}
mysqli_close($con);
?>

my jquery code;

 $.getJSON("url.php", function(data){
           $("ul").empty();
           $.each(data.result, function(){
              $("ul").append("<li>Name:"+this['Name']+"</li>");
              alert("kj");
           });    
    });

I have done several things ,But nothing works.I php is correct and I have problem in script ,I guess. Please give me the answer ,What wrong is done. Thanks

Upvotes: 2

Views: 98

Answers (3)

moe
moe

Reputation: 735

Heres what I would do.

 $.ajax({
    url: "url.php",
    type: "GET",
    dataType: 'json'
}).done(function (data) {
    $.each(codes, function(key,value){
             $("ul").append('<option value="' + key + '">' + key + ' - ' + value + '</option>');
    });
}).fail(function (jqXHR, textStatus, error) {
    alert("error message");
    console.log("error message: " + error);
});

Upvotes: 0

James Bailey
James Bailey

Reputation: 508

json_encode from php can be accessed easily in a Javascript varible There are many ways of doing this, however consider the simple way first if it works with the array you are sending. in your example - assuming jquery:

  var valu =  [];
    $.ajax(url: "url.php",
           success: function(data){
                  valu=data;
           });    

see: http://www.dyn-web.com/tutorials/php-js/json/array.php

works for numeric and associative arrays.

Upvotes: 2

sharma sk
sharma sk

Reputation: 651

I think you want change your code to something like this..

$.each(data.result, function(index,value){
              $("ul").append("<li>Name:"+value.name+"</li>");
              alert("kj");
           });  

Upvotes: 0

Related Questions