gigiopasticcio
gigiopasticcio

Reputation: 51

Data of a Form sent in 2 different pages

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>

The fact is that I don't know if I inserted it right and I don't know how to call the function on the button.

Upvotes: 0

Views: 259

Answers (1)

Emmanuel Ogwo
Emmanuel Ogwo

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

Related Questions