使用 javascript 返回带有计数的 php 元素

发布于 2024-12-06 18:15:27 字数 2177 浏览 0 评论 0原文

我有一个网站,人们可以在其中搜索音乐,并且搜索记录会附有他们的 ID。我正在尝试用它来制作饲料,到目前为止它已经奏效了。然而,提要包含一个 JavaScript 函数,它将搜索放入一个链接中,单击该链接后,会填充搜索字段并完成搜索。问题是我希望每个链接都具有特定的搜索,但最终发生的情况是将 php 变量的值设置为数组中的最后一个搜索。我现在想做的是使用计数器设置 php 搜索变量,以便稍后在 javascript 中调用该变量。我知道这听起来非常令人困惑/不清楚,但我正在尽力而为。也许代码会有所帮助。

    $sql_userSearch = mysql_query("SELECT id, searchstring, timecount, userSearch FROM trending WHERE userSearch != 0 ORDER BY timecount DESC LIMIT 50");


    $counter = 1; 
    while($row = mysql_fetch_array($sql_userSearch)){

$search_id = $row["id"];
$searcher_id = $row["userSearch"];
$the_search[$counter] = $row["searchstring"];
$search_date = $row["timecount"];
$convertedTime = ($myObject -> convert_datetime($search_date));
$whenSearch = ($myObject -> makeAgo($convertedTime));
$search_date = $row["timecount"];


$searchDisplayList .= 
'<table style="border:#999 1px solid; border-top:none;" background="img/searchBG.png" cellpadding="5" width="100%">
 <tr>
    <td width="10%" valign="top"><img src="https://graph.facebook.com/'. $searcher_id . '/picture" width="35" height="35"></td>
    <td width="90%" valign="top" style="line-height:1.5em;">
    <span class="liteGreyColor textsize9"><strong>' . $searcher_id . '</strong></a> searched for </span>
    <a href="#" onClick="swapSearch()">' . $the_search[$counter] . '</a></br> ' . $whenSearch . ' 
    </td>
    </tr></table>';


$counter++;
}

然后稍后,这是我用来返回链接的 javascript。

     <script type="text/javascript">
    function swapSearch(){
        document.getElementById('s').value = "<?= $the_search[$counter] ?>";
        $('#searchForm').submit();  
    }
    </script>

编辑:

我刚刚最终使用了一些 JavaScript 来抓取链接的文本并填充搜索表单。比我想做的容易得多。

   <script type="text/javascript">
   $("a.friendSearchLink").live("click",function(a) {       
a.preventDefault();                         
var searchTerm = $(this).text();
document.getElementById('s').value = searchTerm;
$("#searchForm").submit();                  
    });
    </script>

I have a site where people search for music and the search is recorded with their id attached to it. I am trying to make a feed out of it and it has worked thus far. However, the feed includes a javascript function where it makes their search into a link which when clicked, populates the search field and completes the search. The problem is I am looking to make each link it's specific search, but what ends up happening is it sets the value of the php variable to the last search in the array. What I'm now trying to do is set the php search variable with an counter to call that later in javascript. I know this sounds super confusing / not clear, but I'm trying my best. Maybe the code will help.

    $sql_userSearch = mysql_query("SELECT id, searchstring, timecount, userSearch FROM trending WHERE userSearch != 0 ORDER BY timecount DESC LIMIT 50");


    $counter = 1; 
    while($row = mysql_fetch_array($sql_userSearch)){

$search_id = $row["id"];
$searcher_id = $row["userSearch"];
$the_search[$counter] = $row["searchstring"];
$search_date = $row["timecount"];
$convertedTime = ($myObject -> convert_datetime($search_date));
$whenSearch = ($myObject -> makeAgo($convertedTime));
$search_date = $row["timecount"];


$searchDisplayList .= 
'<table style="border:#999 1px solid; border-top:none;" background="img/searchBG.png" cellpadding="5" width="100%">
 <tr>
    <td width="10%" valign="top"><img src="https://graph.facebook.com/'. $searcher_id . '/picture" width="35" height="35"></td>
    <td width="90%" valign="top" style="line-height:1.5em;">
    <span class="liteGreyColor textsize9"><strong>' . $searcher_id . '</strong></a> searched for </span>
    <a href="#" onClick="swapSearch()">' . $the_search[$counter] . '</a></br> ' . $whenSearch . ' 
    </td>
    </tr></table>';


$counter++;
}

then later on, this is the javascript I am using to return the link.

     <script type="text/javascript">
    function swapSearch(){
        document.getElementById('s').value = "<?= $the_search[$counter] ?>";
        $('#searchForm').submit();  
    }
    </script>

EDIT:

I just ended up using some javascript that grabbed the text of the link and populated the search form. Way easier than what I was trying to do.

   <script type="text/javascript">
   $("a.friendSearchLink").live("click",function(a) {       
a.preventDefault();                         
var searchTerm = $(this).text();
document.getElementById('s').value = searchTerm;
$("#searchForm").submit();                  
    });
    </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九局 2024-12-13 18:15:27

PHP 是经过预处理的,您只能在页面发送到客户端之前回显一个值。为了完成您想做的事情,您必须向包含您要查找的信息的页面发送 ajax 调用,否则您的输出将是完全静态的。

PHP is preprocessed, you can only echo a value prior to the page being sent to your client. In order to do what you're wanting to do, you must send an ajax call to a page that contains the information you're looking for, otherwise your output will be completely static.

浅唱ヾ落雨殇 2024-12-13 18:15:27

您可以将 $the_search 中的所有搜索存储到 JS 数组中,然后将 $counter 传递给 swapSearch('.$counter.') 。然后你可以在js中查找任何搜索内容。

我唯一的另一个问题是您是否将搜索表单作为帖子提交?因为如果将其更改为从 $_GET 读取,则只需设置 href="?q=$the_search[$counter]" 即可使用该链接来运行搜索。另一种选择是在 url 中传递搜索 ID,然后使用 php 加载搜索结果/关键字。

you could store all the searches from $the_search into a JS array and then pass in $counter to swapSearch('.$counter.'). then you can look up in js whatever the search is.

The only other question I have though is are you submitting your search form as a post? Because if you change it to read from $_GET, you can just use the link to run the search just by setting href="?q=$the_search[$counter]". Other option is to pass the search id in the url and just use php to load the search result/keywords.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文