I'm new to Javascript and I couldn't find this information so far.
I'd like to create a button which, when clicked, creates a new entry in my database, and another one which removes a specific entry when clicked. The reason I don't want to use PHP is because I don't want the page to reload when the button is clicked.
Where can I find information on how to achieve this?
EDIT
Okay I found this:
<script type="text/javascript">
$(document).on('click','#save',function(e) {
var data = $("#save-form").serialize();
$.ajax({
data: data,
type: "post",
url: "save.php",
success: function(data){
alert("Data Save: " + data);
}
});
});
</script>
My HTML:
<form id="save-form" method="post">
<input id="save" type="submit" value="Save" name="save-submit" />
</form>
My PHP file (save.php), which creates a new record in my database, works, but only if my form's action
is action="save.php"
. What should I put in my form's action
to trigger the script?
Also, it seems like the submit button still reloads the page (even if my form has no action
).