简体   繁体   English

提交PHP表单并刷新页面后,可以使用javaScript隐藏该表单吗?

[英]Can I hide the form using javaScript when a PHP form has been submitted and the page refreshes?

I have a form that is created in PHP and I've been doing the front end styling to it. 我有一个用PHP创建的表单,并且一直在对其进行前端样式设计。 When the form is submitted the page refreshes and a message is displayed. 提交表单后,页面将刷新并显示一条消息。 The message is displayed above the form - but the user shouldn't be able to interact with the form once it's submitted - or see it! 该消息显示在表单上方-但是一旦提交,用户就不能与该表单进行交互-或看到它! Just see the confirmation message. 只需查看确认消息即可。 I have no idea where to start with this or why this is happening. 我不知道从哪里开始或者为什么会这样。 Can someone send me in the right direction? 有人可以向我发送正确的方向吗?

If you want to do this server side, then you can simply do: 如果要在服务器端进行操作,则只需执行以下操作:

{% if (confirmed) %}`
    <p>Thank you. The form has been hidden</p>
{% else %}
    <form>
        <input type='text'>
    </form>
{% endif %}

Or, if you want to do this client side, then it is simple using jquery and you can hide the form by doing: 或者,如果您想做此客户端,那么使用jquery很简单,您可以通过执行以下操作隐藏表单:

$("#form-id").hide()

Apply the "myForm" id to tag. 将“ myForm” ID应用于标签。

<form id="myForm">

Close the PHP tag one line before your message line printed in PHP, for example in case of contact form, like this. 在您的消息行以PHP打印之前,请关闭PHP标记行的第一行,例如在使用联系表格的情况下。

//your ongoing code on click event of submit button
mail("mymail@gmail.com", $subject, $message, $headers, $email);
?>
<script>
$("#myForm").hide()
</script>
<?php 
echo "Message sent.";
?>

Another way to do it through CSS (Tested, Working!) 通过CSS完成测试的另一种方式(已测试,正在工作!)

//your ongoing code on click event of submit button
mail("mymail@gmail.com", $subject, $message, $headers, $email);
?>
<style>
#myForm{display:none;}
</style>
<?php 
echo "Message sent.";
?>

Make sure to place this INSIDE THE CLICK function of the button. 确保将其放置在按钮的“单击内部”功能中。 It should work! 它应该工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM