php 你如何通过链接发布数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/426310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you post data with a link
提问by Howard May
I have a database which holds the residents of each house in a certain street. I have a 'house view' php web page which can display an individual house and residents when given the house number using 'post'. I also have a 'street view' web page which gives a list of houses. What I want to know is if you can have links on the street view which will link to the house view and post the house number at the same time without setting up a form for each?
我有一个数据库,其中包含某条街道上每栋房屋的居民。我有一个“房屋视图”php 网页,当使用“post”给出门牌号时,它可以显示单个房屋和居民。我还有一个“街景”网页,其中提供了房屋列表。我想知道的是,您是否可以在街景上设置链接,该链接将链接到房屋视图并同时发布门牌号,而无需为每个视图设置表格?
Regards
问候
采纳答案by Click Upvote
I assume that each house is stored in its own table and has an 'id' field, e.g house id. So when you loop through the houses and display them, you could do something like this:
我假设每个房子都存储在它自己的表中,并且有一个“id”字段,例如房子 id。因此,当您遍历房屋并显示它们时,您可以执行以下操作:
<a href="house.php?id=<?php echo $house_id;?>">
<?php echo $house_name;?>
</a>
Then in house.php, you would get the house id using $_GET['id'], validate it using is_numeric()and then display its info.
然后在 house.php 中,您将使用 获取房屋 ID,使用$_GET['id']验证它is_numeric(),然后显示其信息。
回答by Ben
If you want to pass the data using POST instead of GET, you can do it using a combination of PHP and JavaScript, like this:
如果您想使用 POST 而不是 GET 传递数据,您可以使用 PHP 和 JavaScript 的组合来完成,如下所示:
function formSubmit(house_number)
{
document.forms[0].house_number.value = house_number;
document.forms[0].submit();
}
Then in PHP you loop through the house-numbers, and create links to the JavaScript function, like this:
然后在 PHP 中循环遍历门牌号,并创建指向 JavaScript 函数的链接,如下所示:
<form action="house.php" method="POST">
<input type="hidden" name="house_number" value="-1">
<?php
foreach ($houses as $id => name)
{
echo "<a href=\"javascript:formSubmit($id);\">$name</a>\n";
}
?>
</form>
That way you just have one form whose hidden variable(s) get modified according to which link you click on. Then JavasScript submits the form.
这样你就只有一个表单,它的隐藏变量会根据你点击的链接进行修改。然后 JavaScript 提交表单。
回答by Sergey Kuznetsov
You cannot make POST HTTP Requests by <a href="some_script.php">some_script</a>
您不能通过以下方式发出 POST HTTP 请求 <a href="some_script.php">some_script</a>
Just open your house.php, find in it where you have $house = $_POST['houseVar']and change it to:
只需打开您的house.php,在其中找到您拥有的位置$house = $_POST['houseVar']并将其更改为:
isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']
isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']
And in the streeview.phpmake links like that:
在这样的streeview.phpmake 链接中:
<a href="house.php?houseVar=$houseNum"></a>
<a href="house.php?houseVar=$houseNum"></a>
Or something else. I just don't know your files and what inside it.
或者是其他东西。我只是不知道你的文件和里面有什么。
回答by brook
This is an old thread but just in case anyone does come across i think the most direct solution is to use CSS to make a traditional form look like an anchor-link.
这是一个旧线程,但以防万一有人遇到我认为最直接的解决方案是使用 CSS 使传统表单看起来像锚链接。
@ben is correct you can use php and javascript to send a post with a link, but lets ask what the js does -- essentially it creates a form with style='display:none' sets an input/text line with value='something' and then submits it.
@ben 是正确的,您可以使用 php 和 javascript 发送带有链接的帖子,但是让我们问问 js 是做什么的——本质上它创建了一个带有 style='display:none' 的表单,设置了一个带有 value=' 的输入/文本行东西'然后提交它。
however you can skip all this by making a form. setting style='display:none' on the input/text lines (not the form itself as above) and then using CSS to make the button look like a normal link.
但是,您可以通过制作表格来跳过所有这些。在输入/文本行上设置 style='display:none'(不是上面的表单本身),然后使用 CSS 使按钮看起来像一个普通的链接。
here is an example is i use:
这是我使用的一个例子:
in PHP Class,
在 PHP 类中,
public function styleButton($style,$text){
$html_str = "<form id='view_form' action='".$_SERVER['REQUEST_URI']."' method='post' >";
$html_str .= "<input style='display:none;' name='list_style' type='text' value='".$style."' >";
$html_str .= "<input id='view_button' type='submit' value='".$text."' >";
$html_str .= "</form>";
return $html_str;
}
Then in the CSS id="view_form" set "display:inline;" and in the CSS id="view_button" set to something like: "background:none;border:none;color:#fff;cursor:pointer"
然后在 CSS id="view_form" 中设置 "display:inline;" 并在 CSS id="view_button" 设置为类似:"background:none;border:none;color:#fff;cursor:pointer"
回答by Jon Tackabury
I would just use a value in the querystring to pass the required information to the next page.
我只会使用查询字符串中的一个值将所需的信息传递到下一页。
回答by Pixeldroid Modding
We should make everything easier for everyone because you can simply combine JS to PHP Combining PHP and JS is pretty easy.
我们应该让每个人都更轻松,因为您可以简单地将 JS 与 PHP 结合使用 结合 PHP 和 JS 非常容易。
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = $house_number; document.forms[0].submit();</script>";
Or a somewhat safer way
或者更安全的方式
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = " . $house_number . "; document.forms[0].submit();</script>";
回答by Rahul Agrawal
This post was helpful for my project hence I thought of sharing my experience as well. The essential thing to note is that the POST request is possible only with a form. I had a similar requirement as I was trying to render a page with ejs. I needed to render a navigation with a list of items that would essentially be hyperlinks and when user selects any one of them, the server responds with appropriate information.
这篇文章对我的项目很有帮助,因此我也想分享我的经验。需要注意的基本事项是 POST 请求只能使用表单。我有一个类似的要求,因为我试图用 ejs 渲染一个页面。我需要使用一系列项目呈现导航,这些项目本质上是超链接,当用户选择其中任何一个时,服务器会以适当的信息进行响应。
so I basically created each of the navigation items as a form using a loop as follows:
所以我基本上使用循环将每个导航项创建为一个表单,如下所示:
<ul>
begin loop...
<li>
<form action="/" method="post">
<input type="hidden" name="country" value="India"/>
<button type="submit" name="button">India</button>
</form>
</li>
end loop.
</ul>
what it did is to create a form with hidden input with a value assigned same as the text on the button. So the end user will see only text from the button and when clicked, will send a post request to the server.
它所做的是创建一个带有隐藏输入的表单,其分配的值与按钮上的文本相同。所以最终用户只会看到按钮上的文本,当点击时,会向服务器发送一个发布请求。
Note that the value parameter of the input box and the Button text are exactly same and were values passed using ejs that I have not shown in this example above to keep the code simple.
请注意,输入框的 value 参数和 Button 文本完全相同,并且是使用 ejs 传递的值,我没有在上面的示例中显示以保持代码简单。
here is a screen shot of the navigation... enter image description here
这是导航的屏幕截图... 在此处输入图像描述

