0

I'm building a live search feature on my site and I need to pass a model number variable so the PHP file knows which table to search.

Here are the form fields:

<form class="form-horizontal" name="search" role="form" method="POST" onkeypress="return event.keyCode != 13;">
<?php

$modelnumber = $_SERVER['REQUEST_URI']; 
$modelwithoutex = basename($modelnumber,".php");
$modelwithex = basename($modelnumber);

echo '<input id="modelmfp" name="modelmfp" type="text" value="'.$modelwithoutex.'">';

?>

<input id="name" name="name" type="text" class="form-control " placeholder="Search by error..." autocomplete="off"/>

</form>

Here's the Jquery

$(".tablesearch").hide();
// Search
function search() {
    var query_value = $('input#name').val();
    var query_modelmfp = $('input#modelmfp').val();
    console.log(query_modelmfp);
    console.log(query_value);


if(query_value !== ''){
    $.ajax({
        type: "POST",
        url: "php/search.php",
        data: { query: query_value, query2: query_modelmfp },
        cache: false,
        success: function(html){
            $("table#resultTable tbody").html(html);
        }
    });
    }return false;    
}

Here's the PHP file

$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$themodelmfp = $_POST['query2'];

$search_string = $test_db->real_escape_string($search_string);
$themodelmfp = $test_db->real_escape_string($themodelmfp);

$query = 'SELECT * FROM '.$themodelmfp.' WHERE code LIKE "%'.$search_string.'%"';

Console Log

jquery.min.js:4 POST http://url/search.php 500 (Internal Server Error)

Why isn't the $themodelmfp variable passing? If I manually type the model number it works fine.

The search string passing without any issues.

Any help would be greatly appreciated.

Thanks,

  • Did you console.log() at your js file to check if the variable query_modelmfp is actually being assigned with desired value without any issue ? – Plabon Dutta Apr 22 '20 at 14:21
  • Does print_r($_POST['query2']) output anything ? – Plabon Dutta Apr 22 '20 at 14:24
  • I updated the post with the console log and the console log error. Can you please review? – Kyocera Alerts Apr 22 '20 at 14:28
  • Doesn't make sense. Why would the console.log() will print something like that ? Please give a return false; after the console.log() statements. – Plabon Dutta Apr 22 '20 at 14:30
  • Also, do read up on [SQL-Injection](https://stackoverflow.com/questions/601300/what-is-sql-injection). – Naruto Apr 22 '20 at 14:33
  • Firstly, you need to make sure if those two variables are getting set at javascript end. Then, we can look at php. When you make the ajax request, you can reach the php script, right ? – Plabon Dutta Apr 22 '20 at 14:34
  • `echo $themodelmfp;` in my computer I see it – Dickens A S Apr 22 '20 at 14:44
  • So, your variable is getting passed to php without any issue. Now can you please echo the $query and give the output here ? echo $query; exit; And let me know the output – Plabon Dutta Apr 22 '20 at 14:46
  • console log is blank, its not coming back with anything. Echo is also blank. I tried manually setting the value and its not passing data: { query: query_value, query2: 'DC111' }. Arggggg – Kyocera Alerts Apr 22 '20 at 15:41

0 Answers0