当用户单击表单上的提交时,将用户保持在同一页面上

时间:2017-06-16 07:29:30

标签: php html forms

以下代码是我的评论表。我在我的博客文章中包含了这个文件comments.php。当用户点击提交时,他们会被带到博客页面,而不是保留在他们正在查看的博客文章页面上。

我在整个网站中对不同的表单使用了相同的代码,这些页面不会重定向,而是在用户点击提交时保留在同一页面上。这是正确的方式。

我想知道为什么当用户点击提交时,博客页面上的相同代码会被重定向到博客类别视图。

如何防止重定向谢谢。

    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $commentErr = "";
    $name = $email = $comment = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
    $nameErr = "First Name is required";
    } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
    }


    //email

    if (empty($_POST["email"])) {
    $emailErr = "Email is required";
    } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
    }


    if (empty($_POST["comment"])) {
    $comment = "";
    } else {
    $comment = test_input($_POST["comment"]);
    }
    }

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    ?>

    <?php 
    if (count($_POST)>0) echo "<h2>Form Submitted! Thank you <b>$name $lname</b> 
    for your comment</h2>";
    ?>

    <hr>
    <h3 style=" margin-top: 50px; ">Leave a comment</h3>
    <h6><span class="error">* All fields required.</span></h6><br /><br />

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <input class="form-control" type="text" placeholder="Name*" name="name" value="<?php echo $name;?>" required>
    <span class="error"> <?php echo $nameErr;?></span>
    <br><br>
    </div>
    </div>
    <!--Email-->
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <input class="form-control" type="email" name="email" placeholder="email address*" value="<?php echo $email;?>"required>
    <span class="error"> <?php echo $emailErr;?></span>
    <br>
    </div>
    </div>
    <div class="col-md-12">
    <div class="form-group">
    <textarea class="form-control" style="margin-left: 17px;" name="comment" placeholder="Enter a comment*" rows="5" cols="40" required><?php echo $comment;?></textarea>
    <br><br>
    <div class="g-recaptcha" data-sitekey="6Lc3ZyUUAAAAAIT2Blrg4BseJK9KFc1Rx8VDVNs-"></div><br/>
    <input type="submit" name="submit" value="Submit">  

    </div>
    <hr>
    </div>
    </div>

    </form>

    <?php

    //check if the form has been submitted
    if(isset($_POST['submit'])){

/* Attempt MySQL server connection. */
    <?php include 'view/conn.php'; ?>

// Check connection
    if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    mysqli_set_charset($link, "utf8");

// Escape user inputs for security
    $Fname = mysqli_real_escape_string($link, $_REQUEST['name']);
    $Email = mysqli_real_escape_string($link, $_REQUEST['email']);
    $Message = mysqli_real_escape_string($link, $_REQUEST['comment']);

// attempt insert query execution
    $sql = "INSERT INTO comments (Name, Email, Comment, Approved) VALUES 
    ('$Fname', '$Email', '$Message', '0')";
    if(mysqli_query($link, $sql)){
    echo "<p>$Fname Your comment will appear once approved";
    } else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

// close connection
    mysqli_close($link);
    }
    ?>

    <?PHP
    $email = $_POST["email"];
    $to = "email@email.com";
    $subject = "New Email Address for Mailing List";
    $headers = "From: $email\n";
    $message = "A visitor to your site has posted a comment on a blog post that requires approval.\n

    Email Address: $email";
    $user = "$email";
    $usersubject = "Thank You";
    $userheaders = "From: email@email.com\n";
    $usermessage = "Thank you for comment at www.oryanm.waiariki.net.nz Geyserland SBA.";
    mail($to,$subject,$message,$headers);
    mail($user,$usersubject,$usermessage,$userheaders);
    ?>

    <?php
    echo "<h2>Comments</h2>";
    <?php include 'view/conn.php'; ?>

// Check connection
    if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM comments WHERE Approved=1 ORDER by id DESC";
    $result = mysqli_query($conn, $sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<p><b>Comment by:</b> " . $row["Name"]. "</p>" . "<p>" . 
    $row["Comment"]. "</p> " . "<i><b>Posted:</b> " . $row["Posted"]. "</i><br>
    <hr>";
    }
    } else {
    echo "0 results";
    }
    $conn->close();

    ?>

2 个答案:

答案 0 :(得分:0)

我使用iframe作为此问题的解决方案而不是php include。谢谢你的帮助

答案 1 :(得分:0)

在表单中传递一个隐藏字段

&LT;输入类型=&#34;隐藏&#34;值=&#34; testRedirect&#34;&GT;

并将重定向代码放在您重定向的位置

并将重定向代码放在您重定向的位置

如果(isset($ _ REQUEST [&#34;隐藏&#34;]))

{

标题(&#39;位置:http://www.example.com/your_form_view_page&#39;);

出口;

}