I have the following form for my users to change their email addresses.
<form enctype="multipart/form-data" id="email_change_form" method="post" action="/my_account/">
<table>
<tr>
<td>
<label for="id_email">E-mail address</label>:
</td>
<td>
<input name="email" value="a@a.com" maxlength="75" type="text" id="id_email" size="30" />
</td>
<td class="error_email">
</td>
</tr>
</table>
<input type="hidden" name="form_id" value="email_change_form" />
</form>
Upon clicking the save button (whose code is not shown), the form gets posted to the server ajaxly with the following jQuery code:
$(".save_button").click(function() {
var $form = $("#email_change_form")
url = $form.attr("action");
$.post(url, $form.serialize(),
function(data) {
//upon success, the server returns the new email (data.email) in a json dictionary.
},
"json"
);
}
})
Once the server received the POST, it needs to verify that the new email does not already exist in the database (ie. email needs to be unique). Say the server detects a duplicate email, I am not sure how to communicate the error back to the client-side. I was tempted to add an entry "duplicate_email: true" to the json dictionary and return it to the client-side. I, somehow, feel that this isn't the right approach.
Please advise the right approach(es) for this scenario. How should the server pass the error back to the client-side? And how should the error be captured at the client-side? Thanks.