BlondePainter BlondePainter - 1 month ago 9
Ajax Question

PHP page not recieving POSTed jquery ajax request

I have my first PHP page having search form:

<form action="#" class="form-inline" id="form_srch">
<input type="text" id="toSearch" name="toSearch" placeholder="Enter Application Number" class="form-control" style="width:250px" required >
<button type="button" class="btn btn-primary" onclick="search()">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
<i><b><p style="display:none;color:red" id="result">** Empty Value</p></b></i>
</form>

<div id="ajax"></div>


and a javascript function

<script>
function liveCheck() {
$("#result").show();
}

function search() {
var inp = $("#toSearch").val();
if(jQuery.trim(inp).length > 0)
{
jQuery.ajax({
url: "load_search_prereg.php",
data:{ to : inp },
type: "POST",
success:function(data){
$("#ajax").load('load_search_prereg.php');
//alert(data);
},
error:function (){}
});
}
else{
liveCheck();
}

}
</script>


And i have my second PHP page named
load_search_prereg.php
to process the POSTed input and the code is

<?php
require '../backend/_classes.php';
$x = new DB();
$id = $_POST['to'];
$sql=$x->select(1,'*','tblregistration','app_number',"'$id'");
$fetch = $sql->fetchObject();
var_dump($fetch);


The problem is when i clicked the search button in my first PHP page, It displays no array of posted data while im using the
$("#ajax").load('load_search_prereg.php');
method in my javascript. But when i use
alert(data);
, it returns the expected result. Does the problem occur in
$("#ajax").load('load_search_prereg.php');
method ? Help pls.

Answer

Not:

$("#ajax").load('load_search_prereg.php');

But:

$("#ajax").html(data);

I've assumed you just want to write this data into div

edit: instead of var_dump($fetch); you must return your data a JSON object, eg:

$fetch = $sql->fetchObject();
echo json_encode($fetch);

Then you can do in js what you want