gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:43:13 26 4
gpt4 key购买 nike

我有一个网站,人们可以在其中搜索音乐,并且搜索记录会附有他们的 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>

最佳答案

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

关于php - 使用 javascript 返回带有计数的 php 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7560761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com