0

每次我单击提交按钮时,页面都会重定向。

我如何设置它以使页面不会重定向并且它运行服务器端代码并​​停留在当前页面上并在我的表单上方回显?

HTML 代码:

    <form name="contact_form" method="post" action="mail.php">
<table border="0">
<tbody class="Form">
<tr>
<td>
<label for="NameInput">Your Name:<font color="#FF0000">*</font></label>
<input autofocus="autofocus" placeholder="John Doe" id="NameInput" name="NameInput" required="required" />
</td>
</tr>
<tr>
<td>
<label for="CompanyName">Your Company:<font color="#FF0000">*</font></label>
<input required="required" placeholder="Warner bros." id="CompanyName" name="CompanyName" />
</td>
</tr>
<tr>
<td>
<label id="PhoneNumber" name="PhoneNumber">Phone: </label>
<input placeholder="888-888-8888" id="PhoneNumber" name="PhoneNumber"/>
</td>
</tr>
<tr>
<td>
<label id="Email" name="Email">Email:<font color="#FF0000">*</font></label>
<input required="required" placeholder="johndoe@gmail.com" id="Email" name="Email" type="email" />
</td>
</tr>
<tr>
<td>
<label for="Website">Website: </label>
<input placeholder="https://" id="Website" name="Website" />
</td>
</tr>
</tbody>
</table>
<br/>
<br/>
<br/>
<br/>

<p><strong>What interests you, broadly speaking?</strong></p>
<br/>

<p><input  id="WebDesignCheckbox" name="WebDesignCheckbox" type="checkbox">Web Design/Development</input></p>

<input id="SocialMediaCheckbox" name="SocialMediaCheckbox" type="checkbox">Social Media</input>
>
<input id="MobilePresence" name="MobilePresence" type="checkbox">Mobile Presence</input>

<input id="OnlineAdvertising" name="OnlineAdvertising" type="checkbox">Online Advertising</input>

<input id="SEOCheckbox" name="SEOCheckbox" type="checkbox">Search Engine Optamization</input>

<p><input id="ecommerceCheckbox" name="ecommerceCheckbox" type="checkbox" >eCommerce</input></p>

<br/>


<label for="comments">Your Ideas to Life?</label><br/>
<textarea placeholder="How can we help you?" id="comments" name="comments" style="margin: 2px; height: 137px; width: 437px;"></textarea></td></tr><br/>
<tr>
<td><input type="submit" id="Submit" name="Submit" value="Submit" align="middle" /></td>
</tr>
<p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
</form>

PHP 脚本 mail.php:

<?php  
if( isset($_POST) ){  

    //form validation vars  
    $formok = true;  
    $errors = array();  

    //sumbission data  
    $ipaddress = $_SERVER['REMOTE_ADDR'];  
    $date = date('d/m/Y');  
    $time = date('H:i:s');  

    //form data  
    $NameInput = $_POST['NameInput'];      
    $CompanyName = $_POST['CompanyName'];  
    $PhoneNumber = $_POST['PhoneNumber'];  
    $Email = $_POST['Email'];  
    $Website = $_POST['Website'];
    $WebDesignCheckbox = $_POST['WebDesignCheckbox'];
    $SocialMediaCheckbox = $_POST['SocialMediaCheckbox'];
    $MobilePresence = $_POST['MobilePresence'];
    $OnlineAdvertising = $_POST['OnlineAdvertising'];
    $SEOCheckbox = $_POST['SEOCheckbox'];
    $ecommerceCheckbox = $_POST['ecommerceCheckbox'];
    $comments = $_POST['comments'];  

    //form validation
    if(empty($NameInput)){
        $formok - false;
        $errors[] =  "You have not entered a name.";
    }

    if(empty($CompanyName)){
        $formok = false;
        $errors[] = "You have not entered a company name.";
    }

    if(empty($Email)){
        $formok = false;
        $errors[] = "You have not entered an email address.";
    }

    //send email if checks out
    if($formok){
        ini_set("sendmail_from","keeano@doodleinc.co");
        /*$headers="From: Contact Page: {$Email}" . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";*/
        $emailbody = "
        <p>Time: {$time} \n {$date}</p>
        <p>You are recieving this from your websites contact form.</p>
        <p><strong>Contacts Name: </strong> {$NameInput}</p>
        <p><strong>Companys Name: </strong> {$CompanyName}</p>
        <p><strong>Phone Number: </strong> {$PhoneNumber}</p>
        <p><strong>Email: </strong> {$Email}</p>
        <p><strong>Website: </strong> {$Website}</p>
        <p><strong>Web Design Checkbox: </strong> {$WebDesignCheckbox}</p>
        <p><strong>Social Media Checkbox: </strong> {$SocialMediaCheckbox}</p>
        <p><strong>Mobile Presence: </strong> {$MobilePresence}</p>
        <p><strong>Online Advertising: </strong> {$OnlineAdvertising}</p>
        <p><strong>SEO Checkbox: </strong> {$SEOCheckbox}</p>
        <p><strong>eCommerce Checkbox: </strong> {$ecommerceCheckbox}</p>
        <p><strong>Comments: </strong> {$comments}</p>
        <p><strong>Extra Data Obtained:</strong>\n Users IPAddress: {$ipaddress}</p>
        ";
        mail("keeano@doodleinc.co", "Contacts Page Enquiry", $emailbody);

        echo('<p style="font-weight: bold; color: #709900"><em>Thank you for contacting doodle Inc., someone will contact you within 48 hours.</em></p>');
    }
}

我只是想让它在表单顶部创建一个标签,上面写着“谢谢,有人会在 48 小时内与您联系”。

如果有人可以帮助我,那将是很好的以及详细的解释,所以我再也不会遇到这个问题了。

4

2 回答 2

1

问题是您mail.php对表单标签的操作。您可以将其留空,然后表单将发布到同一页面(contact.php)

要使功能在该页面上运行,您可以mail.php在表单之前包含您的文件,以便错误显示在同一页面上。只需 arequire("mail.php");在代码的顶部就可以了。

于 2013-05-31T03:56:51.190 回答
0

您可以查看在提交按钮上执行 javascript 事件。然后使用 json 和 ajax 处理您的表单等,返回成功/失败消息,并插入表单所在的位置。这不会将用户“重定向”到不同的页面,表单将被成功消息替换。

<form method="get" action="" onsubmit="jsquickcomp(this); return false;" > // or something like this for your form heading.

然后你的js函数......

function jsquicksub(po_form) {

    if (gb_ajaxbusy)
        return false;

    gb_ajaxbusy = true;

    var lo_request = {};
    lo_request.n = $(po_form).find('#sub-firstname').val();
    lo_request.e = $(po_form).find('#sub-email').val();
// etc etc

    $(po_form).fadeTo(200, 0.3);

    $.getJSON('/mail.php', lo_request, function(po_result) { // this is your mail.php which performs all the php side of the form stuff

        gb_ajaxbusy = false;
        $(po_form).fadeTo(0, 1);

        if (!po_result.success) {
            alert(po_result.content); // has an alert box for error message. Can change it for anything
            return false;
        }else{
            setTimeout(function() { 
                    $.modal.close();
            }, 2500);
        }

        $(po_form).replaceWith(po_result.content); // replaces the form with the success message

    });

}

然后在你的 mail.php

// function gets passed values after validation and returns a message to the user on screen
function jsonreturn($pb_success, $ps_content = null) {

    $lo_result = (object) array('success'=>$pb_success, 'content'=>$ps_content);

    ob_clean();
    echo json_encode($lo_result);
    exit;

} // end jsonreturn()

// use $_GET to get all the values you have send through from your js file
// validate the data and do whatever you want to do with it

// these return the message that will replace the form
jsonreturn(true, 'Success!'); // for success
jsonreturn(false, 'Fail'); // for fail

希望这有点道理?

于 2013-05-31T04:59:28.183 回答