我正在尝试向关系数据库添加一些数据,并希望session_user_id是该数据库的外键。当用户单击一个按钮时,我想创建一个数据库条目,其中包含session_user_id和我张贴到页面上的一些其他信息。我的ajax发布到运行它的php网页上(意味着我所有的脚本都在同一页面上)
我目前正在领取Uncaught ReferenceError: $sess_user_id1 is not defined
。jquery正在启动。虽然我很想修复未定义的变量,但总的来说,这似乎不是一种非常直接的方法,并且添加了一堆令人困惑的变量,而我需要的所有变量都已经在我的PHP语句中了。有没有任何方法可以在不经过ajax并且不必再次定义变量的情况下触发PHP条目?
这是我的php,它在标题处,与我的JS和HTML:在同一页上
<?php
$markerid = $_POST["id"];
$name = $_POST["name"];
$type = $_POST["type"];
$point = $_POST["point"];
$lat2 = $_POST["lat"];
$lng2 = $_POST["lng"];
$locationdescription = $_POST["locationdescription"];
$locationsdirections = $_POST["locationdirections"];
session_start();
if (!isset($_SESSION['sess_user_id']) || empty($_SESSION['sess_user_id'])) {
// redirect to your login page
exit();
}
$sess_user_id1 = $_SESSION['sess_user_id'];
if ((isset($_POST['usid'])) && (isset($_POST['usid']))) {
$user_id_follow = strip_tags($_POST['usid']);
echo $user_id_follow;
$query = "INSERT INTO markerfollowing ( userID, markerID, type )
VALUES ('$user_id_follow', '$markerid', '$type');";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
mysql_close();
}
?>
这是HTML按钮:
<div class="btn pull-right">
<button class="btn btn-large btn-followmarker" type="submit"id="followmarker">Add me to the list</button>
</div>
以下是jquery/ajaxpost:
<script/javascript>
$(document).ready(function () {
$("#followmarker").click(function(){
$.ajax({
type: "POST",
url: "", //
data: { usid: <?php echo '$sess_user_id1'; ?>},
success: function(msg){
alert("success");
$("#thanks").html(msg);
},
error: function(){
alert("failure");
}
});
});
});
</script
衷心感谢您的帮助。我以前从未使用过关系数据库。
<?php echo '$sess_user_id1'; ?>
是错误的。如果你习惯于$sess_user_id1处的data: { usid: 123}
是123,您应该编写
data: { usid: <?php echo "$sess_user_id1"; ?>}
在你的争吵中查看你的html源代码。我认为有data: { usid: $sess_user_id1}
,javascript不明白什么是$sess_user_id1
这是我现在能看到的唯一一个问题,但我不理解你目前的任务。