如何使用和检查<;?php if($loggedin):?>;如果在这个javascript代码中有条件从服务器端回显按钮按下或弹出?
我想勾选这个如何使用和检查<;?php if($loggedin):?>;如果在这个javascript代码中有条件从服务器端回显按钮按下或弹出?,javascript,php,ajax,Javascript,Php,Ajax,我想勾选这个'php,如果JavaScript代码中的条件代码显示下面在search-box.php中以粗体突出显示的按钮按下或弹出,顺便说一句,我有一个想法:我可以在js之外使用这个php$loggedin代码&用true或false存储一个js变量&因此我可以用js变量显示按钮,但我不想这样做,因为我想从客户端隐藏按钮代码,这就是为什么我需要把它放在php中。我想把这个按钮放在result+=js代码中,因为它将在textbox中搜索时显示为search-box.php网页中的输出 sear
'
php,如果JavaScript代码中的条件代码显示下面在search-box.php中以粗体突出显示的按钮按下或弹出,顺便说一句,我有一个想法:我可以在js之外使用这个php$loggedin代码&用true或false存储一个js变量&因此我可以用js变量显示按钮,但我不想这样做,因为我想从客户端隐藏按钮代码,这就是为什么我需要把它放在php中。我想把这个按钮放在result+=
js代码中,因为它将在textbox中搜索时显示为search-box.php网页中的输出
search-backend.php文件
<?Php
require "config2.php"; // Database Connection
///////
//// Collect options from table ///
@$search_text = $_GET['txt'];
//echo $search_text;
/////////// Start of preparation for Paging //////
@$end_record = $_GET['endrecord'];//
//$end_record=20;
if (strlen($end_record) > 0 AND (!is_numeric($end_record))) {
echo "Data Error 1";
exit;
}
$limit = 5; // Number of records per page
if ($end_record < $limit) {
$end_record = 0;
}
switch (@$_GET['direction']) // Let us know forward or backward button is pressed
{
case "fw":
$start_record = $end_record;
break;
case "bk":
$start_record = $end_record - 2 * $limit;
break;
default:
//echo "Data Error";
//exit;
$start_record = 0;
break;
}
if ($start_record < 0) {
$start_record = 0;
}
$end_record = $start_record + $limit;
////////// End of preparation for paging /////
$search_text = trim($search_text);
$message = '';
$query = '';
$query2 = '';
$kt = preg_split("/[\s,]+/", $search_text);//Breaking the string to array of words
// Now let us generate the sql
while (list($key, $val) = each($kt)) {
if (!ctype_alnum($val)) {
$message .= " Data Error 2 ";
//echo "Data Error";
$main = array('value' => array("message" => "$message"));
exit(json_encode($main));
}
if ($val <> " " and strlen($val) > 0) {
$query .= " keyyo like '%$val%' or ";
$query2 .= " keyyo like '%$val%' and ";
}
}// end of while
$query = substr($query, 0, (strLen($query) - 3));
$query2 = substr($query2, 0, (strLen($query2) - 4));
// this will remove the last or from the string.
$q = "select count(id) from users where confirmcode='y' and " . $query;
$q2 = "select count(id) from users where confirmcode='y' and " . $query2;
$query = 'select `id`, `subject` from users where ' . $query . " limit $start_record,$limit ";
$query2 = 'select `id`, `subject` from users where ' . $query2 . " limit $start_record,$limit ";
///////////////End of adding key word search query //////////
//echo $query;
$message .= $query;
$records_found = $dbo->query($q)->fetchColumn(); // Number of records found
$records_found2 = $dbo->query($q2)->fetchColumn(); // Number of records found
$records_found_total = $records_found + $records_found2;
$row = $dbo->prepare($query);
$row->execute();
$result = $row->fetchAll(PDO::FETCH_ASSOC);
$row2 = $dbo->prepare($query2);
$row2->execute();
$result2 = $row2->fetchAll(PDO::FETCH_ASSOC);
$result = array_merge($result2);
//$nume=count($result);
if (($end_record) < $records_found_total) {
$end = "yes";
} else {
$end = "no";
}
if (($end_record) > $limit) {
$startrecord = "yes";
} else {
$startrecord = "no";
}
$main = array(
'data' => $result,
'value' => array(
"no_records" => "$records_found",
"no_records2" => "$records_found2",
"message" => "$message",
"status1" => "T",
"endrecord" => "$end_record",
"limit" => "$limit",
"end" => "$end",
"startrecord" => "$startrecord"
)
);
echo json_encode($main);
//// End of data collection from table ///
?>
search-box.php文件
<?php $loggedin = isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true; ?>
<html>
<head>
<title>Ajax keyword search</title>
<META NAME="DESCRIPTION" CONTENT=" ">
<META NAME="KEYWORDS" CONTENT="">
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript">
function ajaxFunction(val) {
var httpxml;
try {
// Firefox, Opera 8.0+, Safari
httpxml = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
httpxml = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpxml = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateChanged() {
if (httpxml.readyState == 4) {
/////////////
var myarray = JSON.parse(httpxml.responseText);
//alert(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for (j = document.getElementById('subject').length - 1; j >= 0; j--) {
document.getElementById('subject').remove(j);
}
var str = '';
var result = '';
for (i = 0; i < myarray.data.length; i++) {
str += ''; // Storing options in variable
result += +myarray.data[i].id + ',' + myarray.data[i].subject + '<br>';
result += ("<?php **if($loggedin) { echo "<button>Push</button>"; } else { echo "<button>Pop</button>";} ?>");
**
}
///////////
document.getElementById("subject").innerHTML = str;
document.getElementById("result").innerHTML = result;
if (myarray.value.status1 != 'T') {
document.getElementById("msg").innerHTML = "About " + myarray.value.no_records2 + " & " + myarray.value.no_records + " results " + " Message : " + myarray.value.message;
} else {
document.getElementById("msg").innerHTML = "About " + myarray.value.no_records2 + " & " + myarray.value.no_records + " results ";
}
var endrecord = myarray.value.endrecord
document.getElementById("navigation").innerHTML = "<table width=700><tr><td width=350><input type=button id=\'back\' value=Prev onClick=\"ajaxFunction('bk'); return false\"></td><td width=350 align=right><input type=button value=Next id=\"fwd\" onClick=\"ajaxFunction(\'fw\'); return false\"></td></tr></tr> </table>";
myForm.st.value = endrecord;
if (myarray.value.end == "yes") {
document.getElementById("fwd").style.display = 'inline';
} else {
document.getElementById("fwd").style.display = 'none';
}
if (myarray.value.startrecord == "yes") {
document.getElementById("back").style.display = 'inline';
} else {
document.getElementById("back").style.display = 'none';
}
}
}
var url = "search-backend.php";
var str = document.getElementById("keyword").value;
var myendrecord = myForm.st.value;
url = url + "?txt=" + str;
url = url + "&endrecord=" + myendrecord;
url = url + "&direction=" + val;
url = url + "&sid=" + Math.random();
//document.getElementById("txtHint").innerHTML=url
httpxml.onreadystatechange = stateChanged;
httpxml.open("GET", url, true);
httpxml.send(null);
document.getElementById("msg").innerHTML = ":)...";
document.getElementById("msg").style.display = 'inline';
////////////////////////////////
}
</script>
</head>
<body>
<div id=msg style="position:absolute; width:300px; height:25px;
z-index:1; left: 400px; top: 0px;
border: 1px none #000000"></div>
<br><br><br>
<form name="myForm">
<input type=hidden name=st value=0>
<input type="text" onkeyup="ajaxFunction('');" name="keyword" id="keyword" list="subject" size=70/>
<datalist id="subject">
</datalist>
<br><br>
<div class='t1' id='result'>
</div>
</form>
<div class='t1' id='navigation'>
</div>
</body>
</html>
Ajax关键字搜索
函数ajaxFunction(val){
var-httpxml;
试一试{
//Firefox、Opera 8.0+、Safari
httpxml=新的XMLHttpRequest();
}
捕获(e){
//Internet Explorer
试一试{
httpxml=新的ActiveXObject(“Msxml2.XMLHTTP”);
}
捕获(e){
试一试{
httpxml=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
捕获(e){
警告(“您的浏览器不支持AJAX!”);
返回false;
}
}
}
函数stateChanged(){
if(httpxml.readyState==4){
/////////////
var myarray=JSON.parse(httpxml.responseText);
//警报(httpxml.responseText);
//在添加新元素之前,我们必须删除以前加载的元素
对于(j=document.getElementById('subject')。长度-1;j>=0;j--){
document.getElementById('subject')。remove(j);
}
var-str='';
var结果=“”;
对于(i=0;i';
结果+=(“”);
**
}
///////////
document.getElementById(“主题”).innerHTML=str;
document.getElementById(“结果”).innerHTML=result;
如果(myarray.value.status1!=“T”){
document.getElementById(“msg”).innerHTML=“关于”+myarray.value.no_records 2+”&“+myarray.value.no_records+”结果”+”消息:“+myarray.value.Message;
}否则{
document.getElementById(“msg”).innerHTML=“关于”+myarray.value.no_记录2+”和“+myarray.value.no_记录+”结果”;
}
var endrecord=myarray.value.endrecord
document.getElementById(“导航”).innerHTML=“”;
myForm.st.value=endrecord;
如果(myarray.value.end==“是”){
document.getElementById(“fwd”).style.display='inline';
}否则{
document.getElementById(“fwd”).style.display='none';
}
如果(myarray.value.startrecord==“是”){
document.getElementById(“back”).style.display='inline';
}否则{
document.getElementById(“back”).style.display='none';
}
}
}
var url=“search backend.php”;
var str=document.getElementById(“关键字”).value;
var myendrecord=myForm.st.value;
url=url+“?txt=“+str;
url=url+“&endrecord=“+myendrecord;
url=url+“&direction=“+val;
url=url+“&sid=“+Math.random();
//document.getElementById(“txtHint”).innerHTML=url
httpxml.onreadystatechange=stateChanged;
open(“GET”,url,true);
httpxml.send(null);
document.getElementById(“msg”).innerHTML=“:)…”;
document.getElementById(“msg”).style.display='inline';
////////////////////////////////
}
search-box.php代码的输出:
您可以简化代码以显示您的问题。我想知道这个代码是否有用? 编辑:(好评论,@Munim Munna):这是一个将PHP条件传递给页面中javascript的基本示例。如果这太简单,我很抱歉。我已经对代码进行了注释
<?php
// change "true" to "false" to see the other condition.
$loggedin = (true) ? 1 : 0; // this is the result condition of your extensive processing
?>
<html><!-- normal HTML structure -->
<head>
<script>
window.onload = function() { // an elementary example of checking a condition supplied from the server (PHP)
"use strict";
var elt = document.getElementById("div1"); // the element to store the results
if (<?php echo $loggedin ?>) { // the PHP variable is inserted
// you can do your "highlighting" or "hiding" or whatever here
elt.innerHTML = "Is logged in"; } // if true
else {
// you can do your "highlighting" or "hiding" or whatever here
elt.innerHTML = "Is NOT logged in"; } // if false
}
</script>
</head>
<body>
<div id="div1">
</div>
</body>
</html>
window.onload=function(){//检查服务器(PHP)提供的条件的基本示例
“严格使用”;
var elt=document.getElementById(“div1”);//存储结果的元素
if(){//插入PHP变量
//你可以在这里做“突出显示”或“隐藏”之类的事情
elt.innerHTML=“已登录”;}//如果为true
否则{
//你可以在这里做“突出显示”或“隐藏”之类的事情
elt.innerHTML=“未登录”}//如果为false
}
这并不完全清楚您的要求-PHP在服务器端执行,JS在客户端执行。您可以执行PHP并将其存储在JS全局变量中-您将存储的是值,而不是PHP代码。请添加对所提供代码的一些解释,并避免回答问题。从@Munim Munna开始,我不知道在我的答案中编辑“@Munim Munna”是否会让你大吃一惊,所以我也在评论。谢谢。不,编辑回答没有发送通知