[英]Passing variable from JS using ajax to PHP undefined variable
I want to pass variable rating_index to the PHP code and send to database.我想将变量 rating_index 传递给 PHP 代码并发送到数据库。 Rating_index is set I'll not pass the code here because it is long, but right before AJAX i console.log(rating_index) and it has value(int).
Rating_index 已设置 我不会在此处传递代码,因为它很长,但就在 AJAX 之前 i console.log(rating_index) 并且它具有 value(int)。 Add-review is a button that after click should send variable.
添加评论是一个按钮,点击后应该发送变量。 In js script I am using AJAX:
在 js 脚本中,我使用的是 AJAX:
$('#add-review').click(function(){
$.ajax({
url:"rating-data.php",
method:"POST",
data: {
rating_index: rating_index
},
success:function(data)
{
console.log(data);
}
})
}
in my php file rating-data.php:在我的 php 文件 rating-data.php 中:
<?php
include 'connection.php';
echo "work";
echo $_POST["rating_index"];
?>
I got a console.log ('work') from PHP file and this error: Notice: Undefined index: rating_index in /Applications/XAMPP/xamppfiles/htdocs/bookwarm-app/rating-data.php on line 4我从 PHP 文件中得到了一个 console.log('work')并且出现了这个错误:注意:未定义的索引:/Applications/XAMPP/xamppfiles/htdocs/bookwarm-app/rating-data.php 中的 rating_index 第 4 行
So it is getting my to php page but the variable are not passing correctly.所以它让我转到 php 页面,但变量没有正确传递。
I was trying everything and I have no idea what is wrong and why this variable is undefined in php file.我尝试了一切,但我不知道出了什么问题,也不知道为什么这个变量在 php 文件中未定义。 Thanks for any clue
感谢您提供任何线索
This is the problem in your code, sir先生,这是您的代码中的问题
$user_name = $_GET["user_name"];
$user_rating = $_GET["rating_index"];
$user_review = $_GET["user_review"];
you're doing a $_POST request from Ajax and in php file you're getting values from $_GET.....?您正在执行来自 Ajax 的 $_POST 请求,而在 php 文件中您正在从 $_GET 获取值......?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.