KDJ
KDJ

Reputation: 79

AJAX call not working to PHP

This is making no sense at all.

I've tried to work the code on my web platform and also from my computer.

Here is my checklist.

1) Port 3306 is open for mySQL.

2) Code minimized as shown below.

3) Used an online PHP code tester to see if there are any errors.

4) Used Document.ready and called the method with the ajax call.

5) Used an alert to test if my html contained any errors.

6) Checked the file permissions

7) mysqli

Here is the code:

PHP

<?php
include 'pass.php';
//ini_set('max_execution_time', 4000);
ini_set('memory_limit', '16M');
try {
    $con = mysqli_connect($host, $username, $password, $db_name, $port);

    if (mysqli_connect_error()) {

        echo 'Could not connect to database';

    }
    echo 'hello';
}
catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
}
mysqli_close($con);
?>

AJAX

function getExample() {
    alert("test");
    $.ajax({
        type: "GET",
        url: "code/getExample.php",
        dataType: "html",
        cache: false,
        success: function(response) {
            alert(response);
        }
    });
}

The alert test is showing on my webpage so no errors so far and the file permissions except for pass is read and write and execute.

From your experience, whats going wrong?

Note: I dont want to send data and eventually I want to retrieve it as json but for the time being Im testing with dataType: 'html',.

I'm expecting hello to appear as an alert but nothing appears only alerts test before the ajax call. ( in reply to comment)

* EDIT *

403 forbidden error... when I set my pass file to read only it resets to read and write... it looks like changing the file permissions does not work.

Upvotes: 1

Views: 1232

Answers (2)

KDJ
KDJ

Reputation: 79

I found the problem, it might be because my webhost has a selective policy on file permissions. MY folder and files needed selected file permissions and when I used my ftp it seemed to have messed up the configurations.

The lesson is to check file permissions with ftp provider and avoid the ftp clients ftp permissions.

When the simple things dont work, nothing works and they know too darn well.

Upvotes: 1

Brad Wong
Brad Wong

Reputation: 1

Firstly I suggest you create some simple file to test it. Make sure your JQuery Library included.

html:

<button id="testAction">Test Ajax</button>

Jquery:

$("#testAction").click(function(){
    $.ajax({
        type: "post",
        url: "testAction.php",
        data: {action:"PrintHi", PrintMessage:"HI"},
        success: function(data){
          alert(data);
        }
    });
});

PHP(testAction.php):

<?php
    if($_POST["action"]=="PrintHi"){
    echo $_POST["PrintMessage"];
    }
?>

If it can work, just start to put your function to your php file.

Upvotes: 0

Related Questions