首页 > 解决方案 > 数据输入数据库后更改内容

问题描述

我有一个页面,用户可以在其中输入他的电子邮件地址来订阅时事通讯。然后通过表单中的操作标记将其转发到 PHP 文件,然后将其保存在数据库中。然后页面将被更新。

现在我想给用户一些反馈,一切都很顺利。

按钮上方有一个带有 id = notify- 的文本,上面写着:订阅我们的时事通讯。现在我希望用新的内容替换此文本,但使用相同的 CSS 格式。我怎么做 ?

标签: javascriptphphtmlcssweb

解决方案


有很多方法可以做到这一点。

但是由于缺乏关于您实际上是如何构建“订阅时事通讯”功能的知识,因此很难准确回答。

正如您所说,您正在通过表单提交将 mailId 转发到 PHP 文件,然后您可以执行类似的操作。

方法1:-

subscribe.php(您订阅时事通讯的页面)

<?php
if(isset($_POST['success']))
{
if($_POST['success']== 1)
echo '<script>document.getElementById("notify").innerHTML = "Subscribed successfully..!";</script>';
else 
echo '<script>document.getElementById("notify").innerHTML = "Something went wrong..!";</script>';
}
?>

//form for subscription
<form action="action.php">
<input type = "email" name="email">
<input type = "submit" value = "Subscribe" >
</form>
<div id="notify"></div>
//after submission it will go to php file and then the database thing will be done as you said

动作.php

<form name="success" action="subscribe.html">
<input type="hidden" name="success" value="" id= "successcode">
</form>
<?php
$email = $_POST['email'];

//do whatever you want 
//database thing or whatever

if(//success)
{
echo '<script> document.getElementById("successcode").value=1;
document.forms["success"].submit();</script>';
}
else
echo '<script> document.getElementById("successcode").value=0;
document.forms["success"].submit();</script>';
?>

方法2:-(需要单个文件)

<?php
if(isset($POST['email']))
{
$email = $_POST['email'];//get the mail
//do the stuff here...
if (//success)
echo '<script>document.getElementById("notify").innerHTML = "Subscribed successfully..!";</script>';
else
echo '<script>document.getElementById("notify").innerHTML = "Something went wrong..!";</script>';
    ?>
    
    //form for subscription
    <form action=" ">
    <input type = "email" name = "email">
    <input type = "submit" value = "Subscribe" >
    </form>
    <div id="notify"></div>
    //after submission it will be submitted to same file and then the database thing will be done as you said

javascript 可能存在一些错误,您可以轻松解决这些错误。

您可以通过更多方式提醒用户订阅成功,例如 cookie、JS 警报、ajax 调用等


推荐阅读