-1

I'm calling a PHP file from jquery ajax javascript. I could able to see the output of the PHP file in browser. But when called via ajax jquery, i'm not able to print he same output. How can i access it ?

**test.php**
<?php
    $dbuser="root";
    $dbname="test";
    $dbpass="root";
    $dbserver="localhost";
    // Make a MySQL Connection
    $con = mysql_connect($dbserver, $dbuser, $dbpass) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());
    // Create a Query
    $sql_query = "SELECT id, login FROM user";
    // Execute query
    $result = mysql_query($sql_query) or die(mysql_error());
    $jsonArray = array();
    while ($row = mysql_fetch_array($result)){
        $jsonArrayItem = array();
        $jsonArrayItem["id"] = $row["id"];
        $jsonArrayItem["login"] = $row["login"];
        array_push($jsonArray, $jsonArrayItem);
    //echo '<option value='. $row['id'] . '>'. $row['login'] . '</option>';
    }
    mysql_close($con);
    $tableData = array(
            "data" => $jsonArray
        );
    header('Content-Type: application/json');
    echo json_encode($tableData,JSON_UNESCAPED_SLASHES);
    die();
  ?>

test.html

<html>
<head>
<title>Example</title>
</head>
<body>
<script  src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript">
$.ajax({
    url: "test.php",
    type: "GET",
    dataType: "json",
    data: values,
    success: function(data){
        window.alert(data);
    },
    error: function(data){
        alert("AJAX error!");
    }
})
</script>
</body>
</html>
<style>
#container {
    text-align: center;
}
a, figure {
    display: inline-block;
}
figcaption {
    margin: 10px 0 0 0;
    font-variant: small-caps;
    font-family: Arial;
    font-weight: bold;
    color: #bb3333;
}
figure {
    padding: 5px;
}
img:hover {
    transform: scale(1.1);
    -ms-transform: scale(1.1);
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -o-transform: scale(1.1);
}
img {
    transition: transform 0.2s;
    -webkit-transition: -webkit-transform 0.2s;
    -moz-transition: -moz-transform 0.2s;
    -o-transition: -o-transform 0.2s;
}
</style>

Is something wrong in syntax ? can someone help here ?

User
  • 93
  • 1
  • 1
  • 9
  • To understand your problem you need to be more specific about what the symptoms are. What does the request show you in the networks panel of the browser debug tools? – Dragony Sep 03 '16 at 20:45
  • 2
    Please stop using the `mysql_*` functions. They were deprecated in PHP 5.5, which is so old it doesn't even receive security updates anymore, and completely removed in PHP 7. See https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – ChrisGPT was on strike Sep 03 '16 at 20:50
  • @Dragony : I don't see any info in debug tools. It's just a blank. Individual PHP script is able to display JSON output, but when called via ajax getting some issues. – User Sep 03 '16 at 21:09
  • Where have you defined `values`? – cube Sep 03 '16 at 21:28
  • 1) place your JavaScript in separate script tag closure, 2) define `values`, 3) missing trailing semi colon after the AJAX call. – Capital C Sep 03 '16 at 21:31

1 Answers1

0

Use for example Chrome debug console. Network section, XHR filter, selected URL, response tab. Like this:

1) Press CTRL+I in Chrome

2) Try to follow this:

enter image description here

pedrouan
  • 12,762
  • 3
  • 58
  • 74