如何从.ajax jquery中的php文件返回值

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>";
?> 
douyunjiaok300404
douyunjiaok300404 您对SQL注入攻击持开放态度。你应该像这样逃避你的搜索词:$offi=mysqli_real_escape_string($con,$offi);在查询中使用它之前。
接近 7 年之前 回复
dourang8305
dourang8305 $_POST[“SEARCHTEXT”];但你的财产名称是searchtxt
接近 7 年之前 回复
dongzu3511
dongzu3511 检查你的控制台,虽然基于这个代码,你将附加很多表。
接近 7 年之前 回复
duangang79177
duangang79177 开发人员控制台中的任何消
接近 7 年之前 回复

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问