简体   繁体   中英

JQuery AJAX call to php function

I am trying to replace page reloading PHP scripts in a web page with AJAX calls.

I am using JQuery to run the AJAX scripts but it doesn't seem to be doing anything so I attempted to write an incredibly basic script just to test it.

My directory is as follows

public_html/index.php
           /scripts/phpfunctions.php
                   /jqueryfunctions.js

index.php contains

<!DOCTYPE html>
<html>
<head>

    <!-- jquery functions -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="scripts/jqueryfunctions.js"></script>

    <!-- php functions -->
    <?php include 'scripts/phpfunctions.php' ?>

</head>

<body>
    <button type="button" id="testButt">TEST</button>
</body>

</html>

Then the phpfunctions.php page which I am trying to call contains just an echo if an argument is set

<?php

    if(isset($_GET["action"])) {
        echo "test has been run";
    }

?>

The jqueryfunctions.js script I am trying to run is

$(document).read(function () {
    $('#testButt').on('click', function () {
        console.log("jquery call worked"); // this bit does run when button is clicked
        $.ajax({ // this bit doesn't seem to do anything
            url: 'scripts/phpfunctions.php?action=run_test',
            type: 'GET',
            success: function (data) {
                $('#ajaxdata').html(data);
            },
            error: function (log) {
                console.log(log.message);
            }
        });
    });
});

I see that the jqueryfunctions.js function is being called by the first console.log but it doesn't seem to be calling my phpfunctions.php function.

I was expecting to see the php echo "test has been run" but this doesn't happen.

Did I miss something?

You should use isset() method:

<?php
    if(isset($_GET["action"])) {
       if($_GET["action"] == "run_test") {
          echo "test has been run";
       }
    }
?>

and if you are using ajax then why do you need to include it on index page:

<?php include 'scripts/phpfunctions.php' ?>

and i can't find this element $('#ajaxdata') on your index page.


Also you can check the network tab of your inspector tool to see the xhr request to the phpfunctions.php and see if this gets successfull or there is any error.

I think problem is here:

$(document).read(function() {

    $('#testButt').on('click', function() {

        console.log("jquery call worked"); // this bit does run when button is clicked

        $.ajax({   // this bit doesn't seem to do anything
            url: 'scripts/phpfunctions.php',
            type: 'GET',
            data: {action:'run_test'}, // <------ Here
            success: function(data) {
                $('#ajaxdata').html(data);
            },
            error: function(log) {
                console.log(log.message);
            }
        });

    });

});

jQuery says :

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting.

So you should set data: {key:'value'}

Most things look fine, but your data attribute is designed for "POST" requests, try to add the data to the url as follows:

$( document ).read( function ()
{
    $( '#testButt' ).on( 'click', function ()
    {
        console.log( "jquery call worked" ); // this bit does run when button is clicked
        $.ajax( {   // this bit doesn't seem to do anything
            url: 'scripts/phpfunctions.php?action=run_test', // Add the GET request to the end of the URL
            type: 'GET',
            //data: 'action=run_test', Unrequired noise :P (this is for post requests...)
            success: function ( data )
            {
                $( '#ajaxdata' ).html( data );
            },
            error: function ( log )
            {
                console.log( log.message );
            }
        } );
    } );
} );

And also ( as mentioned in my comments ), you need to finish your bodys closing tag:

</body> <!-- Add the closing > in :P -->

</html>

I hope this helps :)

Where do you load ajaxfunctions.js? It look like in your code you never load the resource And change

<button id="xxx">

In

<button type="button" id="xxx">

So the page isn't reloaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM