[英]Temporarily changing HTML structure on the same page in PHP when submit button is clicked?
I have PHP page that have submit button to another URL. 我有一个具有提交按钮到另一个URL的PHP页面。
I want to reload the current page after the submit button clicked, and add div
to the HTML. 单击提交按钮后,我想重新加载当前页面,并将
div
添加到HTML。
My page url is: /foo.php
, and in the HTML I have: 我的页面网址是:
/foo.php
,在HTML中,我有:
<button onclick="$.get('/bar', function() { ... })">Submit</button>
As you can see the form sends request to /bar
page. 如您所见,表单将请求发送到
/bar
页面。
I want to reload the /foo.php
(the current page), and change the HTML to: 我想重新加载
/foo.php
(当前页面),并将HTML更改为:
<button onclick="$.get('/bar', function() { ... })">Submit</button>
<div>Thank you!</div>
My problem is how can I know that the user click on the button and the refresh was because the click, and not because just navigating. 我的问题是我怎么知道用户单击按钮,刷新是因为单击,而不是因为只是导航。
Another thing, if it possible, I want that the new div
will disappear if the user refresh the page again. 另一件事,如果可能的话,我希望如果用户再次刷新页面,新的
div
将消失。
Why don't you just append the div in the success callback of the get function? 为什么不将div附加到get函数的成功回调中呢? You wouldn't have to reload the page.
您无需重新加载页面。
<div id="btn_area">
<button onclick="$.get('/bar', function() { $('#btn_area').append($('<div>').html('Thank You');)})">Submit</button>
</div>
By the way, i hardly recommend to separate the javascript from the html and not put it directli in the DOM. 顺便说一句,我几乎不建议您将javascript与html分开,而不要将其直接放在DOM中。
Another Method would be, to fire an additional form with a hidden parameter to the same side. 另一种方法是,将具有隐藏参数的其他表单激发到同一边。 After that, you check on the serverside the hidden parameter and display the div.
之后,您在服务器端检查hidden参数并显示div。
A third method is, to set a cookie in the Callback, reload the side, check the cookie, display the div and remove the cookie again. 第三种方法是,在回调中设置cookie,重新加载一侧,检查cookie,显示div,然后再次删除cookie。
In my opinion, the first mentioned option (add the div directly in the callback without reloading) would be by far the 'prettiest', but of course i don't know what else is going on on your site 我认为,第一个提到的选项(直接在回调中添加div而无需重新加载)到目前为止是“最漂亮的”,但是我当然不知道您网站上还有什么其他情况
You can handle that in js side. 您可以在js端进行处理。 Just make your request, and in callback, you can manipulate dom.
只需发出请求,然后在回调中就可以操作dom。 You can see below;
您可以在下面看到;
<button>Submit</button>
$("button").on("click", function() {
var $button = $(this);
$.get("/echo/html", function() {
$button.after("<div>Thank you!</div>");
});
});
Alternatively, you could simulate a flash session (one time use session) if you opt to do this in PHP. 另外,如果您选择在PHP中执行此操作,则可以模拟一个Flash会话(一次使用的会话)。 Consider this example:
考虑以下示例:
foo.php foo.php
<?php session_start(); ?>
<form method="POST" action="bar.php">
<button type="submit" name="thank_you">Submit</button>
</form>
<?php if(isset($_SESSION['thank_you'])): ?>
<?php unset($_SESSION['thank_you']); ?>
<h1>Thank You!</h1>
<?php endif; ?>
bar.php bar.php
<?php
session_start();
if(isset($_POST['thank_you'])) {
$_SESSION['thank_you'] = true;
// processes
header('Location: foo.php');
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.