0

我在下面使用一个简单的 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>

我的PHP文件.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); });

我已经$_SESSION在 myPHPfile.php 以及所有 jQuery AJAX 函数中尝试过: load(),和方法get(),它们都没有在 JavaScript 中给出 PHP 值。post()ajax()

我翻遍了所有地方,但找不到答案。

4

4 回答 4

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>
于 2013-01-10T16:12:22.407 回答
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);

                });
于 2013-01-10T16:18:10.577 回答
0

尝试:

alert('<?php echo $buttonPHP; ?>');
于 2013-01-10T16:11:34.260 回答
0

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

索引.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

于 2013-01-10T16:25:30.590 回答