0

I'm trying to display the search result of my page under the the search area. So I used AJAX to display the result in a div. but I could'nt get it work.

I have three main pieces, the div, the searchResult page and the ajax function

<input type="text" name="studentName">
<button type="submit" name="searchByName" onclick='get_info();'>بحث</button>

  <div id="searchResult"><b></b></div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function get_info() { // Call to ajax function


  $.ajax({
    type: "POST",
    url: "NameSearchResult.php", // Name of the php files
    data: {name: <?php echo $_POST['studentName']; ?>},
    success: function(html)
    {
        $("#searchResult").html(html);
    }
  });
 }

and my search Page:

<?php
include_once 'dbConfigBDO.php';

$studentName = $_POST["name"];
$counter=0;
$emptyString = "لايوجد";

$sql = "SELECT * FROM Student";
$result = $conn->query($sql);
$row_count = $result->rowCount();
if ($row_count > 0){
  ..........  }

Now when I search nothing appears, although it works when I put all the code in one page (which would be messy in term of the appearance of the result!).

2
  • Based on your code you want to update HTML after pressing button, right? So, where's your get_info() JS function? Also, if you are not redirected with POST on this page, you can't take $_POST['studentName'] because there's no request. You should take that field using $('#student-name').val() and set the id="student-name" to your input. Commented Jan 31, 2019 at 6:24
  • Sorry I have updated my question Commented Jan 31, 2019 at 6:30

3 Answers 3

2

From function return the output as per below: return json_encode($result);

In ajax call use dataType:"json" and show your html

Example ajax call:

$.ajax({
type: "POST",

dataType:"json",

url: "NameSearchResult.php", // Name of the php files

data: {name: $("#studentName").val()},

success: function(html)
Sign up to request clarification or add additional context in comments.

1 Comment

it gives an error message: json_encode is not defined.
1

change code like this

<input type="text" name="studentName" id="studentName">
<button type="submit" name="searchByName" onclick='get_info();'>بحث</button>

  <div id="searchResult"><b></b></div>
<script>
  $.ajax({
    type: "POST",
    url: "NameSearchResult.php", // Name of the php files
    data: {name: $("#studentName").val()},
    success: function(html)
    {
        $("#searchResult").html(html);
    }
  });
 }
</script>

1 Comment

I did add the id part and changed AJAX data but nothing happened.
0

inside ajax success method try catch what you are getting

success: function(html)
{
   console.log(html);

}

if you getting something then your code must be work.

6 Comments

see in your browser console. What you getting in your ajax response it will show there .
From function return the output as per below: return json_encode($result); $.ajax({ type: "POST", url: "NameSearchResult.php", // Name of the php files data: {name: $("#studentName").val()}, success: function(html)
something comes very quickly then disappear.
can you provide the code what are you trying to return .
success: function(html) { console.log(html); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.