1

Hi the website i have built has multiple forms that i want to handle using AJAX, each request goes to a separate PHP page and returns a string.

HTML

<form name="addUserModule" id="addUserModule" action=""  method="POST">
<input type="checkbox" name="moduleToADD[]" value="1">
<input type="submit" value="Save" id="addUserModuleSubmit">

<form action="" method ="post" id="addNewModule" name="addNewModule">
<input type="text" name="newModule"> 
<input type="submit" value="Add Module" id="addNewModuleSubmit">

Jquery

$('#addUserModule').submit(function(e){
 e.preventDefault(); //Prevent the normal submission action
 var form = this;
  $.ajax({
url:'addUserModules.php',
data:$(form).serialize(),
type:'POST',
success: function(msg) {
        displayNotification(msg);
    }
});   
});

$('#addNewModule').submit(function(e){
 e.preventDefault(); //Prevent the normal submission action
 var form = this;
  $.ajax({
url:'addNewModule.php',
data:$(form).serialize(),
type:'POST',
success: function(msg) {
        displayNotification(msg);
    }
});   
});

The first form works fine, but the second form runs the same function that i made for the first form. Im using the HTML id's to determine the difference.

0

1 Answer 1

1

In your html code you havent closed your form and hence it is behaving like that.

<form name="addUserModule" id="addUserModule" action=""  method="POST">
<input type="checkbox" name="moduleToADD[]" value="1">
<input type="submit" value="Save" id="addUserModuleSubmit">
</form>
<form action="" method ="post" id="addNewModule" name="addNewModule">
<input type="text" name="newModule"> 
<input type="submit" value="Add Module" id="addNewModuleSubmit">
</form>

Now it will work fine.

Check this fiddle JSFiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Good catch ... you have a keen eye :)
darn, thanks so much..any tips for keeping an eye out for things like that in the future ecspeciallay when doing it in PHP?

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.