I’ve got a page that needs POST data to get an ajax response. The information relying on the POST data displays fine. However, after that, if the user wants to update some of the displayed data, I want to make another ajax call to update it on the fly. Then comes the problem as the first script doesn’t get any POST data from the page that the user requests to update the information.
Right now I have this, which, obviously is giving me the problem
$(document).ready(function(){
var uid = <?php echo $_POST['user_id']; ?>;
$.ajax({
type: "POST",
data : {user_id:uid},
cache: false,
url:"modules/user/showuserdata.php",
success : function (data) {
$("#response").html(data);
}
})
});
$('#commit-changes').click(function(){
$('#commit-user').submit();
$.ajax({
type: "POST",
data: $("#commit-user").serialize(),
cache: false,
url:"modules/user/updateuser.php",
success : function(data){
$("#response-update").html(data);
}
})
});
The javascript error that chrome outputs is
$(document).ready(function(){
var uid = <br />
Uncaught SyntaxError: Unexpected token <
<b>Notice</b>: Undefined index: user_id in <b>C:\xampp\htdocs\inacesso\admin\edituser.php</b> on line <b>73</b><br />
;
I’ve ommited the html for a better reading, but if necessary please notice me.
Any hint on how I can bypass this? I didn’t want to use SESSION variables…
Glad for all the help I can have on this.
EDIT
HTML response of the showuserdata.php
$user = new Users();
$id = $_POST['user_id'];
$rcs_user = $user->getUserByID($id);
$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 commit-user" 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 href="" id="commit-changes" class="uibutton submit_form" name="submit">Submeter</a><a class="uibutton special" onClick="ResetForm()" title="Limpar Formulário" >Limpar Formulário</a>
</div>
</div>
</fieldset></form>';
echo $response;
The problem is that data is loaded through ajax calls.
I’ve solved this issue using .live or .on events of jQuery.
Thank you for your efforts!