1

我在一个 PHP 页面上有两个表单。第一个要求电子邮件,第二个要求反馈。两种形式的名称不同并且有效。我的 php 脚本正在工作,结果将发送到两个不同的数据库,所以那里都很好。

当我希望在每个表单下方显示一条确认消息时,就会出现问题。目前,当用户单击“订阅”或“发布反馈”按钮时,确认消息会出现在页面顶部。如何让每条确认消息显示在表单下方(由下面表单代码中的 HTML 注释标记指示)?

我确信有一种相对简单的方法可以做到这一点,但我只是看不到它。

<?php 
if(isset($_POST['submit'])){
    if($_POST['submit'] == 'Subscribe'){
        //process form1


        $email = str_replace("'", "\'", htmlentities($_POST['email']));


        $dbc = mysqli_connect('localhost', 'root', 'password', 'newslist') 
        or die('Error no connection to server.');


        $query = "INSERT INTO email_list(email, submitted) VALUES ('$email', now())";


        $result = mysqli_query($dbc, $query) or die ('Error querying database.');


        mysqli_close($dbc);

        echo "<p span style=\"color:#c3593c; font-weight:bold; font-size:18px;  text-align:center\">Thanks for signing up.</span></p>";

    }


        else if($_POST['submit'] == 'Post Feedback'){
        //process form2


        $feedback = str_replace("'", "\'", htmlentities($_POST['feedback']));


        $dbc = mysqli_connect('localhost', 'root', 'password', 'feedbacklist') 
        or die('Error no connection to server.');


        $query = "INSERT INTO feedback(feedback, submitted) VALUES ('$feedback', now())";


        $result = mysqli_query($dbc, $query) or die ('Error querying database.');


        mysqli_close($dbc);

        echo "<p span style=\"color:#c3593c; font-weight:bold; font-size:18px; text-align:center\">Thanks for the feedback.</span></p>";
    }
}
?>



<html>
<head>
<style type="text/css">
#newsletter, #feedback{
    padding: 15px;
background-color: #E4E4E4;
    border: 1px solid #ccc;
    width: 300px;}

#newsletter h2, #feedback h2{
    margin: 0;
    padding-bottom: 3px;
    font-size: 19px;
    color: #c3593c;
    font-family:verdana, arial, sans-serif;}

#newsletter p, #feedback p{
    font-family:verdana, arial, sans-serif;
    margin: 0;}

</style>
</head>

<body>

<div id="newsletter">
<h2>Newsletter</h2>
<p>Subscribe to our newsletter.</p><br />
<form name="mailinglist" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<p><strong>Email</strong> <input type="text" name="email" />
<input name="submit" type="submit" value="Subscribe" /></p>
<!--I want the first message echoed here once & only if the first form has been submitted-->
</form>
</div>


<br /><br />


<div id="feedback">
<h2>Feedback</h2>
<p>Post your feedback.</p><br />
<form name="feedback" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<p>Your Suggestion:<br /><textarea name="feedback" rows="8" cols="30"></textarea>
<input name="submit" type="submit" value="Post Feedback" /></p>
<!--I want the second message echoed here once & only if the second form has been submitted-->
</form>
</div>
</body>
</html>

谢谢你的帮助。

安迪 ;-)

4

2 回答 2

1

考虑将现有的 PHP 代码分成两部分。

<form name="mailinglist" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<p><strong>Email</strong> <input type="text" name="email" />
<input name="submit" type="submit" value="Subscribe" /></p>
<!--I want the first message echoed here once & only if the first form has been submitted-->
</form>

Put the first one here:
<?php if(isset($_POST['submit'])){
  if($_POST['submit'] == 'Subscribe'){
// do stuff
}
}?>

<form name="feedback" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<p>Your Suggestion:<br /><textarea name="feedback" rows="8" cols="30"></textarea>
<input name="submit" type="submit" value="Post Feedback" /></p>
<!--I want the second message echoed here once & only if the second form has been submitted-->
</form>

    Put the second one here:
    <?php if(isset($_POST['submit'])){
    if($_POST['submit'] == 'feedback'){
    // do stuff
    }
    }?>
于 2013-08-08T20:04:11.073 回答
0

使用您的代码,我会这样做:

<?php if(isset($_POST['submit'])){
if($_POST['submit'] == 'Subscribe'){
    //process form1


    $email = str_replace("'", "\'", htmlentities($_POST['email']));


    $dbc = mysqli_connect('localhost', 'root', 'password', 'newslist') 
    or die('Error no connection to server.');


    $query = "INSERT INTO email_list(email, submitted) VALUES ('$email', now())";


    $result = mysqli_query($dbc, $query) or die ('Error querying database.');


    mysqli_close($dbc);

    $subscribe_message = "<p span style=\"color:#c3593c; font-weight:bold; font-size:18px;  text-align:center\">Thanks for signing up.</span></p>";

}


    else if($_POST['submit'] == 'Post Feedback'){
    //process form2


    $feedback = str_replace("'", "\'", htmlentities($_POST['feedback']));


    $dbc = mysqli_connect('localhost', 'root', 'password', 'feedbacklist') 
    or die('Error no connection to server.');


    $query = "INSERT INTO feedback(feedback, submitted) VALUES ('$feedback', now())";


    $result = mysqli_query($dbc, $query) or die ('Error querying database.');


    mysqli_close($dbc);

    $feedback_message = "<p span style=\"color:#c3593c; font-weight:bold; font-size:18px; text-align:center\">Thanks for the feedback.</span></p>";
}
}
?>



<html>

...

<body>

<div id="newsletter">
<h2>Newsletter</h2>
<p>Subscribe to our newsletter.</p><br />
<form name="mailinglist" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<p><strong>Email</strong> <input type="text" name="email" />
<input name="submit" type="submit" value="Subscribe" /></p>
<!--I want the first message echoed here once & only if the first form has been submitted-->
<? echo $subscribe_message; ?>
</form>
</div>


<br /><br />


<div id="feedback">
<h2>Feedback</h2>
<p>Post your feedback.</p><br />
<form name="feedback" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<p>Your Suggestion:<br /><textarea name="feedback" rows="8" cols="30"></textarea>
<input name="submit" type="submit" value="Post Feedback" /></p>
<!--I want the second message echoed here once & only if the second form has been submitted-->
<? echo $feedback_message?>
</form>
</div>
</body>

于 2013-08-08T23:34:11.717 回答