简体   繁体   English

单击提交按钮时,在PHP的同一页面上临时更改HTML结构吗?

[英]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而无需重新加载)到目前为止是“最漂亮的”,但是我当然不知道您网站上还有什么其他情况

Demo 演示版

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.

相关问题 在反应中单击提交按钮时如何在同一页面上显示状态 - How to display the state on the same page when clicked Submit button in react 根据单击的提交按钮重定向到php页面 - Redirect to a php page based on submit button clicked 单击“提交”按钮时,HTML表单无法提交数据? - Form in html is not able to submit data when clicked on submit button? 单击提交按钮时,将在javascript中创建的动态生成的html表保存并传递给php - preserve and pass dynamically generated html table created in javascript to php when submit button is clicked 在html上提交按钮以在同一页面上输出结果 - submit button on html to output result on same page 单击浏览器后退按钮时,禁用页面上的提交按钮 - Disable submit button on a page when browser back button is clicked 单击提交按钮后如何隐藏和显示页面 (HTML) - How to hide and show a page after the submit button is clicked (HTML) 在 html 和 javascript 中单击时更改按钮。(单击时将添加到购物车按钮更改为购物车图标) - Changing the button when clicked in html and javascript.(changing add to cart button to a cart icon when clicked) 单击提交按钮时,使用HTML5将文本添加到画布 - Adding text to a canvas with HTML5 when submit button is clicked HTML,Javascript和JSP:单击提交按钮时,表单不参与“操作” - HTML, Javascript, and JSP: Form not engaging in “action” when submit button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM