使用分页时,PHP联系表单错误消息显示在同一页面上

时间:2012-10-23 16:01:41

标签: php url action

我有一个联系表单,用于检查点击“完成订单”时某些字段是否为空 它应刷新页面并显示错误消息。

但我正在使用页面重命名(一些简单的方法来重命名页面并在同一个php页面上有多个页面)

所以我把联系表格放在order.php?action = order

这是代码:

<?php
if(isset($_GET['action']) && $_GET['action'] != '') {
   $action = $_GET['action'];
} else {

}
?>
<?php
if(isset($action)) {
  if($action == 'order') {
?>
<div class="container">

            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
                <fieldset>


                    <?php if(isset($hasError)) { //If errors are found ?>
                        <p class="alert-message error">Opps! Looks like you forgot to fill some specify field, please <b>go back</p> and fill the empty field.</p>

                    <?php  } ?>

                    <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
                        <div class="alert-message success">
                            <p style="color: white;" ><strong>Your Order has been Successfully Sent!</strong></p>
                            <p style="color: white;" >Thank you for submitting your order, <strong><?php echo $pname;?></strong>! Please check your Private Messages folder at <strong><?php echo $forum;?></strong>, as we will get back to you soon. You will redirect back to homepage in less than 5 seconds.<meta HTTP-EQUIV="REFRESH" content="5; url=http://justxp.plutohost.net/jonydesigns/index.php"></p>
                        </div>
                    <?php } ?>
                        <div class="formwarp">
                        <div class="tos1">
<input type="checkbox" id="new_info_yes" class="box1"> I agree to the <a href="#" style="color: #4fc5cf;" >Terms of Service</a>.
    </div><br />
<div id="new_info_form">
                    <div class="clearfix">
                        <label for="subject">
                            <span class="coloring">Forums Name:</span>
                        </label>


                                            <div class="input">
                            <select name="forumn" id="forumn" class="span6 required" role="select" aria-required="true">
                                <option value="slct">Please select a forum</option>
                                <option value="hackforums">Hackforums</option>
                                <option value="sythe">Sythe</option>
                                <option value="rune-server">Rune-Server</option>
                            </select>
                        </div>
                    <br />
<div id="slct" class="colors">You haven't selected any forum.</div>
<div id="hackforums" class="colors" style="display:none"> <span id="brdr">HackForums:</span> Discount Percent: <font color="green">10%</font> </div>
<div id="sythe" class="colors" style="display:none"> <span id="brdr">Sythe:</span> Discount Percent: <font color="green">15%</font> </div>
<div id="rune-server" class="colors" style="display:none"> <span id="brdr">Rune-Server:</span> Discount Percent: <font color="green">15%</font> </div>
<script type="text/javascript">
    $(function() {
        $('#forumn').change(function(){
            $('.colors').hide();
            $('#' + $(this).val()).show();
        });
    });
    </script> </div>
<br />
                    <div class="clearfix">
                        <label for="name">
                            <span class="coloring">Forums Username:</span>
                        </label>
                        <div class="input">
                            <input type="text" name="forumuser" id="forumuser" value="" class="span6 required" role="input" aria-required="true" />
                        </div>
                    </div>

                    <div class="clearfix">
                        <label for="phone">
                            <span class="coloring">Personal Name:</span>
                        </label>
                        <div class="input">
                            <input type="text" name="pname" id="pname" value="" class="span6 required" role="input" aria-required="true" />
                        </div>
                    </div>


                    <div class="clearfix">
                        <label for="email">
                            <span class="coloring">Email Address:</span>
                        </label>
                        <div class="input">
                            <input type="text" name="email" id="email" value="" class="span6 required email" role="input" aria-required="true" />
                        </div>
                    </div>
                                        <div class="clearfix">
                        <label for="email">
                            <span class="coloring">PayPal Email Address:</span>
                        </label>
                        <div class="input">
                            <input type="text" name="ppemail" id="ppemail" value="" class="span6 required email" role="input" aria-required="true" />
                        </div>
                    </div>

                    <div class="clearfix">
                        <label for="subject">
                            <span class="coloring">Product Type</span>
                        </label>
                        <div class="input">
                            <select name="product" id="product" class="span6 required" role="select" aria-required="true">
                                <option>Please select a product\project</option>
                                <option>PSD to HTML</option>
                                <option>Web Design</option>
                                <option>Thread Design</option>
                                <option>Logo Design</option>
                                <option>Banner Design</option>
                                <option>Thread design + Signature</option>
                                <option>Other (Please type in additional information field)</option>
                            </select>
                        </div>
                    </div><br />
<div style="margin-left: -13%;" id="line"></div><br />
                    <div class="clearfix">
                        <span class="coloring">Information</span>
                        <div class="input">
                            <textarea rows="8" name="message" id="message" class="span10 required" role="textbox" aria-required="true"></textarea>
                        </div>
                    </div>
                    <div class="clearfix">
                        <span class="coloring">View Information</span>
                        <div class="input">
                            <textarea rows="8" name="messageview" id="messageview" class="span10 required" role="textbox" aria-required="true"></textarea>
                        </div>
                    </div>
                    <div class="clearfix">
                        <label for="phone">
                            <span class="coloring">Additional Information:</span>
                        </label>
                        <div class="input">
                            <input type="text" name="additional" id="additional" value="Additional information regarding the product" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" class="span6 required" role="input" aria-required="true" />
                        </div>
                    </div>
                    <div class="clearfix">
                        <label for="phone">
                            <span class="coloring">PSD link:</span>
                        </label>
                        <div class="input">
                            <input type="text" name="psd" id="psd" value="PSD link here" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" class="span6 required" role="input" aria-required="true" />
                        </div>
                    </div>
                    <div class="clearfix">
                        <label for="phone">
                            <span class="coloring">Your Budget:</span>
                        </label>
                        <div class="input">
                            <input type="text" name="money" id="money" value="$0.00 Enter your budget" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" class="span6 required" role="input" aria-required="true" />
                        </div>
                    </div>
                    <div class="actionss">
                        <input type="submit" value="Complete Order" name="submit" id="submitButton" title="Click here to submit your message!" />
                    </div>
                </fieldset>
            </form>
            </div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){

    $('input#upload_yes').change(function(){
        if($(this).is(':checked')) {
            $("#upload_form").show();
        } else {
            $("#upload_form").hide();
        }
    });

    $('input#new_info_yes').change(function(){
         if($(this).is(':checked')) {
                $("#new_info_form").slideDown(500);
        } else {
                $("#new_info_form").delay(300).slideUp(500);;
        }   
    });

    //Trigger the change event so the divs are initially shown or hidden.
    $('input[type=checkbox]').trigger('change');

});
</script>
<?php
  } 
  elseif($action == 'contact') {
  ?>
 <!-- action=contact info here -->
<?php
  }
}
?>

这会启动页面?action = order

<?php
if(isset($action)) {
  if($action == 'order') {
?>

显示表格,一切正常.. 但是,当您填写所有内容并单击完成订单时,它会重定向到order.php并且它不会显示任何警报,如果订单已发送,它应显示此警报:

                <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
                    <div class="alert-message success">
                        <p style="color: white;" ><strong>Your Order has been Successfully Sent!</strong></p>
                        <p style="color: white;" >Thank you for submitting your order, <strong><?php echo $pname;?></strong>! Please check your Private Messages folder at <strong><?php echo $forum;?></strong>, as we will get back to you soon. You will redirect back to homepage in less than 5 seconds.<meta HTTP-EQUIV="REFRESH" content="5; url=http://justxp.plutohost.net/jonydesigns/index.php"></p>
                    </div>
                <?php } ?>

如果某些内容为空,则应显示此错误:

                <?php if(isset($hasError)) { //If errors are found ?>
                    <p class="alert-message error">Opps! Looks like you forgot to fill some specify field, please <b>go back</p> and fill the empty field.</p>

                <?php  } ?>

但它根本没有显示错误,只是将它重定向到order.php页面但如果成功则只发送电子邮件但不会显示任何警告。

我该如何修复它或导致它发生的原因?

http://justxp.plutohost.net/jonydesigns/order.php?action=order

抱歉这个庞大的代码,我只是不知道如何更清洁地问这个问题。

2 个答案:

答案 0 :(得分:0)

当错误发生时你不会停止脚本执行,因此代码的测试仍在继续。

这不是一个理想的解决方案,而是编写代码的方式......

你可以在这里输入die()函数来阻止它:

<?php if(isset($hasError)) { //If errors are found ?>
  <p class="alert-message error">Opps! Looks like you forgot to fill some specify field, please <b>go back</p> and fill the empty field.</p>
  die(); //See me here!
<?php  } ?>

答案 1 :(得分:0)

我解决了这个问题,

我把错误部分移出了页面,到了开头,现在检查是否有错误,如果是,它会显示错误或警告。

很抱歉发布这么简单的问题^^