dongtangjie0495 2010-01-08 21:25
浏览 15

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

I have a popup that contains a subscriber sign up form. After the user clicks subscribe I want the form be removed and display a thank you message. All this needs to run using php.

Below is what I have so far, but the popup doesn't stay up and when clicking on the subscribe button again it shows the form validation errors because the 'subscribeSubmit' variable is set.

So, how keep the popup open and replace the form with the welcome message?

Please help, thanks!

<?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条回答 默认 最新

  • duanchuang1935 2010-01-08 21:46
    关注

    Is this what you're going for?

    <?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>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大