abdul
abdul

Reputation: 1

How to return value from php file in .ajax jquery

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>";
?> 

Upvotes: 0

Views: 2041

Answers (1)

Barmar
Barmar

Reputation: 780723

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.

Upvotes: 1

Related Questions