如何从php返回php变量值以及html响应ajax
how to return php variables values along with html response ajax from php
我想返回两个变量:
1) 来自search_members.php 的mysqli查询的记录总数
2) 如果发现任何结果,则为true或false
这是我的代码:
<input type="button" value="Search" onclick="GetSearchResults(0)"
class="SrchBtn" />
<script>
var pageNo = 1;
var DoSearch;
var SearchString = "";
function GetSearchResults()
{
DoSearch = false;
if (document.getElementById("SearchString").value > 0) {
SearchString = document.getElementById("SearchString").value;
DoSearch = true;
if (DoSearch === true) {
$.ajax({
type: 'POST',
url: 'search_members.php',
data: { SearchString: SearchString, pageNo: pageNo},
success: function(response) {
$('#SearchReultsBox').html(response);
}
});
}
}
}
</script>
我想要得到的变量值是$SearchFound和$total_pages,如下所示这是search_members.php 的代码
<?php
include_once('dbConnect.php');
$SearchFound=false;
$Items_PerPage=10;
if (!empty($_POST['SearchString']))
{
$SearchString= $_POST['SearchString'] ;
$SearchString = trim(htmlentities($SearchString));
$SearchString= strip_tags($SearchString);
$SearchString= mysqli_real_escape_string($con,$SearchString);
}
$pageNo=$_POST['pageNo'];
$sql="SELECT * from members where member_name=$SearchString";
$result = $con->query($sql);
$num_rec = mysqli_num_rows($result);
if ($num_rec>0)
{
$SearchFound=true;
$total_pages = ceil($num_rec / $Items_PerPage);
$start_from = ($pageNo-1) * $Items_PerPage;
$sql .= " LIMIT $start_from, $Items_PerPage";
$result = $con->query($sql);
$total_page_records = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
//echo "<table>";.......display table with results
}
}
else
{
echo "<p>no resutls found</p>";
}
?>
如果您想要一个Collection或多个Scalar Value(如HTML Text或Simple String),则可能需要返回JSON。在这里像这样:
<script>
var pageNo = 1;
var SearchString = "";
var DoSearch;
function GetSearchResults() {
DoSearch = false;
if(document.getElementById("SearchString").value>0) {
SearchString = document.getElementById("SearchString").value;
DoSearch=true;
if (DoSearch===true) {
$.ajax({
type: 'POST',
dataType: 'JSON', //EXPLICITLY SET THIS TO JSON
url: 'search_members.php',
data: { SearchString: SearchString, pageNo: pageNo},
success: function(response) {
// YOUR AJAX REQUEST RETURNS JSON DATA WITH ONLY 2 VALUE TYPES - INTEGER & BOOLEAN
// NOW IF YOU WANT TO DISPLAY YOUR SEARCH RESULT
// YOU MAY WANT TO BUILD THAT IN YOUR PHP FILE (PERHAPS WITH A KEY LIKE SO: html
// AFTERWARDS YOU MIGHT BE ABLE TO DO:
//$('#SearchResultsBox').html(response.html);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('Error: '+jqXHR.responseText);
}
});
}
}
}
</script>
转到等式的PHP方面;你可能想做这样的事情:
<?php
/**
* search_member.php
*/
include_once('dbConnect.php');
$SearchFound = false;
$Items_PerPage = 10;
//DECLARE DEFAULT RESPONSE PAYLOAD
$response = array(
"message" => "<p>no resutls found</p>",
"numRecords" => null,
"searchFound" => false,
);
$searchString = isset($_POST['SearchString']) ? htmlspecialchars(trim($_POST['SearchString'])) : null;
$pageNo = isset($_POST['pageNo']) ? htmlspecialchars(trim($_POST['pageNo'])) : null;
if ($searchString) {
$SearchString = strip_tags($SearchString);
$SearchString = mysqli_real_escape_string($con,$SearchString);
}
$sql = "SELECT * FROM members WHERE member_name='{$SearchString}'";
$result = $con->query($sql);
$num_rec = mysqli_num_rows($result);
if ($num_rec>0) {
$SearchFound = true;
$total_pages = ceil($num_rec / $Items_PerPage);
$start_from = ($pageNo-1) * $Items_PerPage;
$sql .= " LIMIT {$start_from}, {$Items_PerPage}";
$result = $con->query($sql);
$total_page_records = mysqli_num_rows($result);
//SINCE YOU WANT TO ONLY RETURN 2 VALUE: DO IT HERE INSTEAD:
$response = array(
"message" => "The World is Good and Everyone is Smiling like you, now.... ;-)",
"numRecords" => $total_page_records,
"searchFound" => $SearchFound
);
/*
while($row = mysqli_fetch_array($result)){
//echo "<table>";.......display table with results
}
*/
}else{
//REMEMBER YOU ARE RETURNING JSON DATA SO NO NEED TO ECHO A HTML DATA RATHER SEND IT BACK AS A PAYLOAD
//echo "<p>no resutls found</p>";
}
//SEND BACK A JSON PAYLOAD BASED ON THE RESPONSE DATA
die( json_encode($response) );
?>
相关文章:
- 通过点击-PHP/HTML发布列表中的选定值
- 在网格中显示来自 PHP [HTML 的数据库的数据]
- 我希望我的php/html网站上的JavaScript能够实时更新
- 我在firebug控制台中得到了index.php.html
- 如何从php/html检查网站是否已打开
- jQuery Slider PHP/HTML Problems
- PHP/HTML:下载PDF链接处理页面加载
- 如何在 php/html 表单中编写链接
- 从生成的 PHP-HTML 表中获取数据库值
- PHP/HTML:如果 url domain.co.uk/?debug 那么
- 免费的应用程序来混淆和缩小JavaScript / PHP / HTML for Mac
- PHP/html:即时搜索功能:无法搜索外语(例如中文)字符
- Php/HTML/css:链接不可点击
- 在 PHP HTML 中向后单击时显示除图像外的所有值
- POST with ajax, php, html
- jquery json PHP HTML 不显示结果
- PHP HTML 表单选择选项来填充文本字段 jQuery
- PHP & HTML Form with javascript onsubmit not executed
- PHP/HTML:将HTML变量保存在PHP会话变量中
- 如何将渲染的PHP+HTML代码转换为文本文件