在jquery div show()中提交表单$ _SERVER ['PHP_SELF']

时间:2012-08-16 09:19:15

标签: php jquery forms

我有一系列的div style =“display:none;”我使用jquery show hide为每个div。

其中一个div是一个选择菜单,我想将表单提交给自己$ _SERVER ['PHP_SELF']。我如何提交表格但仍保留在正确的当前div中。

提前致谢。

   <div style="display:none;">
    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
        <select id="employee_user" name="employee_user" onchange="this.form.submit();">
        <option value="">Select Staff</option>

    <?php  // $Id: emails.php, 27/06/2011 mbrennand
    $users_in_department = mysql_query('SELECT DISTINCT(employee) FROM holiday_entitlement_academic WHERE line_manager = \'' . $username. '\'');
    $users_in_department2 = mysql_query('SELECT DISTINCT(employee) FROM holiday_entitlement_business WHERE line_manager = \''.$username.'\'');

    while($user_in_dept = mysql_fetch_array($users_in_department)) {
    echo '<option value="'.$user_in_dept['employee'].'">'.$user_in_dept['employee'].'</option>';
    }
    while($user_in_dept2 = mysql_fetch_array($users_in_department2)) {
    echo '<option value="'.$user_in_dept2['employee'].'">'.$user_in_dept2['employee'].'</option>';
    }
    ?>    
    </select>
    </form>
    </div>


    <div style="display:none;">
    </div>

    <div style="display:none;">
    </div>

    <div style="display:none;">
    </div>

修改.... 修改代码

<script type="text/javascript">
$('#employee_user').on('change', function(){
    $.post('SELF', $('form input,form select').serializeArray(), function(data){
         // Form has submitted
    });
});
</script>

<div style="float:right;">
<form action='' method='post' name='form_filter' >
    <select id="employee_user" name="employee_user">
    <option value="">Select Staff</option>

<?php  // $Id: emails.php, 27/06/2011 mbrennand
$users_in_department = mysql_query('SELECT DISTINCT(employee) FROM holiday_entitlement_academic WHERE line_manager = \'' . $username. '\'');
$users_in_department2 = mysql_query('SELECT DISTINCT(employee) FROM holiday_entitlement_business WHERE line_manager = \''.$username.'\'');

while($user_in_dept = mysql_fetch_array($users_in_department)) {
echo '<option value="'.$user_in_dept['employee'].'">'.$user_in_dept['employee'].'</option>';
}
while($user_in_dept2 = mysql_fetch_array($users_in_department2)) {
echo '<option value="'.$user_in_dept2['employee'].'">'.$user_in_dept2['employee'].'</option>';
}
?>    
</select>
</form>
</div>
<h2>STAFF HOLIDAY LEAVE</h2>
<br/>

<?php
$username_indept = $_POST["employee_user"];

$is_academic_result = mysql_query('SELECT * FROM holiday_entitlement_academic WHERE employee = \'' . $username_indept . '\'');
$is_business_result = mysql_query('SELECT * FROM holiday_entitlement_business WHERE employee = \'' . $username_indept . '\'');

if($is_academic = mysql_fetch_array($is_academic_result)) {
    switch($is_academic['units']) {
        case 'days':
            include_once('admin_academic_days.php');
            break;
        case 'hours':
            include_once('admin_academic_hours.php');
            break;
        default:
            break;
}
} else if ($is_business = mysql_fetch_array($is_business_result)) {
    switch($is_business['units']) {
        case 'days':
            include_once('admin_business_days.php');
            break;
        case 'hours':
            include_once('admin_business_hours.php');
            break;
        default:
            break;
    }
} 
else {
         include_once('no_record.php');
}
?>

2 个答案:

答案 0 :(得分:3)

有三种方法可以解决这个问题:

  • 使用AJAX,在这种情况下,页面不会更改并保持静态,在这种情况下,您保留在正确的div中。
  • 使用与AJAX相同的iframe目标
  • 使用表单元素来了解填写了多少表单,并隐藏/显示页面的部分内容。

另外,作为提示,请勿在表单提交中使用PHP_SELF。这是不安全的。

AJAX提交的一个例子:

$('#employee_user').on('change', function(){
    $.post('SELF', $('form input,form select').serializeArray(), function(data){
         // Form has submitted
    });
});

答案 1 :(得分:2)

两个选项:

  1. 通过ajax提交表单

  2. 在您的请求中提供一些信息,以确定哪些div最初应在页面重新加载时显示。

    这可以通过隐藏输入,动作查询参数action="?active_div=3"或类似的东西来完成。

  3. 顺便说一句,如果提交到当前网址,您可以将action属性留空。