我有这个表格可以更新 db 上的用户信息。但是,此功能不适用于 ajax(使用带刷新的简单提交)。
这将表单序列化数据发布到服务器 jQuery
$('#commit-changes').click(function(){
$('#validation').submit(function(){
$.ajax({
type: "POST",
data: $("#validation").serialize(),
cache: false,
url:"modules/user/updateuser.php",
success : function(data){
$("#response-update").html(data);
},
error:function(){
alert("ERROR");
}
});
});
});
这是输出标记 HTML 的 HTML
$rcs_roles = $user->getRoles();
$role ='';
foreach($rcs_roles as $roles)
{
if($roles->role_number == $rcs_user->permissao)
$role .= '<option value="'.$roles->role_number.'" selected="selected">'.$roles->role.'</option>';
else
$role.= '<option value="'.$roles->role_number.'">'.$roles->role.'</option>';
}
if($rcs_user->activo == 0)
{
$activo = '<input type="checkbox" name="activo" class="on_off_checkbox" value="1" />';
}
else
{
$activo = '<input type="checkbox" name="activo" class="on_off_checkbox" checked="checked" value="1" />';
}
$response = '';
$response.='<form id="validation" action="" method="post">
<fieldset >
<input type="hidden" name="user_id"
value="'.$_POST['user_id'].'"/>
<legend>Actualizar Dados Utilizador</legend>
<div class="section ">
<label>Nome<small>Insira o seu nome</small></label>
<div>
<input type="text" class="validate[required,custom[onlyLetterSp]] large" name="nome" id="f_required" value="'.utf8_encode($rcs_user->nome).'">
</div>
</div>';
$response.='<div class="section ">
<label> Email<small>Insira o seu email</small></label>
<div>
<input type="text" class="validate[required,custom[email]] large" name="email" id="e_required" value="'. utf8_encode($rcs_user->email).'">
</div>
</div>';
$response.= '<div class="section">
<label>Permissões<small>Seleccione o tipo de utilizador </small></label>
<div>
<select class="medium" name="role">
'.$role.'
</select>
</div>
</div>
<div class="section">
<label>Activo<small>Activar utilizador</small></label>
<div>
'.$activo.'
<span class="f_help">ON / OFF </span>
</div>
</div>
<div class="section last">
<div>
<a id="commit-changes" class="uibutton submit_form" name="submit" >Gravar</a><a class="uibutton special" onClick="ResetForm()" title="Limpar Formulário" >Limpar Formulário</a>
</div>
</div>
</fieldset></form>';
然后服务器端处理 PHP
$response='';
$id_user = $_POST['user_id'];
$name = utf8_encode($_POST['nome']);
$email = utf8_encode($_POST['email']);
$permitions = $_POST['role'];
if(!isset($_POST['activo']))
{
$active = 0;
}
else
{
$active = 1;
}
$user = new Users();
try
{
$user->updateUsers($name, $email, $permitions, $active, $id_user);
$response = "SUCESSO";
}
catch (Exception $e)
{
$response = "ERRO".$e->getMessage();
}
echo $response;
很高兴我能得到所有帮助