-1

How to make sure that my ajax is properly calling my PHP file. My php file is supposed to print items on the console as well as send data to mysql. Neither are happening and I wonder is it because my Ajax isn't properly calling it?

None of my echos print on the console making me believe that it isn't running.

JS File

 var dataString = JSON.stringify(survey.data);
    jQuery.support.cors = true;

     $.ajax({
             type: "GET",
            dataType: "json",
             url: "connect.php",
             data: {dataString},
          contentType: "application/json; charset=utf-8",
          success: function( data, textStatus, jQxhr ){
             $('#response pre').html( data );
         },
         error: function( jqXhr, textStatus, errorThrown ){
         //    console.log( errorThrown );
         }
     }); 

Connect.php

<?php

echo '$test';

echo '<script>console.log("Your stuff here")</script>';

$json = (file_get_contents("php://input"));

$obj = json_decode($json,true);


header("Content-Type: application/json; charset=UTF-8")


$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "arc";


$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


 ?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
whatstheword
  • 3
  • 1
  • 3
  • 1
    Check the Network tab in your browser's debugging tools. Is the AJAX request being made? Does it contain the data you expect? What is the server's response? Is it what you expect? Are there any errors in your PHP logs? It also looks like you're trying to read the request body in a GET request, but a GET has no request body. – David Apr 22 '19 at 23:38
  • 1
    You're using functions that don't exist any more in PHP. No more `mysql_*` for quite some time now. – miken32 Apr 22 '19 at 23:40
  • 1
    Also syntax errors in the PHP including bad quotes. – miken32 Apr 22 '19 at 23:41
  • @David the network tab shows the data that I want being posted to the php file, server status is 200, type xhr. How can I check the php logs in the browser? – whatstheword Apr 22 '19 at 23:52
  • @miken32 Thanks updated it, vscode isn't showing any errors for me. How can I fix my syntax errors? – whatstheword Apr 22 '19 at 23:52
  • @whatstheword: You check the PHP logs on the server. See: https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel – David Apr 22 '19 at 23:53
  • 1
    Once you do that you'll see fatal errors about missing semicolons. Once you fix that you'll see warnings about setting a header after sending content. – miken32 Apr 22 '19 at 23:54

1 Answers1

0

You have several problems.

  1. If you want to send JSON to the server, you shouldn't put the data: parameter in an object. It should be

    data: dataString,
    

    When you use an object, it converts it to URL-encoded format, so it was sending dataString=<your JSON>, which is not valid JSON so json_decode() in the PHP script was failing. You could have used:

    $obj = json_decode($_POST['dataString'], true);
    

    to get it instead of reading from php://input.

  2. Since you have

    dataType: 'json',
    

    in the AJAX call, jQuery will try to parse the response as JSON. If the parse fails it won't call your success: callback (if you provide an error: callback it will call it). Since you're returning HTML, use

    dataType: 'html',
    

    instead.

    If this is just for testing, you could leave it as it is, and use the Network tab of the browser's Developer Tools to see whether it called the script and what the raw response was.

Barmar
  • 741,623
  • 53
  • 500
  • 612