简体   繁体   English

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

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

This is my javascript. 这是我的javascript。 I want to get jquery variable value to php variable in same page using ajax. 我想使用ajax将jQuery变量值转换为同一页面中的php变量。 I used to print php variable but there is an error. 我曾经打印过php变量,但是有错误。 I would be very grateful if anyone can help me. 如果有人可以帮助我,我将不胜感激。

<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>

You should add PHP somewhat like this: 您应该这样添加PHP:

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

?>

Adding edits as per the comments from question author. 根据问题作者的评论添加编辑内容。

Since you are using the same page for posting data using jquery, you should do a refresh of the page. 由于您使用同一页面通过jquery发布数据,因此应刷新页面。 Modify the code like this for the ajax call ( javascript part ). 像这样为ajax调用修改代码(javascript部分)。

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

And add this to the top of your pages code. 并将其添加到页面代码的顶部。

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

BTW, your total variable can be accessed only by the change method anonymous callback function!. 顺便说一句,您的total变量只能通过更改方法匿名回调函数来访问! total will be undefined in your ajax call. 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
        }
    });
});

You should declare your 'total' variable in javascript before your function. 您应该在函数之前用JavaScript声明“ total”变量。

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

Or get it before ajax init like that 或者像这样在ajax init之前获取它

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

You can access your var in php 您可以在php中访问您的var

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

You can see what inside total before ajax send it 在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>

I think the problem is with your sequence of code. 我认为问题出在您的代码序列上。 The ajax call is made before document.ready is called. 在调用document.ready之前进行ajax调用。 If you convert it to something like this then may be it will work 如果您将其转换为类似的内容,则可能会起作用

<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