0

我已经使用 html 和 php 在我的 wordpress 网站上建立了一个联系表单,但它似乎没有按应有的方式工作。单击提交按钮后,我被带到一个错误页面,上面写着“我不知道你在找什么”。如何将所有表单提交发送到我的电子邮件?

<div id="contact">
    <div id="contact-form" class="clearfix">  
        <h2><img src="<?php echo get_template_directory_uri(); ?>/img/chat.png" alt="contact frsh studio"></h2> 
        <?php  
            //init variables  
            $cf = array();  
            $sr = false;  
            if(isset($_SESSION['cf_returndata'])){  
                $cf = $_SESSION['cf_returndata'];  
                $sr = true;  
            }  
        ?> 
            <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">  
                <li id="info">There were some problems with your form submission:</li>  
                <?php  
                if(isset($cf['errors']) && count($cf['errors']) > 0) :  
                    foreach($cf['errors'] as $error) :  
                ?>  
                <li><?php echo $error ?></li>  
                <?php  
                    endforeach;  
                endif;  
                ?>  
            </ul>  
            <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for reaching out! I'll be in touch shortly!</p>    
        <form method="post" action="process.php"> 
            <textarea id="message" name="message" placeholder="MESSAGE" required="required" data-minlength="20"></textarea>

            <input type="text" id="name" name="name" value="" placeholder="NAME" required="required" autofocus="autofocus" />      

            <input type="email" id="email" name="email" value="" placeholder="EMAIL" required="required" />   
            <select id="enquiry" name="enquiry">  
                <option value="refrsh">Brand REFRSH</option>  
                <option value="consult">Brand Consultation</option>  
                <option value="support">Just a Hello!</option>  
            </select>  

            <span id="loading"></span>  
            <input type="submit" value="Holla!" id="submit-button" />  

        </form>  
    </div>  
</div><!-- end contact -->
<?php  
if( isset($_POST) ){  
    //form validation vars  
    $formok = true;  
    $errors = array();  
    //submission data  
    $ipaddress = $_SERVER['REMOTE_ADDR'];  
    $date = date('d/m/Y');  
    $time = date('H:i:s');  
    //form data  
    $name = $_POST['name'];  
    $email = $_POST['email'];  
    $telephone = $_POST['telephone'];  
    $enquiry = $_POST['enquiry'];  
    $message = $_POST['message'];  
    //validate form data  
    //validate name is not empty  
    if(empty($name)){  
        $formok = false;  
        $errors[] = "You have not entered a name";  
    }  
    //validate email address is not empty  
    if(empty($email)){  
        $formok = false;  
        $errors[] = "You have not entered an email address";  
    //validate email address is valid  
    }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){  
        $formok = false;  
        $errors[] = "You have not entered a valid email address";  
    }  
    //validate message is not empty  
    if(empty($message)){  
        $formok = false;  
        $errors[] = "You have not entered a message";  
    }  
    //validate message is greater than 20 characters  
    elseif(strlen($message) < 20){  
        $formok = false;  
        $errors[] = "Your message must be greater than 20 characters";  
    }  
    //send email if all is ok  
    if($formok){  
        $headers = "From: info@example.com" . "\r\n";  
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
        $emailbody = "<p>You have received a new message from the enquiries form on your website.</p>
                      <p><strong>Name: </strong> {$name} </p>
                      <p><strong>Email Address: </strong> {$email} </p>
                      <p><strong>Telephone: </strong> {$telephone} </p>
                      <p><strong>Enquiry: </strong> {$enquiry} </p>
                      <p><strong>Message: </strong> {$message} </p>
                      <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";  
        mail("alex@frshstudio.com","New Enquiry",$emailbody,$headers);  
    }  
    //what we need to return back to our form  
    $returndata = array(  
        'posted_form_data' => array(  
            'name' => $name,  
            'email' => $email,  
            'telephone' => $telephone,  
            'enquiry' => $enquiry,  
            'message' => $message  
        ),  
        'form_ok' => $formok,  
        'errors' => $errors  
    );  
    //if this is not an ajax request  
    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){  
        //set session variables  
        session_start();  
        $_SESSION['cf_returndata'] = $returndata;  
        //redirect back to form  
        header('location: ' . $_SERVER['HTTP_REFERER']);  
    }  
}
4

2 回答 2

1

您的问题可能来自您的表单操作,请尝试此固定操作

<form method="post" action="http://frshstudio.com/wp-content/themes/frsh/process.php">

并移动 session_start(); 到 process.php 的顶部

并确保这<?php 是 process.php 第一行的唯一内容

于 2013-03-06T04:24:48.317 回答
0

尝试全部更改为:

<div id="contact">
    <div id="contact-form" class="clearfix">  
        <h2><img src="<?php echo get_template_directory_uri(); ?>/img/chat.png" alt="contact frsh studio"></h2> 
        <?php  
        $message = $_REQUEST["message"];
        $name = $_REQUEST["name"];
        $email = $_REQUEST["email"];
        $enquiry = $_REQUEST["enquiry"];
        $headers = "From:" . $email;
        if($message !== null && $name !== null && $email !== null && $enquiry !== null){
            mail("alex@frshstudio.com",$enquiry,$message,$headers);
            echo "message sent!";
        } else {

        }
        ?> 
            <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">  
                <li id="info">There were some problems with your form submission:</li>  
                <?php  
                if(isset($cf['errors']) && count($cf['errors']) > 0) :  
                    foreach($cf['errors'] as $error) :  
                ?>  
                <li><?php echo $error ?></li>  
                <?php  
                    endforeach;  
                endif;  
                ?>  
            </ul>  
            <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for reaching out! I'll be in touch shortly!</p>    
        <form method="post" action="#"> 
            <textarea id="message" name="message" placeholder="MESSAGE" required="required" data-minlength="20"></textarea>

            <input type="text" id="name" name="name" value="" placeholder="NAME" required="required" autofocus="autofocus" />      

            <input type="email" id="email" name="email" value="" placeholder="EMAIL" required="required" />   
            <select id="enquiry" name="enquiry">  
                <option value="refrsh">Brand REFRSH</option>  
                <option value="consult">Brand Consultation</option>  
                <option value="support">Just a Hello!</option>  
            </select>  

            <span id="loading"></span>  
            <input type="submit" value="Holla!" id="submit-button" />  

        </form>  
    </div>  
</div><!-- end contact -->

还有一个,我想你还没有创建 process.php

于 2013-03-06T04:33:09.853 回答