0

im trying to get some data from the Fetch.php but unfortunately there's an error.

I tried to use an older version of jQuery but it's not working either. Now I'm using jQuery 3.5.1

The ajax script:

<script>
$(document).ready(function() {
    load_data();
    function load_data(query)
    {
        $.ajax({
            type: 'POST',
            url: 'Fetch.php',
            data: {query: query},

            success: function(data)
            {
                $('#result').html(data);
            },
            error: function (err) {
                console.log(err);
            }
        });
    }

    $('#search_text').keyup(function() {
        const search = $('#search_text').val();
        if(search != '')
        {
            load_data(search);
        }
        else
        {
            load_data();
        }
    });
});

Fetch.php

var_dump('query: ' . $_POST["query"]);

Error:

jquery.min.js:4 POST Fetch.php 500
send @ jquery.min.js:4
ajax @ jquery.min.js:4
load_data @ index.php:46
(anonymous) @ index.php:65
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3
randomuser
  • 19
  • 2
  • check error log, you have a 500 error caused by *something* – Lawrence Cherone Oct 31 '20 at 15:56
  • There is no issue with your jQuery code. Make sure that your file name is correct, as they are case sensitive. You can try to debug by enabling the error display. – tanay jha Oct 31 '20 at 16:03
  • Does changing `data: {query: query},` to `data: {'query': query},` fix this? Not sure whether it would or not. If not, is there more code in the Fetch.php file that could be causing the 500 error? – Spoon Oct 31 '20 at 16:45
  • You should check the file Fetch.php, as Tanay said it's case sensitive. Please provide the HTML as well. – sativay Nov 01 '20 at 04:23

1 Answers1

0

I believe you are looking for form serialization in order for the form (just search text in this case) to be passed to Fetch.php properly.

var qry = $('$formnName').serialize();
$.ajax({
  method: 'POST',
  url: 'Fetch.php',
  data: qry,
  ...
});

See Pass entire form as data in jQuery Ajax function

sativay
  • 162
  • 11