PHP表单重定向到PHP文件本身

时间:2015-02-26 01:37:07

标签: php html

我正在尝试使联系表单正常工作,而我正被重定向到PHP文件本身,我不知道为什么。我对PHP很新,我不完全确定我做错了什么。我希望被指向正确的方向。代码如下。

由于

HTML:

<p id='feedback'><?php echo $feedback; ?></p>
        <form id="contact_us" enctype="text/plain" method="post" action="form.php">
            <input class="form_field" type="text" name="Name" placeholder="Full Name">
            <br>
            <input class="form_field" type="text" name="Company" placeholder="Company Name">
            <br>
            <input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
            <br>
            <textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
            <br>
            <p id="required"><i>Please fill in all the fields*</i></p>
            <input class="submit" type="submit" value="SUBMIT">
        </form>

PHP:

<?php
    $to = 'zack@zfisch.com';
    $subject ='Dropset Work Request';

    $name = $_POST['Name'];
    $company = $_POST['Company'];
    $email = $_POST['Email_Address'];
    $message = $_POST['Description'];
    $message = <<<EMAIL

 From: $name

 $message

 Email: $email   

EMAIL;

$header = $subject;

if($_POST) {
    mail($to, $subject, $message, $header);
    $feedback = 'Email sent!';
}

?>

2 个答案:

答案 0 :(得分:1)

这一行告诉表单将​​数据发送到哪个PHP文件:

<form id="contact_us" enctype="text/plain" method="post" action="form.php">

特别是,这部分将数据发送到特定形式:

action="form.php"

事实上,这很好。您可以在包含表单的同一PHP文件中处理表单。只需将PHP表单处理放在顶部:

<?php

    if (isset($_POST['Email_Address'] && $_POST['Email_Address'] != ''){
        $to = 'zack@zfisch.com';
        $subject ='Dropset Work Request';

        $name = $_POST['Name'];
        $company = $_POST['Company'];
        $email = $_POST['Email_Address'];
        $message = $_POST['Description'];
        $message = <<<EMAIL

        From: $name

        $message

        Email: $email   

EMAIL;

        $header = $subject;

        if($_POST) {
        mail($to, $subject, $message, $header);
        $feedback = 'Email sent!';
        }
    }else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
    <input class="form_field" type="text" name="Name" placeholder="Full Name">
    <br>
    <input class="form_field" type="text" name="Company" placeholder="Company Name">
    <br>
    <input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
    <br>
    <textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
    <br>
    <p id="required"><i>Please fill in all the fields*</i></p>
    <input class="submit" type="submit" value="SUBMIT">
</form>
<?php
    }
?>

答案 1 :(得分:1)

只需删除enctype="text/plain",否则您的代码在HTML中是正确的,并尝试echo feedback变量以其他方式将所有代码写在HTML之上,如下所示:

     <?php

    if (isset($_POST['Email_Address'] && !empty['Email_Address']){
        $to = 'zack@zfisch.com';
        $subject ='Dropset Work Request';

        $name = $_POST['Name'];
        $company = $_POST['Company'];
        $email = $_POST['Email_Address'];
        $message = $_POST['Description'];
        $message = <<<EMAIL

        From: $name

        $message

        Email: $email   

        $header = $subject;

        if($_POST) {
        mail($to, $subject, $message, $header);
        $feedback = 'Email sent!';
        }
    }else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
    <input class="form_field" type="text" name="Name" placeholder="Full Name">
    <br>
    <input class="form_field" type="text" name="Company" placeholder="Company Name">
    <br>
    <input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
    <br>
    <textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
    <br>
    <p id="required"><i>Please fill in all the fields*</i></p>
    <input class="submit" type="submit" value="SUBMIT">
</form>
<?php
    }
?>