Reputation:
I wannt it to update a div and show an alert but neither are working
<script>
$(document).ready(function() {
$('#submit').click(function(){
$.ajax({
type: 'POST',
url: '<?php echo URL;?>user/registeruser/',
data: $('#username').val() + '/' + $('#password').val() + '/' $('#email').val() + '/' $('#firstname').val() + '/' + $('#lastname').val() + '/' $('#securityq1').val() + '/' + $('#securitya2').val() + '/' + $('#securityq2').val() + '/' + $('#securitya2').val(),
success: function(msg) {
$('#registermessage').html(msg);
alert("thgfg");
}
});
})});
</script>
<button type="submit" name="submit" class="btn btn-danger">Register</button> </p>
Upvotes: 0
Views: 78
Reputation: 73301
The line here
$('#submit').click(function(){
could be translated to
jQuery find the element with the ID submit, listen for a click event on this element and run the following function.
In your case, jQuery listens, but can't hear something because there is no element the event could get triggered by. You need to give the element an ID if you use the #.
<button type="submit" name="submit" <!-- this >> id="submit" --> class="btn btn-danger">
To prevent your button form from beeing submitted via standard html, you need to add
$('#submit').click(function(e){
e.preventDefault();
to your click function.
Try to change it to:
...
$.ajax({
type: 'POST',
url: 'user/registeruser',
data: { username: $('#username').val() ,
password: $('#password').val() ,
email : $('#email').val() ,
firstname:$('#firstname').val() ,
lastname: $('#lastname').val() ,
securityq1:$('#securityq1').val() ,
securitya2:$('#securitya2').val(),
securityq2:$('#securityq2').val() ,
securitya2:$('#securitya2').val()
}
success: function(msg) {
$('#registermessage').html(msg);
alert("thgfg");
}
});
...
I hope your jQuery script is below your html, if not, you need to change this too.
Upvotes: 2
Reputation: 1079
you should add id to the button..
<script>
$(document).ready(function() {
$('#submit').click(function(){
alert("gi")
$.ajax({
type: 'POST',
url: '<?php echo URL;?>user/registeruser/',
data: $('#username').val() + '/' + $('#password').val() + '/' $('#email').val() + '/' $('#firstname').val() + '/' + $('#lastname').val() + '/' $('#securityq1').val() + '/' + $('#securitya2').val() + '/' + $('#securityq2').val() + '/' + $('#securitya2').val(),
success: function(msg) {
$('#registermessage').html(msg);
alert("thgfg");
},error:function(){
alert("hi");
}
});
});
});
</script>
<button type="submit" id='submit' name="submit" class="btn btn-danger">Register</button>
Upvotes: 4