页面跳转PHP表单提交

时间:2014-06-11 21:29:59

标签: php mysql forms

我在一个寻呼机网站上使用php表单。我遇到了两个问题。首先是表单在提交时跳转到页面顶部。我已经尝试了一些不同的东西来防止这种情况,但似乎都没有成功。第二个是表单会话保持启动状态,如果用户点击ctrl-r,则会在我的数据库中重新提交空白。

如果有人知道如何否定页面跳转提交我体验到这将是惊人的。

感谢,

<?php
if(isset($_POST['add']))
{
$dbhost = 'XXX';
$dbuser = 'XXX';
$dbpass = 'XXX';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $form_name     = addslashes ($_POST['form_name']);
   $form_email    = addslashes ($_POST['form_email']);
   $form_phone    = addslashes ($_POST['form_phone']);
   $form_linkedin = addslashes ($_POST['form_linkedin']);

   $form_q1       = addslashes ($_POST['form_q1']);
   $form_q2       = addslashes ($_POST['form_q2']);
   $form_q3       = addslashes ($_POST['form_q3']);
   $form_q4       = addslashes ($_POST['form_q4']);
   $form_q5       = addslashes ($_POST['form_q5']);
}
else
{

   $form_name     =  $_POST['form_name'];
   $form_email    =  $_POST['form_email'];
   $form_phone    =  $_POST['form_phone'];
   $form_linkedin =  $_POST['form_linkedin'];

   $form_q1       =  $_POST['form_q1'];
   $form_q2       =  $_POST['form_q2'];
   $form_q3       =  $_POST['form_q3'];
   $form_q4       =  $_POST['form_q4'];
   $form_q5       =  $_POST['form_q5'];
}

$sql = "INSERT INTO XXX".
       "(form_name, form_email, form_phone, form_linkedin, form_q1, form_q2, form_q3, form_q4, form_q5) ".
       "VALUES('$form_name','$form_email','$form_phone', '$form_linkedin', '$form_q1', '$form_q2', '$form_q3', '$form_q4', NOW())";
mysql_select_db('XXX');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
    die("<section id='thank_you'><h1>SOMETHING WENT WRONG</h1><p class='about_body_txt'>Click <a href='XXX'>here</a> to resubmit</p></section>"); 
}
    echo("<section id='thank_you'><h1>THANK YOU</h1><p class='about_body_txt'>Selected participants will be notified by July 8, 2014. Click <a href='XXX'>here</a> to return to the main site.</p></section>");
    mysql_close($conn);
}
else
{
?>
  <form id="gfbm_form" class="theForm" role="form" autocomplete="off" method="POST">

                    <h2>APPLICATIONS DUE<br/>
                <span class="medium_txt">THURSDAY, JULY 3RD AT 5PM, EST.</span></h2>



                    <div class="application">

                        <ul>
                            <li class="small_field">
                                <span><label class="label_field">Name</label></span>
                                <br/>
                                <input id="form_name" class="name input_field" type="text" name="form_name" />
                            </li>

                            <li class="small_field">
                                <span><label class="label_field">Email</label></span>
                                <br/>
                                <input id="form_email" class="email input_field" type="text" name="form_email" />
                            </li>

                            <li class="small_field_2">
                                <span><label class="label_field">Phone</label></span>
                                <br/>
                                <input id="form_phone" class="phone input_field" type="text" name="form_phone" />
                            </li>


                            <li class="small_field_2">
                                <span><label class="label_field">LinkedIn</label></span>
                                <br/>
                                <input id="form_linkedin" class="linkedin input_field" type="text" name="form_linkedin" />
                            </li>


                            <li class="essay_question">
                                <span><label class="label_field">QUESTION 1</label></span>
                                <br/>
                                <textarea id="form_q1" class="q1 input_field_big" type="text" name="form_q1"></textarea>
                            </li>

                            <li class="essay_question">
                                <span><label class="label_field">QUESTION 2</label></span>
                                <br/>
                                <textarea id="form_q2" class="q2 input_field_big" type="text" name="form_q2"></textarea>
                            </li>


                            <li class="essay_question">
                                <span><label class="label_field">QUESTION 3</label></span>
                                <br/>
                                <textarea id="form_q3" class="q3 input_field_big" type="text" name="form_q3"></textarea>
                            </li>


                            <li class="essay_question">
                                <span><label class="label_field">QUESTION 4</label></span>
                                <br/>
                                <textarea id="form_q4" class="q4 input_field_big" type="text" name="form_q4"></textarea>
                            </li>
                        </ul>



                            <input type="checkbox" required id="checkbox-2-1" class="regular-checkbox big-checkbox" />
                            <label class="check_text">
                                <p>YES, I'LL BE AVAILABLE FOR ALL OF THOSE DATES.</p>
                            </label>


                            <input type="checkbox" required id="checkbox-2-2" />
                            <label class="check_text">
                                <p>I have read and understand the terms and conditions.</p>
                            </label>



<input name="add" type="submit" class="submit btn btn-default" id='submit' value="APPLY">




                </form> 

1 个答案:

答案 0 :(得分:1)

submit提交页面然后重新加载它,再次将您置于顶部。您可以创建一个HTML锚点,并使用onload javascript事件触发该事件,或者将用户移回相同的位置,但这与范围有关。