Reputation: 51
I have a form where the data is sent through an email when I click on a button.
The issue I have is that I want the same data to be inserted into a table.
I found a solution on here: send the form values to multiple pages
but I don't know how to insert that portion of code in mine.
This is the code I put in my page:
<script language="Javascript">
<!--
$('form').submit(function() {
$.ajax({
async: false,
method: 'POST',
url: 'submit.php',
data: $( this ).serialize(),
success: function() {
window.location.href = "http://stackoverflow.com"; //redirect to this page
}
});
$.ajax({
async: false,
method: 'POST',
url: 'appuntamento.php',
data: $( this ).serialize(),
success: function() {
window.open("http://www.stackoverflow.com"); //this page will be open in new tab
}
});
});
-->
</script>
Upvotes: 0
Views: 259
Reputation: 11
You don't exactly have to call the function on the button. If you include the js in your html or php file which contains the form tag <form>
once the button with type="submit" is clicked the event will be triggered.
However, if you must use a button that doesn't automatically trigger form submit on click then you can trigger manually by adding an id to the form and submit onclick of the button
if you have
<form id="target">
<input type="text" value="Hello there">
<input type="submit" value="Go">
</form>
You can do this
$('form').submit(function() {
$.ajax({
async: false,
method: 'POST',
url: 'submit.php',
data: $( this ).serialize(),
success: function() {
window.location.href = "http://stackoverflow.com"; //redirect to this page
}
});
$.ajax({
async: false,
method: 'POST',
url: 'appuntamento.php',
data: $( this ).serialize(),
success: function() {
window.open("http://www.stackoverflow.com"); //this page will be open in new tab
}
});
});
If you have
<form id="target">
<input type="text" value="Hello there">
<button id="submit-form">Submit</button
</form>
you can have this
$( "#submit-form" ).click(function() {
$( "#target" ).submit();
});
$('form').submit(function() {
$.ajax({
async: false,
method: 'POST',
url: 'submit.php',
data: $( this ).serialize(),
success: function() {
window.location.href = "http://stackoverflow.com"; //redirect to this page
}
});
$.ajax({
async: false,
method: 'POST',
url: 'appuntamento.php',
data: $( this ).serialize(),
success: function() {
window.open("http://www.stackoverflow.com"); //this page will be open in new tab
}
});
});
Upvotes: 1