Hi all,
I am newish to Ajax, I've used it in the past in very rare circumstances and I understand for the most part how ajax works to combine client side and server side by sending information off to the server and displaying returned information from whereever it sent it's data all without forcing the client to refresh. My Question is how do I get multiple forms on 1 page to send to different areas.
Here is the AJAX code I am using to send data FROM my html form to some php page, in this case database.php:
<script>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'database.php',
data: $('form').serialize(),
success: function (info) {
$(".the-return").html(
info
);
}
});
});
});
</script>
And here is the form I am using:
<form action="javascript:void(0);" class="js-ajax-php-json" method="post" accept-charset="utf-8">
<table cellpadding="10">
<tr>
<td><label>Item Type</label></td>
<td><select name="itemType" id="itemType" required><option value="Chair">Chair</option><option value="Couch">Couch</option><option value="Credenza">Credenza</option><option value="Desk">Desk</option><option value="File Cabinet (2)">File Cabinet (2)</option><option value="File Cabinet (3)">File Cabinet (3)</option><option value="File Cabinet (4)">File Cabinet (4)</option><option value="Phone">Phone</option><option value="Printer">Printer</option><option value="Projector">Projector</option><option value="Shelf">Shelf</option><option value="Table">Table</option></select><br/></td>
</tr>
<tr>
<td><label>Location</label></td>
<td><input type="text" name="location" id="location" value="Enter Location" required /><br /></td>
</tr>
<tr>
<td><label>Extra Info</label></td>
<td><input type="text" name="extraInfo" id="extraInfo" value="Enter Extra Info" /><br /></td>
</tr>
</table>
<input type="submit" name="submit" class="btn btn-primary" value="Submit form" />
</form>
This is used to insert data into the database which is done in database.php but if I want a second, third or fourth form all on this page with different functions, for instance searching the database, how do I send them to different files. Could I just send them all to database.php and use conditional statements to determine if some textarea is filled out which would be a search request or some check box is filled out which is a insert request. What is the best way? I thought about functions but I was unsure how I could specify with ajax which function I would want to send to.
I understand I may not be making much sense and I will try and answer any questions you may have about what I am trying to do to clear up any confusion.
Best
Andy