简体   繁体   English

通过将变量传递给 jquery,无需重新加载即可获取数据

[英]fetch data without reloading by passing a variable to jquery

I want to fetch some data (without reloading page) based on UserIds that are linked to a tags.我想根据链接到标签的 UserId 获取一些数据(无需重新加载页面)。 I am not able to pass on UserID to jquery successfully and accurately.我无法成功且准确地将 UserID 传递给 jquery。 What it does is, just picks the last UserID and fetches it.它所做的只是选择最后一个 UserID 并获取它。

I have also tried to pass on variable throuh a-tag URL but could not get it accurately in jquery file.我还尝试通过 a-tag URL 传递变量,但无法在 jquery 文件中准确获取它。 It fetches the data from already opened url, not the one being clicked right now.它从已经打开的 url 中获取数据,而不是现在点击的那个。

HTML: HTML:

<?php foreach($Messagers as $Messagers1){ ?>
  <form action="" method="post">
    <input type="hidden" id="ANP" value="<?php echo ($Messagers1['UserID']*3);?>"></input>                  
      <a  class="LoadMsgsBtn">
          Click to load data 
       </a>
  </form>
<?php}?>
<div id="result">
  <--The output will come here!-->
</div>

jquery:查询:

 $(document).ready(function(){
   $(".LoadMsgsBtn").click(function LoadMsgsfunc(){
    var ANP = $("input#ANP").val(); 
    var Msg=6;
    alert(ANP);
    $("#result").load("LoadDB.php",{
        ANP: ANP,
        Msg: Msg
    });
  })
});

LoadDB.php:加载数据库.php:

<?php
    $ID = $_POST['ANP'];
    $Msg = $_POST['Msg'];
    echo $ID . "\n";
    echo $Msg;

I want to fetch data from database without reloading using UserID sent.我想从数据库中获取数据而不使用发送的 UserID 重新加载。 $Messager is an array having multiple rows, there is a series of a-tags. $Messager 是一个多行的数组,有一系列的 a 标签。 clicking on any a-tag sents corresponding UserID to jquery code单击任何 a-tag 将相应的 UserID 发送到 jquery 代码

Classic issue of multiple element selection.多元素选择的经典问题。 It's a bit tricky when you are using loops and jquery selectors ;) Check out the following code.当您使用循环和 jquery 选择器时,这有点棘手;) 查看以下代码。 It should work like butter B)它应该像黄油 B)

<?php foreach($Messagers as $Messagers1){ ?>
  <form action="" method="post">
      <a class="LoadMsgsBtn" href="javascript:void(0)" onclick="featchData('<?php echo ($Messagers1['UserID']*3);?>')">
          Click to load data 
       </a>
  </form>
<?php}?>
<div id="result">
  <--The output will come here!-->
</div>

Javascript Javascript

function featchData(userId)
{
    console.log(userId);
    //if you get the id, write in the rest of the code here ;)
}

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

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