使用这个ajax代码,我正在将数据速度Mbps从我的javascript传递给php。但是,当我尝试在PHP中使用speedMbps时,我得到了一个未定义的索引:speedMbps。我应该怎么做才能在PHP中使用SpeedMbps?
已编辑:video_id是使用 html 表单发布发送的。
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {speedMbps: speedMbps },
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
PHP 文件
if(isset($_POST['video_id']) && ( $_POST['speedMbps'] )){
$speed = $_POST['speedMbps'];
if ($speed < 100) {
}
尝试这样的事情:
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {"speedMbps" : speedMbps,"video_id":video_id },
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
在这里,您可以使用AND
条件检查两个变量,但只传递一个变量。 因此,您可以将AND
条件更改为OR
并尝试一下。
if(isset($_POST['video_id']) || isset( $_POST['speedMbps'] )){ //missing isset function here. also change AND to OR
或者你必须在ajax函数中传递video_id像这样:
data: {speedMbps: speedMbps, video_id:video_id },
您需要
将 speedMbps 放在单引号中。
data: {'speedMbps': speedMbps },
var speedMbps ='' //here is Your value for speedMbps
var videoid = //here is Your value for video id
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {speedMbps: speedMbps ,videoid:videoid},
cache: false,
success:(function( html ) {
$( "#speed" ).val( html );
});
您要么需要使用 AJAX 请求发送video_id
,要么可以在 php 中删除条件并检查,因为在 php 中您正在检查是否设置了video_id
,if(isset($_POST['video_id']) && ( $_POST['speedMbps'] )){
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {
speedMbps: speedMbps,
video_id: VIDEO_ID_PUT_HERE //`video_id: $('[name="video_id"').val()`
},
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
在你的 PHP 中:
if(isset($_POST['video_id']) && isset($_POST['speedMbps'] )){
$speed = $_POST['speedMbps'];
if ($speed < 100) {
//do your stuff
}
}
试试这个:
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {speedMbps: "speedMbps" },
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});