表单提交后,仅使用PHP将内容替换为消息

时间:2010-01-08 21:25:30

标签: php forms

我有一个包含订阅者注册表单的弹出窗口。用户单击订阅后,我希望删除该表单并显示一条感谢信息。所有这些都需要使用php运行。

下面是我到目前为止所做的,但弹出窗口没有停留,当再次单击订阅按钮时,它会显示表单验证错误,因为已设置'subscribeSubmit'变量。

那么,如何保持弹出窗口打开并用欢迎消息替换表单?

请帮助,谢谢!

<?php
if(isset($_POST['subscribeSubmit'])){

unset($error); //errasing error variable


//**************finding errors in form*******************  

if(strlen($_POST['Fname']) <= 0) {$error[] = "Your name is required.";}

if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,6}$", stripslashes(trim($_POST['emailPopin'])))) {$error[] = "Your e-mail address is not valid.";}

if(strlen($_POST['emailPopin']) <= 5) {$error[] = "You have to enter at least one e-mail to subscribe.";}

if(strlen($_POST['industry']) <= 0) {$error[] = "Your industry is required.";}

if(strlen($_POST['country']) <= 0) {$error[] = "Your country is required.";}

if(strlen($_POST['zip']) <= 0) {$error[] = "Your zip is required.";}

//************if there is an error**********

if(sizeof($error) > 0)
{
report_errors($error);
subscribe_form();                   
}

//**********if there is no error, we can subscribe them******  

else
{

//**********Reduce First Name to 30 characters and replace special characters******  

$name = substr($_POST['Fname'], 0, 30);

//***** Replaces special characters ******
$name = special_letters($name);


//**********write to data file******  

//Left out on purpose

//**********Display thank you message******

echo '<p>Thanks for signing up!<br/><a href="#" onClick="window.location.href=window.location.href">Click here to sign up friends.</a></p>';

}

}

else

{ 

$Fname = $_POST["Fname"];
$email = $_POST["emailPopin"];
$leader = $_POST["radiobuttonTeamLeader"];
$industry = $_POST["industry"];
$country = $_POST["country"];
$zip = $_POST["zip"];


echo '<form action="" method="post" enctype="multipart/form-data" style="background:#efefef;">';

echo '<div style="text-align:left;background:#efefef;padding-bottom:4px;"><p style="line-height:20px; font-size:12px;">Fill in the form below to signup for our free daily newsletter. All fields are Necessary.( <span class="required">*</span> ).</p><table id="popupSubscribe-form">';

echo '<tr><td class="label"><label for="name">First Name: <span class="required">*</span></label></td><td><input type="text" name="f_name" size="30" value=""></td></tr>';

echo '<tr><td class="label"><label for="email">Email: <span class="required">*</span></label></td><td><input type="text" size="30" id="emailPopin" value=""></td></tr>';

echo '<tr><td class="label"><label for="email">I Lead A Team: <span class="required">*</span></label></td>';

echo '<td><table><tr><td><input type="radio" value="yes" name="radiobuttonTeamLeader" style="width:15px;"><strong style="margin: 0 15px 0 5px;">Yes</strong></td>';

echo '<td><input type="radio" value="no" name="radiobuttonTeamLeader" style="width:15px;"><strong style="margin: 0 15px 0 5px;">No</strong></td></tr></table></td>';

echo '<tr><td class="label"><label for="industry">Industry: <span class="required">*</span></label></td><td><input type="text" name="industry" size="30" value=""></td></tr>';

echo '<tr><td class="label"><label for="country">Country <span class="required">*</span></label></td>';

echo '<td><select size="1" class="countryDropDown" name="country">';

echo '<option value="us" selected="selected">United States</option>';
echo '<option value="ca" >Canada</option>';

echo '</select></td></tr>';

echo '<tr><td class="label"><label for="email">Zip Code: <span class="required">*</span></label></td><td><input type="text" name="zip" size="30" value=""></td></tr>';

echo '<tr><td></td><td><input type="submit" id="subscribeSubmit" value="" name="subscribeSubmit"/></td></tr></table></div>';



echo '</form>';

}

}

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

<?php
if(isset($_POST['subscribeSubmit'])){

    unset($error); //errasing error variable


    //**************finding errors in form*******************  

    if(strlen($_POST['Fname']) <= 0) {$error[] = "Your name is required.";}

    if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,6}$", stripslashes(trim($_POST['emailPopin'])))) {$error[] = "Your e-mail address is not valid.";}

    if(strlen($_POST['emailPopin']) <= 5) {$error[] = "You have to enter at least one e-mail to subscribe.";}

    if(strlen($_POST['industry']) <= 0) {$error[] = "Your industry is required.";}

    if(strlen($_POST['country']) <= 0) {$error[] = "Your country is required.";}

    if(strlen($_POST['zip']) <= 0) {$error[] = "Your zip is required.";}

    //************if there is an error**********

    if(sizeof($error) > 0)
    {
        report_errors($error);
        subscribe_form();                   
    }

    //**********if there is no error, we can subscribe them******  

    else
    {

        //**********Reduce First Name to 30 characters and replace special characters******  

        $name = substr($_POST['Fname'], 0, 30);

        //***** Replaces special characters ******
        $name = special_letters($name);


        //**********write to data file******  

        //Left out on purpose

        //**********Display thank you message******

        $thank_you_message = '<p>Thanks for signing up!<br/><a href="#" onClick="window.location.href=window.location.href">Click here to sign up friends.</a></p>';

    }

    $Fname = $_POST["Fname"];
    $email = $_POST["emailPopin"];
    $leader = $_POST["radiobuttonTeamLeader"];
    $industry = $_POST["industry"];
    $country = $_POST["country"];
    $zip = $_POST["zip"];

}

if(!empty($thank_you_message)){
    echo $thank_you_message;
}else{

    echo '<form action="" method="post" enctype="multipart/form-data" style="background:#efefef;">';

    echo '<div style="text-align:left;background:#efefef;padding-bottom:4px;"><p style="line-height:20px; font-size:12px;">Fill in the form below to signup for our free daily newsletter. All fields are Necessary.( <span class="required">*</span> ).</p><table id="popupSubscribe-form">';

    echo '<tr><td class="label"><label for="name">First Name: <span class="required">*</span></label></td><td><input type="text" name="f_name" size="30" value=""></td></tr>';

    echo '<tr><td class="label"><label for="email">Email: <span class="required">*</span></label></td><td><input type="text" size="30" id="emailPopin" value=""></td></tr>';

    echo '<tr><td class="label"><label for="email">I Lead A Team: <span class="required">*</span></label></td>';

    echo '<td><table><tr><td><input type="radio" value="yes" name="radiobuttonTeamLeader" style="width:15px;"><strong style="margin: 0 15px 0 5px;">Yes</strong></td>';

    echo '<td><input type="radio" value="no" name="radiobuttonTeamLeader" style="width:15px;"><strong style="margin: 0 15px 0 5px;">No</strong></td></tr></table></td>';

    echo '<tr><td class="label"><label for="industry">Industry: <span class="required">*</span></label></td><td><input type="text" name="industry" size="30" value=""></td></tr>';

    echo '<tr><td class="label"><label for="country">Country <span class="required">*</span></label></td>';

    echo '<td><select size="1" class="countryDropDown" name="country">';

    echo '<option value="us" selected="selected">United States</option>';
    echo '<option value="ca" >Canada</option>';

    echo '</select></td></tr>';

    echo '<tr><td class="label"><label for="email">Zip Code: <span class="required">*</span></label></td><td><input type="text" name="zip" size="30" value=""></td></tr>';

    echo '<tr><td></td><td><input type="submit" id="subscribeSubmit" value="" name="subscribeSubmit"/></td></tr></table></div>';

    echo '</form>';
}

答案 1 :(得分:0)

subscribe_form();与上面的表单代码完全相同,它在显示感谢信息的else / echo之后。

我试图发表评论,但显然没有足够的用户点,并且在发布问题时没有登录。