我可以在JavaScript中使用PHP值吗?

时间:2013-01-10 16:07:39

标签: php javascript jquery ajax

我在下面使用一个简单的Jquery代码来调用PHP页面并在div中获取按钮信息:

<head>
<script>
  $(document).ready(function(){
    $('#myButtons input:radio').change(function() {
      var buttonValue = $("#myButtons input:radio:checked").val();
        $("#myDiv").load('myPHPfile.php', {selectedButtonValue : buttonValue});
    });
  });
</script>
</head>

<body>
  <div id="myButtons">
    <input type="radio" name="category" value="10" />ButtonA 
    <input type="radio" name="category" value="20" />ButtonB
  </div>
  <div id="myDiv">Click the button to load results</div>
</body>

myPHPfile.php

<?php 
  if( $_REQUEST["selectedButtonValue"] )
  {
     $buttonPHP = $_REQUEST['selectedButtonValue'];
     echo "Value button is ". $buttonPHP;
  }
?>

如何在JavaScript中获取PHP信息,如下所示:

alert(<?php echo('buttonPHP'); ?>);

注意:以下代码显示了警告框消息,但我无法使用它,因为我需要$ buttonPHP值:

$("#myDiv").load('myPHPfile.php', {selectedButtonValue : buttonValue}, function(data){ alert(data); });

我在myPHPfile.php中尝试了$_SESSION,以及所有jQuery AJAX函数:load()get()post()ajax()方法,他们都没有在JavaScript中提供PHP值。

我看了一遍,但找不到答案。

4 个答案:

答案 0 :(得分:2)

$.ajax请求会更好。不要回显'按钮的值是'部分,只是回显实际值。例如:

$.ajax({
  url: "myPHPfile.php",
  context: document.body
 }).done(function(data) {
   var buttonValue = data;
   // Whatever you want to do with the data goes here.
 });

请参阅jQuery文档http://api.jquery.com/jQuery.ajax/

或者,如果您使用PHP生成页面,只需将PHP变量回显到JavaScript

即可
<script>
 ...
    var buttonValue = "<?php echo $buttonPHP; ?>";
 ...
</script>

答案 1 :(得分:2)

使用JSON编码,而不是尝试回显数据:

<?php 
  if( $_GET["selectedButtonValue"] )
  {
     $buttonPHP = $_GET['selectedButtonValue'];
     header('Content-Type: application/json');
      echo json_encode($buttonPHP);

  }
?>

然后使用jquery get json来获取数据

$.get('myPHPfile.php?selectedButtonValue='+buttonvalue, function(data){
                    console.log(data);

                });

答案 2 :(得分:0)

尝试:

alert('<?php echo $buttonPHP; ?>');

答案 3 :(得分:0)

作为临时解决方案,您可以快速将两者合并为脏代码,如下所示:

的index.php:

<?php 

  $buttonPHP = "10"; //Set up your default button variable

  if( $_REQUEST["selectedButtonValue"] == 10 )
  {
     echo "Selected button value: 10";  //You can also print $_REQUEST["selectedButtonValue"] here directly, but what's the point if you can select it from DOM through jQuery?
     exit;
  } else if ($_REQUEST["selectedButtonValue"] == 20) {
     echo "Selected button value: 20";
     exit;
  }
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
  var buttonValue = <?php echo $buttonPHP ?>;  //Duplicate default value from PHP to JS
  $(document).ready(function(){
    $('#myButtons input:radio').change(function() {
      var buttonValue = $("#myButtons input:radio:checked").val();
        $("#myDiv").load('index.php', {selectedButtonValue : buttonValue});
    });
  });
</script>
</head>

<body>
  <div id="myButtons">
    <input type="radio" name="category" value="10" />ButtonA 
    <input type="radio" name="category" value="20" />ButtonB
  </div>
  <div id="myDiv">Click the button to load results</div>
</body>
</html>

index.php在浏览器中呈现:

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
  var buttonValue = 10;  //Duplicate default value from PHP to JS
  $(document).ready(function(){
    $('#myButtons input:radio').change(function() {
      var buttonValue = $("#myButtons input:radio:checked").val();
        $("#myDiv").load('index.php', {selectedButtonValue : buttonValue});
    });
  });
</script>
</head>

<body>
  <div id="myButtons">
    <input type="radio" name="category" value="10" />ButtonA 
    <input type="radio" name="category" value="20" />ButtonB
  </div>
  <div id="myDiv">Click the button to load results</div>
</body>
</html>

编辑:存储会话

<?php 

  session_start();

  if (isset($_REQUEST['selectedButtonValue'])) {
      $_SESSION['selectedButtonValue'] = $_REQUEST['selectedButtonValue'];
      echo $_REQUEST['selectedButtonValue'];
      exit;
  } else if (!isset($_SESSION['selectedButtonValue'])) {
      $_SESSION['selectedButtonValue'] = 10;
  }

?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
  var buttonValue = <?php echo $_SESSION['selectedButtonValue']; //Get input value from session ?>;
  $(document).ready(function(){
    $('#myButtons input:radio').change(function() {
      var buttonValue = $("#myButtons input:radio:checked").val();
        $("#myDiv").load('index.php', {selectedButtonValue : buttonValue});
    });
  });
</script>
</head>

<body>
  <div id="myButtons">
    <input type="radio" name="category" value="10" />ButtonA 
    <input type="radio" name="category" value="20" />ButtonB
  </div>
  <div id="myDiv"><?php echo $_SESSION['selectedButtonValue']; //Get input value from session ?></div>
</body>
</html>

还要确保过滤$ _REQUEST变量,因为到目前为止,它会打印任何内容。有关详细信息,请参阅http://en.wikipedia.org/wiki/Cross-site_scripting