簡體   English   中英

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

[英]How to return value from php file in .ajax jquery

ajax不從php文件返回數據我希望我的php文件中的數據顯示在我使用的keyup函數上,php文件工作正常,但在下一頁上顯示結果我想在同一頁上顯示它是我的代碼

<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>

這是表格

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

這是我的PHP代碼

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

擺脫<form>標記-如果您不提交表單,則不需要它。 我懷疑正在發生的事情是,當用戶按下Return鍵時,表單正在提交,正在重新加載頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM