简体   繁体   English

将变量从jQuery传递给PHP

[英]Pass variable from jQuery to PHP

I created a script to read a hash (www.website.com/#hash) and pass the hash in to php. 我创建了一个脚本来读取哈希值(www.website.com/#hash)并将哈希值传递给php。 The alert(hash) pops up with the hash value, but the hash is not being echoed out in the post variable. alert(hash)弹出哈希值,但哈希值不会在post变量中回显。 Any idea why? 知道为什么吗?

jQuery page jQuery页面

<script>
    var hash = location.hash;
    $.post("community.php", {
        hash: hash
    });
    alert(hash);
</script>

Community.php page Community.php页面

<?php echo $_POST['hash']; ?>



Edits - $_GET below was originally $_POST above. 编辑 - 下面的$ _GET最初是$ _POST以上。

I have a foreach that is looping through postings (posting IDs) in a function. 我有一个循环通过在函数中发布(发布ID)的foreach。 I need to pass the HASH into the function and compare it to the posting IDs everytime. 我需要将HASH传递给函数并每次将其与发布ID进行比较。 Only problem is, the $_GET['hash'] will not show up inside the function. 唯一的问题是,$ _GET ['hash']不会出现在函数内部。

function something() {
   echo $_GET['hash'];
}

use ajax like this, send the value in hash 像这样使用ajax,以hash形式发送值

  function send_hash(hash) {
    $.ajax({
      url   : "community.php", 
      type  : "POST",
      cache : false,
      data  : {
        hash : hash
      }
    });
  }

now u will get 现在你会得到的

<?php echo $_POST['hash']; ?>
<script>
    var hash = location.hash;
    $.post("community.php", {
        hash: hash
    }, function(responde){ alert(responde); });
</script>

To check what your PHP response :) 检查你的PHP响应:)

$.post is an ajax event. $ .post是一个ajax事件。 You are posting data by ajax so by that the page doesn't go to the communitypage.php. 您正在通过ajax发布数据,因此该页面不会转到communitypage.php。 For the behaviour you want you have to do normal form post and not ajax post. 对于你想要的行为你必须做普通的表格发布而不是ajax发布。 $.post will retrive anything you echo on communitypage.php using this code. $ .post将使用此代码检索您在communitypage.php上回显的任何内容。

//Note the 3rd argument is a callback.
var hash = location.hash;
$.post('communitypage.php',{hash:hash},function(data){
    alert(data);
});

Process hash on that page and alert something if u find the hash. 在该页面上处理哈希并在找到哈希值时发出警告。 You'd find the echo in the data 你会在data找到回声

You could mod the communitypage.php to return html as below 您可以修改communitypage.php以返回html,如下所示

<?php
if($isset($_POST["hash"]) && strlen(trim($_POST["hash"])) > 0){
    echo "Hash found :: " . $_POST["hash"]; 
}else
    echo "No hash found";
?>

Note this will return html you can modify the response to return xml, json as per your needs. 注意这将返回html,您可以根据需要修改响应以返回xml,json。

Refer jQuery post() documentation for more info. 有关详细信息,请参阅jQuery post()文档

For the updated question 对于更新的问题

It works outside because it is called when the ajax call is made inside a function it won't because the function is not called. 它在外面工作,因为当ajax调用是在函数内部调用时调用它,因为函数没有被调用。 If you want to do that (and i don't know why) u can do like this. 如果你想这样做(我不知道为什么),你可以这样做。

function myfunction(){
    //your code goes here
}

myfunction(); //call the function so that the code in the function is called upon

Though that's an overkill unless u want to call the function over and over in the same script. 虽然这是一种矫枉过正,除非你想在同一个脚本中反复调用该函数。 If it works without a function you should do it that way. 如果它在没有功能的情况下工作,你应该这样做。

  <script>
  var hash = location.hash;
   //Try this.
   window.location.href = 'community.php?val='+hash;
  </script>

OR 要么

   <script>
   $.post("community.php","val="+hash);
   </script>

In community.php 在community.php中

     $res=isset($_REQUEST['val'])?$_REQUEST['val']:'';

hope it will give you some solution. 希望它会给你一些解决方案。

but the hash is not being echoed out in the post variable 但是在post变量中没有回显哈希

This is because if you want to send it when page loads then you have to use this in doc ready handler: 这是因为如果你想在页面加载时发送它,那么你必须在doc ready处理程序中使用它:

<script>
  $(function(){
     var hash = location.hash;
     $.post("community.php", { hash: hash });
     alert(hash);
  });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM