[英]Sending PHP Variable from AJAX
I am creating a notification checker through AJAX and PHP. 我正在通过AJAX和PHP创建一个通知检查器。
I have created an alert which is "yep" or "nope" to depend if the response is received, however neither alert is showing. 我创建了一个“是”或“否”的警报,取决于是否收到响应,但是两个警报均未显示。
Am I sending my php variable correctly? 我可以正确发送我的php变量吗?
PHP: PHP:
<?php session_start(); if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { //Request identified as ajax request //HTTP_REFERER verification if($_SESSION['email'] == $_GET['email']) { require_once 'connect.php'; /* FORM VARIABLES */ $email = strtolower($_GET['email']); try { $stmt = $pdo->prepare("SELECT * FROM notifications WHERE to=:email"); $stmt->execute(array(":email"=>$email)); $count = $stmt->rowCount(); echo $count; } catch(PDOException $e){ echo $e->getMessage(); } } else { header('Location: http://website.com'); } } else { header('Location: http://website.com'); } ?>
AJAX: AJAX:
<script> function notificationCheck() { $.ajax({ url: 'website.com/page.php?email='<? echo $email ?>, data: "", success: function (data) { var bubification = data; if (bubification === 0) { window.alert("Yes"); } else { window.alert("Nope"); } $('#profile-sidebar-responsive-notifcation-count').html(''+bubification+''); $('#profile-sidebar-notifcation-count').html(''+bubification+''); } }); } $(document).ready(notificationCheck); setInterval(notificationCheck, 10000); </script>
在您的ajax URL中,您缺少php
Try this one. 试试这个。 I see a sytax error in url. 我在网址中看到语法错误。 To concatenate a string in php you use .
要在php中连接字符串,请使用.
and in jquery you use +
. 并在jquery中使用+
。 You haven't concatenated your string with php blocks in url 您尚未将字符串与url中的php块连接在一起
$.ajax({
url: 'website.com/page.php?email='+'<?php echo $email ?>',
method: "GET",
success: function (data) {
var bubification = data;
if (bubification === 0) {
window.alert("Yes");
} else {
window.alert("Nope");
}
$('#profile-sidebar-responsive-notifcation-count').html(''+bubification+''); $('#profile-sidebar-notifcation-count').html(''+bubification+'');
}
});
我认为您有串联错误更改
<? echo $email ?> to +'<?php echo $email ?>'
In PHP if you handling AJAX you are suppose to echo
the response always 在PHP中,如果您处理AJAX,则应该始终echo
显响应
please remove header('Location: http://website.com');
请删除header('Location: http://website.com');
and use something like 并使用类似
echo 'false'
Because if your $email
is not matching you are sending that call to another page. 因为如果您的$email
不匹配,您将把该呼叫发送到另一个页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.