javascript - php can not get the data send by ajax -


this js code, ajax has 2 arguments, first url, 2nd object contains type data , onsuccess. (i didn't use jquery function define myself, code @ end of question) want send 'text' string php, there problem this? have tried change data data: {searchinput:"text"}, still don't work.

ajax(      'http://localhost/test.php',       {          type: 'post',          data:  "searchinput=text",          onsuccess: function (responsetext, xhr) {              console.log(responsetext);          }      }  );

this php code, sorry changing code wrong while pasting on.

$searchinput = $_post["searchinput"];  @ $db = new mysqli('localhost', 'root', '', 'text');  if (mysqli_connect_errno()) {      echo "error:can not connect database";  }  $query = "select * text data like'".$searchinput."%' ";  $result = $db->query($query);

then error is

undefined index: searchinput

i have search method change onsuccess function settimeout, , ajax again, doesn't work, send data again php still can't data

this ajax function

function ajax(url, options) {      if (!options.type) {          options.type = "post"      };      var xhr = new xmlhttprequest();      xhr.open(options.type, url, true);      xhr.send(options.data);      xhr.onreadystatechange = function () {          if (xhr.readystate == 4) {          if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {              options.onsuccess(xhr.responsetext, xhr)          } else {              options.onfail(xhr.responsetext, xhr);          }          };      }  }

well, since used ajax wrong, i'm not surprised. there should error in console.

jquery ajax used this:

$.ajax({         url: "http://localhost/test.php",         type: 'post',         data:  {searchinput: text},         success: function (responsetext, xhr) {             console.log(responsetext);         }     } ); 

url part of object ajax expects, needs inside , not outside of it. also, data expecting object, gave plain string.

also, @muhammad ahmed stated in answer, using wrong variable in php code.

edit: ajax in javascript without jquery:

var request = new xmlhttprequest(); request.open('post', 'http://localhost/test.php', true);  request.onreadystatechange = function() {   if (this.readystate === 4) {     if (this.status >= 200 && this.status < 400) {       // worked       var data = json.parse(this.responsetext);     } else {       // failed     }   } }; request.send(); request = null; 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -