1

Here is my code:

<form action='insert.php' method='post' id='myform' >
    <input type='hidden' name='tmdb_id'/>
    <button id='insert'>Insert</button>
    <p id='result'></p>
    <script src='insert.js'></script>
</form>

<form action='insert.php' method='post' id='myform' >
    <input type='hidden' name='tmdb_id'/>
    <button id='insert'>Insert</button>
    <p id='result'></p>
    <script src='insert.js'></script>
</form>

<form action='insert.php' method='post' id='myform' >
    <input type='hidden' name='tmdb_id'/>
    <button id='insert'>Insert</button>
    <p id='result'></p>
    <script src='insert.js'></script>
</form>

Here is: insert.js

$('#myform').submit(function(){
    return false;
});

$('#insert').click(function(){
    $.post(     
        $('#myform').attr('action'),
        $('#myform :input').serializeArray(),
        function(result){
            $('#result').html(result);
        }
    );
});

The Problem:

Only the code inside first <form></form> tag works. If i click on submit button of other<form></form> tags, then I get re-directed to insert.php file.

What is the problem? If it is related to same id thing, then I would not like to add different id's. for each new form

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Toby
  • 717
  • 2
  • 8
  • 19

3 Answers3

0

ID's Must Be Unique, specifically because it will cause problems in JavaScript and CSS when you try to interact with those elements.

Assuming you load insert.js once into the page:

<form action='insert.php' method='post' class='myform' >
    <input type='hidden' name='tmdb_id'/>
    <button class='insert'>Insert</button>
    <p class='result'></p>
</form>

<form action='insert.php' method='post' class='myform' >
    <input type='hidden' name='tmdb_id'/>
    <button class='insert'>Insert</button>
    <p class='result'></p>
</form>

<form action='insert.php' method='post' class='myform' >
    <input type='hidden' name='tmdb_id'/>
    <button class='insert'>Insert</button>
    <p class='result'></p>
</form>

Once you have that done you can use a single jQuery function to handle all of the forms:

$(function() {
    $('.insert').click(function(e){
        e.preventDefault();
        $.post(     
            $(this).closest('.myform').attr('action'),
            $(this).closest('.myform :input').serializeArray(),
            function(result){
                $(this).closest('.myform').find('.result').html(result);
            }
        );
    });
});

You do have to do some DOM traversal to get the form elements related to the insert button.

  1. $(this).closest('.myform') finds the parent of the insert button
  2. $(this)closest(.myform').find('.result') finds the child element with the class of 'result' to add the results to.
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

You don't need multiple instances of insert.js

Use common classes for all the forms and elements instead of duplication of ID's.

We can also use the submit event to post the data

HTML

<form action='insert.php' method='post' class='myform'>
  <input type='hidden' name='tmdb_id' />
  <button class='insert'>Insert</button>
  <p class='result'></p>

</form>

<form action='insert.php' method='post' class='myform'>
  <input type='hidden' name='tmdb_id' />
  <button class='insert'>Insert</button>
  <p class='result'></p>

</form>
<!-- load once per page -->
<script src='insert.js'></script>

JS

$('.myform').submit(function(){
    // `this` is the instance of myForm class the event occurred on
    var $form = $(this),
        url = $form.attr('action'),
        data = $form.serializeArray(); // or use serialize() which is more common

    $.post( url, data, function(result){
         // look inside this form instance for element to populate
         $form.find('.result').html(result);
     });

    return false;
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
-2

Try something like below using jQuery Ajax:

$(".submit-button-class").click(function() {
  var formData = $(this).closest('form').serialize();
  $.ajax({
    type: "POST",
    url: "YOUR URL",
    data: formData,
    dataType: 'json',
    beforeSend: function() {
      //show loading image while processing
    },
    success: function(resp) {
      // do something here
    },
    error: function(e) {
      alert('error: ' + JSON.stringify(e));
    }
  });
});  

Note: you should use class selector instead of id selector for submit button

Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35
  • Same error as mentioned in the question. Only first form works. – Toby Jul 18 '17 at 14:29
  • @Toby please check my `Note` in answer. use class selector instead of id selector – Dhaval Bharadva Jul 18 '17 at 14:34
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Jay Blanchard Jul 18 '17 at 14:36
  • Same problem, if I keep this class `$(".submit-button-class")`, If i replace it with `myform` class. Even the first form redirects me to insert.php + all the forms get echo'ed again (total 6 form) before redirect – Toby Jul 18 '17 at 14:39
  • @Toby you should add `submit-button-class` to your `` – Dhaval Bharadva Jul 18 '17 at 14:42
  • Now the first form also re-directs me to insert.php page – Toby Jul 18 '17 at 14:50
  • Why does the OP need to add a new class? And why can't they use the `$.post` AJAX shorthand? – Jay Blanchard Jul 18 '17 at 14:55
  • I don't know how $.post works, but I will try to google it, thanks. @JayBlanchard – Toby Jul 18 '17 at 14:56
  • @Toby you're already using that function. I was asking this answerer why he wanted you to change something which works perfectly fine for your purposes. – Jay Blanchard Jul 18 '17 at 14:57