0

I'm getting my multiple forms using a while loop (fetch data in the database).

<form id="form" class="form-horizontal" method="post" >
<input type="text" class="form-control" name="name" value="test1">
<input type="text" class="form-control" name="car_type">
<button type="submit" class="buttona" id="buttona">Send</button>
</form>
<form id="form" class="form-horizontal" method="post" >
<input type="text" class="form-control" name="name" value="test2">
<input type="text" class="form-control" name="car_type" value="test2">
<button type="submit" class="buttona" id="buttona">Send</button>
</form>

Here's my ajax (It only works in the 1st form but the rest not working):

$(document).ready(function(){ 
 $(".form").submit(function(e) {
       e.preventDefault();
        $("#buttona").html('...');
        $("#buttona").attr("disabled", "disabled");
        sendInfo();

     });
});

Function for ajax:

function sendInfo() { 
        $.ajax({
          type: 'POST',
          url: '../process.php',
          data: $(".form").serialize(),
          success: function(data){
             if(data == 'Success') {
                $('#text_errora').html('added');
             }else {
               $('#text_errora').html('not aadded');                    
               }
          }
      })
        return false;
    }

How can I set or how ajax will recognize the button I click/submit to process the form?

2 Answers 2

1
  1. You don't close your <form> tag.
  2. You use the same id twice.
  3. You select anything with class form, not ID form.

Actually, I am amazed it works even one time.

Try this (no need to touch the JavaScript), your form should submit, but the button changing might not work (you use identical IDs there too; tip: an id has to be unique within the entire HTML DOM).

<form class="form form-horizontal" method="post" >
<input type="text" class="form-control" name="name" value="test1">
<input type="text" class="form-control" name="car_type">
<button type="submit" class="buttona" id="buttona">Send</button>
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi bro I checked it has a close tag. I also changed the ID (using $i++). but how can I call the ID form in ajax?
Show the actual code please, we might be able to help better. (And I am not your bro.)
I echo all the value inside the form then use 1 ajax only the edit/send the data again.
0

Using "this" keyword inside submit handler, you will receive a reference to the form to which the clicked button belongs.

$(document).ready(function(){ 
$(".form").submit(function(e) {
       e.preventDefault();
        var form_to_submit = this;
        $("#buttona").html('...');
        $("#buttona").attr("disabled", "disabled");
        sendInfo(form_to_submit);

     });
});


function sendInfo(form_to_submit) { 
    $.ajax({
        type: 'POST',
        url: '../process.php',
        data: $(form_to_submit).serialize(),
        success: function(data){
            if(data == 'Success') {
                $('#text_errora').html('added');
            }else {
                $('#text_errora').html('not aadded');                    
            }
        }
    })
    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.