【问题标题】:Access the php value on the same page using jquery and ajax使用jquery和ajax访问同一页面上的php值
【发布时间】:2018-04-25 18:46:07
【问题描述】:

我正在尝试使用 jquery 和 ajax 回显用户在输入标记中输入的值。 以下是我的代码

< script src = "//code.jquery.com/jquery-1.11.0.min.js" > < /script>     <
  script type = "text/JavaScript" >
  function showMessage() {
    var message = document.getElementById("message").value;
    $.ajax({
      url: 'value_pass.php',
      type: 'GET',
      data: {
        var_PHP_data: message
      },
      success: function(data) {
        alert("success");
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        //case error
      }
    });

  } <
  /script>
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  Enter message: <input type="text" id="message">
  <input type="submit" onclick="showMessage()" value="submit" />
  <?php echo "hi".$_GET['var_PHP_data'];?>
</body>

</html>

我的php脚本是:

<?php 
echo "hi".$_GET['var_PHP_data'];
?>

代码提示成功提示,但我无法使用 GET 访问该值,任何关于我做错了什么的指针。

【问题讨论】:

  • 你也可以发布你的php脚本
  • &lt;?php echo "hi".$_GET['var_PHP_data'];?&gt;
  • 您是否尝试使用表单提交或 ajax 获得价值?
  • 我正在尝试使用 ajax 获取变量内的值

标签: javascript jquery html ajax web


【解决方案1】:

如果您在同一页面上执行此操作,请将您的 php 脚本添加到处理您的 ajax 请求的页面顶部

这样

<?php if(isset($_GET['var_PHP_data'])){
  echo "hi".$_GET['var_PHP_data'];
  die;
}?>

<!-- rest of your html code start from here -->
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  Enter message: <input type="text" id="message">
  <input type="submit" onclick="showMessage()" value="submit" />
</body>

</html>

【讨论】:

    【解决方案2】:

    如果您使用 ajax,请按照以下代码进行操作

    <!DOCTYPE html>
    <html>
    
    <head>
    </head>
    
    <body>
      Enter message: <input type="text" id="message">
      <input type="submit" onclick="showMessage()" value="submit" />
     <span id = 'PHP_data'></span>
    </body>
    
    </html>
    
    
    <script src = "//code.jquery.com/jquery-1.11.0.min.js" > < /script>
      <script type = "text/JavaScript" >
      function showMessage() {
        var message = document.getElementById("message").value;
        $.ajax({
          url: 'value_pass.php',
          type: 'GET',
          data: {
            var_PHP_data: message
          },
          success: function(data) {
          message1 = "Hi "+message
           $('#PHP_data').html(message1);
            alert("success");
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            //case error
          }
        });
    
      } 
      </script>
    

    如果您使用的是表单提交,请使用以下代码

    <!DOCTYPE html>
    <html>
    
    <head>
    </head>
    
    <body>
      <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method ='GET'>
      Enter message: <input type="text" id="message" name = "message">
      <input type="submit" value="submit" />
     <?php if(isset($_GET['message'])){
     echo "hi".$_GET['message'];
      die;
    } ?>
     </form>
    </body>
    
    </html>
    

    【讨论】:

    • 上面的代码工作得很好,但不是在 span 标签中获取值,我怎样才能在变量中获取它.. 我试过 $_GET 但没有成功
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    相关资源
    最近更新 更多