0

ajax not returning data from php file i want my the data from php file to be shown on keyup function i used, the php file works fine but show result on next page i want to show it on the same page here is my code

<script>
$(document).ready(function () {
    $("#searchtext").keyup(function () {
        var searchtext = $("#searchtext").val();
        var searchby = $("#searchby").val();
        $.ajax({
            url: "searchtw.php",
            dataType: "html",
            type: 'POST',
            async: true,
            data: {
                searchtxt: searchtext,
                searchby: searchby
            },
            success: function (result) {
                $("#result").append(result);
            }
        });
    });
});
</script>

here is form

<form id="search" enctype="multipart/form-data">
<input type="text" name="searchtext" id="searchtext"  />
</form>
<div id="result"></div>

and here is my php code

<?php
require 'opendb.php';
$offi = $_POST["searchtext"];
$sql="SELECT * FROM abc WHERE tw_UN=$offi";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
echo "<table border='1'>
<tr>
<th>College Number</th>
<th>Name</th>
<th>Session</th>
</tr>
<tr>
<td>" . $row['tw_CN'] . "</td>
<td>" . $row['txtName'] . "</td>
<td>" . $row['numSession'] . "</td>
</tr>";
echo "</table>";
?> 
4
  • Any messages in developer's console? Commented Oct 24, 2013 at 19:35
  • Check your console, although based on this code, you're going to be appending a lot of tables. Commented Oct 24, 2013 at 19:35
  • $_POST["searchtext"]; but you're property name is searchtxt Commented Oct 24, 2013 at 19:36
  • You're open to an SQL Injection attack. You should escape your search term like this: $offi = mysqli_real_escape_string($con, $offi); before using it in your query. Commented Oct 24, 2013 at 19:39

1 Answer 1

1

Get rid of the <form> tag -- you don't need it if you're not submitting a form. I suspect what's happening is that when the user presses Return, the form is submitting, which is reloading the page.

Sign up to request clarification or add additional context in comments.

Comments

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.