如何在同一页面中显示Php中的文本?

时间:2016-01-03 16:36:31

标签: php

我有一个引用另一个PHP文件的表单。

在我写的另一个PHP文件中:

echo $result;

然后,在提交表单时,将$result上的文本显示在不同的页面中。我应该如何在同一页面上显示它?

我希望结果显示在index.php文件中。

的index.php:

<form method="post" action="form.php">
    <div class="col-sm-6 form-group">
        <label for="name"></label>
        <input class="form-control" id="name" name="name" type="text" placeholder="Name">
    </div>
    <div class="col-sm-6 form-group">
        <label for="email"></label>
        <input class="form-control" id="email" name="email" type="email" placeholder="Email">
    </div>
    <div class="col-xs-12 form-group">
        <label for="subject"></label>
        <input class="form-control" id="subject" name="subject" type="text" placeholder="Subject">
    </div>
    <div class="col-xs-12">
        <label for="message"></label>
        <textarea class="form-control" id="message" name="message" rows="3" placeholder="Message"></textarea>
    </div>
    <button type="submit" id="submit">Submit</button>
    <?php echo $result; ?>
</form>

form.php的:

<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST'){

        $result = "";
        $name = $_POST['name'];
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $to = 'myemail@gmail.com';
        $body = "From: $email (Name: $name). \nSubject: $subject \n \nMessage:\n$message";

        if (!$_POST['name'] || !$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ||
            !$_POST['subject'] || !$_POST['message']) {
                $result = '<div class="alert alert-danger">Invaild form. Please try again.</div>';
            }
        else {
            mail($to, $subject, $body);
            $result = '<div class="alert alert-success">Thank You! We will respond you soon.</div>';
        }
        echo $result;
    }
?>

2 个答案:

答案 0 :(得分:1)

这个分离怎么样。

<强>的index.php

<?php 
if (!empty($_POST)){
echo "Welcome ".$_POST["name"];
}
require_once 'form.php';
?>

<强> form.php的

<form method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

答案 1 :(得分:1)

<强> form.php的

<form method="post" action="form.php">
    <div class="col-sm-6 form-group">
        <label for="name"></label>
        <input class="form-control" id="name" name="name" type="text" placeholder="Name">
    </div>
    <div class="col-sm-6 form-group">
        <label for="email"></label>
        <input class="form-control" id="email" name="email" type="email" placeholder="Email">
    </div>
    <div class="col-xs-12 form-group">
        <label for="subject"></label>
        <input class="form-control" id="subject" name="subject" type="text" placeholder="Subject">
    </div>
    <div class="col-xs-12">
        <label for="message"></label>
        <textarea class="form-control" id="message" name="message" rows="3" placeholder="Message"></textarea>
    </div>
    <button type="submit" id="submit">Submit</button>
</form>

<强>的index.php

<?php
require_once "form.php";

if(!empty($_POST)){

    $result = "";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $to = 'myemail@gmail.com';
    $body = "From: $email (Name: $name). \nSubject: $subject \n \nMessage:\n$message";

    if (!$_POST['name'] || !$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ||
        !$_POST['subject'] || !$_POST['message']) {
            $result = '<div class="alert alert-danger">Invaild form. Please try again.</div>';
        }
    else {
        mail($to, $subject, $body);
        $result = '<div class="alert alert-success">Thank You! We will respond you soon.</div>';
    }
    echo $result;
}
?>