doulan8846 2015-11-06 06:21
浏览 36
已采纳

如何使用从javascript传递的变量加载PHP页面

I'm new to PHP and am trying to figure something out that I'm sure is very basic. What I am wanting to do is generate variables in javascript and pass these to a PHP page which then loads and displays. The problem is, I seem to be able to both POST variables to the PHP page and load the PHP page but am unable to load the PHP page with the variables that I POSTed.

Here is an example of my code: index.php

...
<script language="javascript">
      function passToPHP(){
          $.ajax({
              type: "POST",
              url: "/TraceExperiment/TraceExperiment.php",
              data: {
                  varToPass: "foo"
              },
              success: function(){
                 window.location.href="/TraceExperiment/TraceExperiment.php";
              }
          })
      }
  </script>
<input type="button", value="displayPHP", onclick="passToPHP()"></input>

TraceExperiment.php

<?php
  $tempVar = $_POST["varToPass"];
  echo("hello" . $tempVar);
  print_r($_POST);
?>

What is happening when I click displayPHP is that the ajax POST succeeds and TraceExperiment.php loads fine (it actually has a whole heap of other html, php etc. code that loads and displays fine but for simplicity I've excluded that) but the $_POST array seems to be empty. i.e. what ends up being displayed when I try to echo and print the POST array and variables is:

Notice: Undefined index: varToPass in C:\xampp\htdocs\TraceExperiment\TraceExperiment.php on line 3 helloArray ( )

Any help resolving this would be much appreciated. Ultimately, I'm simply after a way to display a PHP page that uses variables passed from a javascript file.

  • 写回答

4条回答 默认 最新

  • duanmian1085 2015-11-06 06:46
    关注

    You can do a get request like this

     <script language="javascript">
              function passToPHP(){
                  var varToPass= "foo"
                  window.location = "/TraceExperiment/TraceExperiment.php?varToPass="+varToPass;
          </script>
        <input type="button", value="displayPHP", onclick="passToPHP()"></input>
    
        <?php
          $tempVar = $_GET["varToPass"];
          echo("hello" . $tempVar);
        ?>
    

    or a post request by creating a simple form

    $('#frm').submit(function(e){
      var varToPass= "foo"
       e.preventDefault();
        $(this).find('#varToPass').val(varToPass);
        $(this).submit();
    });
    
    <form id ="frm" method="POST" action="/TraceExperiment/TraceExperiment.php">
      <input type="hidden" id="varToPass"/>
      <input type="submit"/>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看