用PHP表单更改href链接 [英] Change href link with php form

查看:109
本文介绍了用PHP表单更改href链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个带有搜索栏的网站.我想使搜索栏搜索"并显示结果后变为交互式.因此,我希望href根据所使用的ID进行变更.例如:有人搜索"Pinecones",如果它在数据库中将有一个ID,在本例中为#4.一旦他们搜索了它,它将显示为链接.但我希望链接使用"/#IDNumber.php"

I'm making a website with a search bar. I want to make the search bar interactive once it has "searched" and shown the results. So I want the href to chnage as per what Id is being used. For example: Someone searches "Pinecones", if its in the database it'll have an ID, for this example its #4. Once they searched it, it'll show up as a link. But I want the link to use "/#IDNumber.php"

这是我使用的代码:

<?php
$output = '';
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$query = mysql_query("SELECT * FROM `findgame` WHERE name LIKE '%$searchq%'       OR keywords LIKE '%$searchq%' LIMIT 1") or die("Search unavailable.");
$count = mysql_num_rows($query);
if($count == 0){
    $output = 'Results not found.';

}else{
    while($row = mysql_fetch_array($query)) {
        $name = $row['name'];
        $kwords = $row['keywords'];
        $id = $row['id'];

        $output .= '<div style="position:absolute;margin:110px    20px;padding:25px;">'.$id.' '.$name.'</div>';
        }

}

}

?>

<a href="/<?php$id?>.php">    <?php print("$output");?></a>

在使它正常工作方面有帮助吗?

Any help in making it work?

推荐答案

您需要打印变量.

$id = 123;

<?php $id ?>      =>
<?php echo $id ?> => 123
<?= $id ?>        => 123

所以最终结果将是这样:

So the final result would be something like:

<a href="/<?= $id ?>.php">
    <?php print($output); ?>
</a>

注意:您不需要$output周围的".不会受伤,但这不是必需的.

Note: You don't need the " around $output. It won't hurt, but it's not necessary.

这篇关于用PHP表单更改href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆