2012-08-16 35 views
1

我有一系列的div style =“display:none;”和即时通讯使用jQuery的显示每个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> 

编辑.... CODE改进

<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'); 
} 
?> 
+0

你是什么意思*“还停留在正确的当前股利。” *? – 2012-08-16 09:25:35

+0

停留在具有窗体的div中,显示的div。 – Codded 2012-08-16 09:42:13

+0

提交重新加载一个新页面,你不留任何东西 – 2012-08-16 09:55:44

回答

2

两个选项:

  1. 阿贾克斯

  2. 提交表单提供的信息的费用相加你请求将确定哪个div应该在页面重新加载时最初可见。

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

顺便说一句,如果提交到当前的URL,你可以离开action属性附加伤害空。

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 
    }); 
}); 
+0

请看我的问题。我用完整的代码修改了它。它不工作,你能看到任何问题吗? – Codded 2012-08-16 10:12:45

+0

@Codded是的我的代码只是一个例子,它不是一个复制和可粘贴的片段。 'SELF'需要指向你想要使用的页面。 – Sammaye 2012-08-16 10:15:19

+0

@Codded即PHP_SELF ='mine.php',因此'SELF'将是'mine.php'。 – Sammaye 2012-08-16 10:15:55