pass PHP variable value to jquery function

前端 未结 2 767
自闭症患者
自闭症患者 2021-01-23 07:39

This code is not working properly. What i want is just send the variable $something to the page.php

What is the correct way to do this ? :

相关标签:
2条回答
  • 2021-01-23 08:17

    First of all, I think you have mistakenly mixed PHP and JavaScript. In your code the line:

    $something = "text";
    

    may be understood in two ways. If this is the whole code you have, then you are actually initializing JavaScript variable called $something. Later in the code you are trying to use the value of PHP variable called $something.

    What you need to do is to change the code into (assuming you want to pass variable from PHP):

    <?php $something = "text"; ?>
    
    $.ajax({
    url: 'page.php',
    type: 'post',
    dataType: 'html',
    data: '<? php $something; ?>', 
    success: function (data) {
        $('#total').load('xxx.php');
    }
    });
    

    or into (assuming you want JS variable):

    var $something = 'text';
    
    $.ajax({
    url: 'page.php',
    type: 'post',
    dataType: 'html',
    data: $something, 
    success: function (data) {
        $('#total').load('xxx.php');
    }
    });
    
    0 讨论(0)
  • 2021-01-23 08:22

    myFile.php:

    <?php $something = 'text'; ?>
    
    <script>
    $.ajax({
      url: "page.php",
      type: "post",
      dataType: "html",
      data: '<?php echo $something; ?>', 
      success: function (data) {
        $('#total').load('xxx.php');
      }
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题