I have been looking at this code and can't see the errors.
I would like as a test for myself to post a SQL query to a PHP file and do the query and return the selected rows. So what is wrong with this POST, and how do I pickup the query on the PHP side. Right now I don't receive a value looking at $_POST.

$(document).ready(function () {
    $("button[name='History']").click(function () {
        var btnHistory = ("In Click function!");
        console.log(btnHistory);
        queryStr = "SELECT * FROM time_table WHERE" +
                " EXTRACT(MONTH FROM DATE_FIELD ) = DATE_FORMAT(NOW()," +
                " '%c')  AND  EXTRACT(DAY FROM DATE_FIELD ) = DATE_FORMAT(NOW(), '%e')" +
                " ORDER BY DATE_FIELD";

        $.ajax({
            type: "POST",
            contentType: "text/plain",
            url: "showDatabase02.php",
            data: queryStr,
            success: function (data) {
                console.log("returned data = " + data);
            }
        });
        return false;
    });
});

In the php file:
$query = $_POST[0];

Recommended Answers

All 2 Replies

Hello rouse, yes you are lost there. With AJAX you only send a request to the server , you don't execute code there. Any other attempt with evil eval or sending the query directly, is simple out of what AJAX (or even PHP) is. I suggest to read and try some tutorials about AJAX in the web (just google it and test the first five or so).

I would gave you an example using your code but I don't understand a lot in it. For example what supposed to be '%c' or '%e' in your code? (Is this a code that will run regularly , if so is it a good idea to make such extensive use of EXTRACT ? )

Having worked my way through the following situation. I send a SQL query to a PHP file that process the query and then sends the result set back. The result set is Json_encode and the echo(ed) from the server back to the jQuery AJAX call.
When the result set is picked up on the javascript side I use a JSON.parse to format the data. This produces an error because the Json_encoded data is contaminated with escape characters. Does anyone have a method of removing these characters. I can’t post my code in this note as I don’t see the editing and formatting tools displayed in the REPLY editor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.