没有表单方法的 POST (PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5753589/
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
POST without Form Method (PHP)
提问by Arjun Bajaj
Is there any way to pass stuff from one page to another using the POST method without using forms.
有没有什么方法可以在不使用表单的情况下使用 POST 方法将内容从一个页面传递到另一个页面。
Like in get you can just append a ?
with whatever you want to send. Can you do something for post.
就像在 get 中一样,您可以在?
要发送的任何内容后附加 a 。你能为帖子做点什么吗?
I also read one other thread which said to use sessions. But sessions are saved on the users computer as cookies, and cookies are unsecure.
我还阅读了另一个线程,它说使用会话。但是会话作为 cookie 保存在用户计算机上,并且 cookie 是不安全的。
So is there any other way to do it? Like describe something in the first page and then pass it to the second page.
那么有没有其他方法可以做到呢?就像在第一页描述一些东西,然后将它传递到第二页。
Also it would be good if anyone could explain me how does the POST method work? And how does it pass data?
如果有人能解释我 POST 方法是如何工作的,那也很好?它是如何传递数据的?
回答by Hyman Billy
You should use jQuery to this. You can use its ajax()
function. Visit the link below and read the full description of it with the functions list to help you out.
您应该为此使用jQuery。你可以使用它的ajax()
功能。访问下面的链接并阅读它的完整说明和功能列表,以帮助您。
Here is a sample code for you:
这是一个示例代码:
<hmtl>
<head>
<script type="http://code.jquery.com/jquery-1.5.1.min.js"></script>
</head>
<body>
<div class="my-fake-form">
<input id="posting-value-1" type="text" />
<a id="submit-form-link" href="#submit">Submit this Div!</a>
</div>
</body>
</html>
Ajax code:
阿贾克斯代码:
function fake_form_submit ()
{
var post = $('input#posting-value-1').val();
$.ajax({
'url': 'your-php-file.php',
'type': 'POST',
'dataType': 'json',
'data': {post: post},
'success': function(data)
{
if(data.finish)
{
$("div.my-fake-form").attr("innerHTML","Form Submited!");
}
else
{
$("div.my-fake-form").attr("innerHTML","Form Not Submited!");
}
},
beforeSend: function()
{
$(document).ready(function () {
$("div.my-fake-form").attr("innerHTML","Loading....");
});
},
'error': function(data)
{
$(document).ready(function () {
$("div.my-fake-form").attr("innerHTML","ERROR OCCURRED!");
});
}
});
}
$(document).ready(function () {
$('a#submit-form-link').click(function (e) {
e.preventDefault();
fake_form_submit();
});
});
PHP:
PHP:
<?php
$post = $_POST['post'];
//Do Something with the value!
//On Succes return json encode array!
echo json_encode(array("finish" => true));
?>
回答by Iwan Luijks
Just use sessions.
只需使用会话。
The storage of sessions in a cookie is as indicated by the answers above limited to the ID of the session. Cookies can even be configured to be sent as HTTP-cookies only, if you're security-minded.
会话在 cookie 中的存储由上述限制为会话 ID 的答案所指示。如果您有安全意识,甚至可以将 Cookie 配置为仅作为 HTTP cookie 发送。
On the requesting page, set the session key-value pair you want. On the requested page request the previously set session key (check if it exists first). In you're php.ini you can choose to use session cookies as HTTP-only so you have more security.
在请求页面上,设置所需的会话键值对。在请求的页面上请求先前设置的会话密钥(首先检查它是否存在)。在 php.ini 中,您可以选择将会话 cookie 用作仅 HTTP,这样您就有更多的安全性。
Furthermore: posting data can be as simple as using AJAX or cURL as described above also.
此外:发布数据也可以像使用 AJAX 或 cURL 一样简单,如上所述。
回答by Wesley van Opdorp
回答by Headshota
you can use Ajax requests to send POST requests to the server. or cURL on the server side.
您可以使用 Ajax 请求向服务器发送 POST 请求。或服务器端的 cURL。
回答by BiAiB
You'll need some javascript if you want to POST from the browser and make it look like a link:
如果您想从浏览器 POST 并使其看起来像一个链接,则需要一些 javascript:
you can use submit() function . see http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtmlfor example.
您可以使用 submit() 函数。例如,请参见http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml。
but if you want it to act like GET, why not use GET ?
但是如果你想让它像 GET 一样,为什么不使用 GET 呢?
回答by Carbos
For send request in POST from a page, you can do with JQuery.post(in this case, you must use the library) or cURL(in php)
对于从页面在 POST 中发送请求,您可以使用JQuery.post(在这种情况下,您必须使用库)或cURL(在 php 中)
回答by Lassi
One way is to use JavaScript to submit an invisible form. HTML:
一种方法是使用 JavaScript 提交一个不可见的表单。HTML:
<button id="my-button">Go to other page</button>
<form id="my-form" method="POST" action="/other-page" style="display: none">
<input type="hidden" name="param1" value="value1">
<input type="hidden" name="param2" value="value2">
</form>
JavaScript (jQuery):
JavaScript (jQuery):
$("#my-button").click(function(e) {
$("#my-form").submit();
});
You could also use a link (<a>
tag) instead of a button, etc.
您还可以使用链接(<a>
标签)而不是按钮等。
This will make the browser go to the other page. If you want to stay on the current page and just post some data in the background, use the ajax functions in jQuery or similar libraries.
这将使浏览器转到另一个页面。如果你想停留在当前页面并且只是在后台发布一些数据,请使用 jQuery 或类似库中的 ajax 函数。