I have some trouble to instert a condition inside the function(data). It works for the second condition (i.e. else) but not for the first one. In other words when data="success" in my php, nothing appears in my html file. Furthermore console.log(data) is "ReferenceError: data is not defined " in both cases even if it works for the second condition.
Also in my networks(console) in Firefox I can see the data send from php in the second condition but for the first one it says that the data was not saved.
$errors is an array in php.
My code in ajax:
$.post('register3.php', {
username : $("#username").val(),
password : $("#password").val(),
passwordconfirm : $("#passwordconfirm").val(),
email : $("#email").val()
},
function(data){
if(data=="success"){
$("#results").html(data);
}
else{
$("#errors1").html(data.username);
$("#errors2").html(data.password);
$("#errors3").html(data.email);
}
My code in php
<?php
if(!empty($errors)){
header('Content-Type: application/json; Charset=utf-8');
$data = json_encode($errors);
echo $data;
}
else {
$data='success';
echo $data;
}
?>
My code in HTML
<div class="flash2">
<ul>
<li><div id="errors1"> </div> </li>
<li><div id="errors2"> </div> </li>
<li> <div id="errors3"> </div> </li>
<li> <div id="results"> </div> </li>
<ul>
</div>