繁体   English   中英

如何将jquery变量值传递给php变量?

[英]How to pass jquery variable value to php variable?

这是我的javascript。 我想使用ajax将jQuery变量值转换为同一页面中的php变量。 我曾经打印过php变量,但是有错误。 如果有人可以帮助我,我将不胜感激。

<script type="text/javascript">  
$(document).ready(function() {
    $('#roomOptions #select1').change(function() {
        var total = 0;
        $('#roomOptions #select1').each(function() {
            total+=parseInt($(this).val());    
        });
        $('#roomOptions #roomOptions_total').html(total);
        });         

    $('#roomOptions #select2').change(function() {
        var total1 = 0;
        $('#roomOptions #select2').each(function() {
            total1+=parseInt($(this).val());    
        });
        $('#roomOptions #roomOptions_total1').html(total1);
    });
});

$.ajax({
    type: "POST",
    url: "getrooms.php",
    data:{ total: total }, 
    success: function(data){
        console.log(data); 
    }
})
</script>

您应该这样添加PHP:

<?php
$total = $_POST['total']  ;
echo $total;

?>

根据问题作者的评论添加编辑内容。

由于您使用同一页面通过jquery发布数据,因此应刷新页面。 像这样为ajax调用修改代码(javascript部分)。

$.post(window.location, {title: title}, success: function(data) {
  //do something here
});

并将其添加到页面代码的顶部。

 <?php
  if(isset($_POST['total'])) {
  echo $_POST['total'];
  }
 ?>

顺便说一句,您的total变量只能通过更改方法匿名回调函数来访问! total将在您的ajax调用中undefined

$('#roomOptions #select1').change(function () {
    var total = 0;
    $('#roomOptions #select1').each(function () {
        total += parseInt($(this).val());
    });

    $('#roomOptions #roomOptions_total').html(total);

    $.ajax({
        url: getrooms.php,
        type: 'POST',
        data: {
            "total": total
        }
    });
});

您应该在函数之前用JavaScript声明“ total”变量。

var total = 0;
$('#roomOptions #select1').change(function () {
    total = 0;
    $('#roomOptions #select1').each(function () {
        total += parseInt($(this).val());
    });
    $('#roomOptions #roomOptions_total').html(total);
});

或者像这样在ajax init之前获取它

  <script> 
    total = $("#roomOptions #roomOptions_total").html();
    $.ajax({
        type: "POST",
        url: "getrooms.php",
        data:{ total: total }, 
        success: function(data){
            console.log(data); 
        }
    })
    </script>

您可以在php中访问您的var

<?php echo $_POST['total']; ?>

在ajax发送之前,您可以查看里面的总计

 <script> 
        total = $("#roomOptions #roomOptions_total").html();
        $.ajax({
            type: "POST",
            url: "getrooms.php",
            data: { total: total }, 
            success: function(data){
                console.log(data); 
            },
beforeSend: function() {
     console.log(total); 
  }
        })
        </script>

我认为问题出在您的代码序列上。 在调用document.ready之前进行ajax调用。 如果您将其转换为类似的内容,则可能会起作用

<script type="text/javascript">  
$(document).ready(function() {
     var total = 0;
    $('#roomOptions #select1').change(function() {
         total = 0;
        $('#roomOptions #select1').each(function() {
            total+=parseInt($(this).val());    
        });
        $('#roomOptions #roomOptions_total').html(total);
        });         

    $('#roomOptions #select2').change(function() {
        var total1 = 0;
        $('#roomOptions #select2').each(function() {
            total1+=parseInt($(this).val());    
        });
        $('#roomOptions #roomOptions_total1').html(total1);
    });


$.ajax({
    type: "POST",
    url: "getrooms.php",
    data:{ total: total }, 
    success: function(data){
        console.log(data); 
    }
})
});
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM