I have a page with 3 forms
<form id="form1" method="post">
<input type="text" name="name" value="">
<input type="text" name="city" value="">
</form>
<form id="form2" method="post">
<input type="text" name="space" value="">
<input type="text" name="time" value="">
</form>
<form id="form3" method="post">
<input type="text" name="tv" value="">
<input type="text" name="genre" value="">
</form>
I want to send the forms to different php pages using using the form id in ajax.
here is the script im trying.
<script>
$('#form1').on("submit", function(event){
event.preventDefault();
$.ajax({
url:"form1.php",
method:"POST",
data:$('#form1').serialize(),
beforeSend:function(){
$('#insert').val("Inserting");
},
});
});
$('#form2').on("submit", function(event){
event.preventDefault();
$.ajax({
url:"form2.php",
method:"POST",
data:$('#form2').serialize(),
beforeSend:function(){
$('#insert').val("Inserting");
},
});
});
$('#form3').on("submit", function(event){
event.preventDefault();
$.ajax({
url:"form3.php",
method:"POST",
data:$('#form3').serialize(),
beforeSend:function(){
$('#insert').val("Inserting");
},
});
});
</script>
Is this the right way to do it as I cannot find any examples. Any example or link will be much appreciated.
Update**
enter image description here shows no error
here is the php
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/core/include/connection.php";
include_once($path);
if(!empty($_POST)){
$name = mysqli_real_escape_string ($databaseLink,$_POST['name']);
$city= mysqli_real_escape_string ($databaseLink,$_POST['city']);
$sql = "INSERT INTO form1 SET name='$name' city='$city'";
$sql = mysqli_query($databaseLink,$sql);
}
?>