I have a problem with placing a result from ajax response into a html element. Here is my php which is inserting things into database, the inserting part works but can not get the result into div.
include('konekcija.php');
if ($_POST['form_submitted'] = 1) {
$aktKod = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$sifra = $_POST['password'];
$korIme = $_POST['username'];
$email = $_POST['email'];
$upit = "INSERT INTO korisnici(Username,Password,Email,Status,AktivacioniBroj,Uloga)
VALUES ('$korIme','$sifra','$email','verify',$aktKod,'user') ";
if(mysql_query($upit)) { $return['success'] = "Uspesna registracija"; }
else { $return['error'] = "neuspesna registracija"; }
echo json_encode($return);
mysql_close($conn); }
Jquery code:
$(document).ready(function() {
$('#prijava').click(function() {
$.ajax({
url: "registracija.php",
type: "POST",
dataType:"json",
data: {form_submitted:1, username: $('#user').val(), password: $('#pass').val(), email: $('#email').val()},
succes: function(data) { $('#errorCol').html(data.success); }
}); }); });
And my form in html:
<form method="POST" name="register" action="" onsubmit="return false">
<table id="register">
<tr>
<td style="padding-bottom:30px; padding-right:20px;"><span style=" font-size:14px; color:#666;">Korisnicko Ime:</span></td>
<td><input id="user" type="text" name="username" /></td>
</tr>
<tr>
<td style="padding-bottom:30px; padding-right:20px;"><span style=" font-size:14px; color:#666;">Sifra:</span></td>
<td> <input id="pass" type="password" name="password" /></td>
</tr>
<tr>
<td style="padding-bottom:30px; padding-right:20px;"><span style=" font-size:14px; color:#666;">Ponovite Sifru:</span></td>
<td> <input id="pass2" type="password" name="password" /></td>
</tr>
<tr>
<td style="padding-bottom:30px;"><span style=" font-size:14px; color:#666;">Email:</span></td>
<td><input id="email" type="text" name="email" /> <input type="hidden" name="form_submitted" value="1"/></td>
</tr>
<tr>
<td style="padding-bottom:30px;" colspan="2" align="center"><input id="prijava" type="submit" value="Prijavi" /></td>
</tr>
<tr>
<td id="errorCol" colspan="2" align="center"></td>
</tr>
</table>
</form>
So it inserts data into database, but the part where I need to display the response into element with the id of “errorCol” doesn’t work, it just stays empty. Tried to return json array.
You’ve got a couple of typos in your code.
The php test to see if the form is submitted
(
$_POST['form_submitted'] = 1) is an assignment, it should be adouble ==, otherwise it will always be true.
In your jquery, the “success” callback is misspelled, you have
“succes”