在php中使用javascript数据错误

时间:2016-02-02 05:22:06

标签: javascript php ajax

使用这个ajax代码我将数据speedMbps从我的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) {

                }

6 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$.ajax({
          method: "POST",
          url: "viewvideo.php",
          data: {"speedMbps" : speedMbps,"video_id":video_id },
          cache: false
        }).done(function( html ) {
            $( "#speed" ).val( html );
    });

答案 1 :(得分:0)

这里检查两个带有AND条件的varibales,但只传递一个变量。因此,您可以将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 },

答案 2 :(得分:0)

你需要将speedMbps放在单引号中。

data: {'speedMbps': speedMbps },

答案 3 :(得分:0)

您需要发送带有AJAX请求的video_id,或者您可以删除php中的条件并检查,因为在php中您正在检查是否设置了video_idif(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
   }
}

答案 4 :(得分:0)

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 );
    });

答案 5 :(得分:-1)

试试这个:

$.ajax({
      method: "POST",
      url: "viewvideo.php",
      data: {speedMbps: "speedMbps" },
      cache: false
    }).done(function( html ) {
        $( "#speed" ).val( html );
});