简体   繁体   中英

Ajax post not successfully opening PHP file

The ajax:

$(".searchbar").keyup(function() {
   let search = $(".searchbar").val();
   if (search.charAt(search.length-1) == " ") {
      $.post("search.php", {
         search: search
      });
   }
});

All of this code "works" because ive tested it with console.log()

The problem is search.php is not being opened. I know that its not being opened because ive set a breakpoint in that file with NetBeans and its never hit.

Search.php (unfinished):

<?php
  require_once 'core/init.php';

  if (Input::exists()) { 
    $_db = Database::getInstance();
    $links[];
    $databaseResults[];
    $searchTerms[] = explode(' ', Input::get('search'));
    foreach ($searchTerms as $searchTerm) {
      $databaseResults = $_db->search('links', array("name", "hash", "file_extension", "created"), $searchTerm, "%%");
    }
  }
 ?>

Ajax that does work for me:

$(".votebutton").click(function() {
   let vote = $(this).val();
   let token = $(this).parent().find(".token").val();
   let link_hash = $(this).parent().find(".link_hash").val();
   $.post("vote.php", {
      vote: vote,
      token: token,
      link_hash: link_hash
   });
});

Assuming that the URLs and your system are compatible and that search.php should run, there are a few issues which you need to look at

The condition

(search.charAt(search.length-1) == " ")

Means that the last character of the tag is space. Your search is to be triggered if the use hits space, that is, he/she obviously did not finish searching. Now, if you test without hitting space, the search will not be triggered.

The debugger

It's possible that your NetBeans debugger does not work. You should check the Network tab in your browser instead and see whether a request was sent. If so, try sending back a dummy answer from your PHP as a test to see that it was triggered.

The location of the breakpoint

You should put the breakpoint to a location which would surely be hit. Since Search is not hit, it's possible that there was a request (see the previous point), but for some reason this file was not triggered (check the server settings).

EDIT

It turns out it was a syntactical error, initializing arrays in a syntactically incorrect manner, so the PHP parser has broken and the debugger was not working.

It turns out the PHP file was the problem.

It seems you cant do:

$links[];
$databaseResults[];

To create an array.

Since the PHP file had an error it did not even hit line one which confuses me.

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