So I am messing with very stupid issue,There is normal HTML form which I am opening in Modal Box for updating the values.When I change the values of any Text Box and click on button for Ajax Call, there is Old values coming in POST instead of changed value.
For example if there is Value 5 and i changed it to 10 and fire Ajax but in POST data there is still 5.I am using on for getting current values.
Here is part of my HTML code :
<form action="" id="UpdateBind_data">
<input type="text" id="weightage" name="weightage" value="<?php echo $mapping_data->weightage; ?>">
<button type="button" class="btn btn-primary UpdateBind">Update</button>
</form>
Jquery :
$("body").on("click", ".UpdateBind", function() {
var Datastring = $("#UpdateBind_data").serialize();// Retrive the old values not the changed ones.
$.ajax({
type: "POST",
url: updatedbindlink,
data: Datastring,
datatype: "json",
cache: false,
success: function(response) {
if(response.res)
{
alert("Mapping data successfully Inserted");
}
else
{
//error
}
return false;
}
});
});
Thanks in advance.
Maybe it because your forms submits before executing the JQuery bind? Try this:
$("body").on("click", ".UpdateBind", function(e) {
e.preventDefault();
var Datastring = $("#UpdateBind_data").serialize();// Retrive the old values not the changed ones.
$.ajax({
type: "POST",
url: updatedbindlink,
data: Datastring,
datatype: "json",
cache: false,
success: function(response) {
if(response.res)
{
alert("Mapping data successfully Inserted");
}
else
{
//error
}
return false;
}
});
});
Related
In product-master.php (view page), I have a few buttons: Add, Edit, Delete.
Each button got unique name: btnAddProduct, btnEditProduct, btnDelProduct
These buttons share the same action page as shown in the ajax code below: action/product-master.php (yes I named it according to the view page, just different folder)
Yes, I'm using ajax method to process form data. How to validate which button was pressed?
I tried using isset($_POST['btnAddProduct']) but it is not working. Below is my button:
btnAddProduct
<input type="submit" class="btn btn-success" id="btnAddProduct" name="btnAddProduct">
product-master.php
$('#btnAddProduct').on('click', function(e) {
var formData = new FormData($('#form-add-modal')[0]);
$.ajax({
url: "action/<?php echo basename(__FILE__); ?>",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function(data) {
console.log(data);
},
error: function(){
//Display error here
}
});
});
Buttons aren't included when you create a FormData from a form, since the FormData constructor has no way of knowing which button was clicked. You need to add it to the FormData explicitly.
$('#btnAddProduct').on('click', function(e) {
var formData = new FormData($('#form-add-modal')[0]);
formData.set(this.name, "1");
$.ajax({
url: "action/<?php echo basename(__FILE__); ?>",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function(data) {
console.log(data);
},
error: function(){
//Display error here
}
});
});
I am using codeiniter framework. I want to Submit form data in database throught ajax coll and after success submiting form process I want to refresh specific div through other ajax request.
But When I press Enter Key for submit form then data is being submitted twice. I want submit data by ajax once time with on enter key press.
My code like-
$('#form1').submit(function () {
var id = $('#id').val();
var comment_text = $('#comment_text').val();
$.ajax({
data: {'id' :id,'comment_text':comment_text},
type: "POST",
url: "first_req.php",
dataType : "json"
success: function(data){
if(data.status =="Success")
{
$.ajax({
data: {'id':id},
type: 'GET',
url: 'sencond_req.php',
contentType : "application/x-www-form-urlencoded; charset=UTF-8",
success: function (data) {
$('#com_display').html(data);
}
});
}
}
});
return false;
});
HTML-
<div id="com_display"></div>
<form id="form1" name="form1">
<input type='hidden' id='id' name='id' />
<input type='text' id='comment_text' name='comment_text' />
</form>
When I type hi in comment box and press enter button then data two times will be submitted in database.
Please give me any solution to solve it.
Even though you are submitting the data using ajax request, you are not stopping the default action of the form submit.
You can call the event.preventDefault() to do this.
$('#form1').submit(function (e) {
//prevent the default form submission
e.preventDefault();
var id = $('#id').val();
var comment_text = $('#comment_text').val();
$.ajax({
data: {
'id': id,
'comment_text': comment_text
},
type: "POST",
url: "first_req.php",
dataType: "json"
success: function (data) {
if (data.status == "Success") {
$.ajax({
data: {
'id': id
},
type: 'GET',
url: 'sencond_req.php',
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function (data) {
$('#com_display').html(data);
}
});
}
}
});
});
or return false from the event handler if you want to prevent the default and action and stop the bubbling of the submit event
Possible duplicate of this, this and this
In the Documentation READ HERE
(Bind an event handler to the "submit" JavaScript event, or trigger that event on an element.)
In other words, when you click enter you will trigger default submit of the form, and also your jquery will submit the form.
Solution:
Prevent default behavior by adding e.preventDefault();. Read -> jQuery AJAX Form Submit Example
$('#form1').submit(function (e) {
e.preventDefault();
//the rest of code.
remove default behavior
$('form').submit(function(){
$(this).find(':submit').attr('disabled','disabled');
});
3. Only submit the form thru jquery when it actually submits (no resubmitting)
$(document).on("submit", "form.form1", function(event){
alert(1);
});
I'm using jQuery and Codeigniter to update the database via AJAX. The following code does not seem to do anything or send anything to my controller when the button is clicked...
jQuery:
$("#button_submit1").click(function(e) {
$.ajax({
type: "POST",
url: window.location.href,
dataType: "json",
data: $('#writereview_form').serialize() + '&ajax=true',
cache: false,
success: function(data) {
alert("yay");
//$("#writereview_box").fadeOut(1000);
}
});
return false;
});
HTML:
<form action="http://mysite.com/places/writereview/2107" id="writereview_form" method="post" accept-charset="utf-8">
...
...
<input type="submit" name="submit" value="Submit Review" class="button_submit" id="button_submit1">
Any ideas why the ajax data is not being sent?
In this case its better to bind to the 'submit' event on the form and then use preventDefault() to stop the HTML submission and use Ajax instead.
You should also use the form's action instead of window.location
$("#writereview_form").submit(function(e) {
var $this = $(this);
e.preventDefault();
$.ajax({
type: "POST",
url: $this.attr('action'),
dataType: "json",
data: $this.serialize() + '&ajax=true',
cache: false,
success: function(data) {
alert("yay");
//$("#writereview_box").fadeOut(1000);
}
});
});
Try withiut using window.location.href and giving url as url: "page.php",
I have a form which I'm validating using jQuery and php. So basically if the php echoes "input must be filled" the jQuery should put a red border around that input, but the thing works only after submitting the form two times.
I explain: if I submit with input unfilled the php file echoes "input must be filled", but only if I press again the submit button - the input goes red.
$("form#maj_email").submit(function(){
var _data = $(this).serialize()
$.ajax({
type: 'POST',
url: 'validation_profil.php?var=maj_email',
beforeSend: function(){
$("div#ajax_icon_maj_email").css({background:"url('http://localhost/www3/images/ajax_loader.gif')"})
$("div#error_maj_email").hide()
if( $("div#error_maj_email").text()=="Email syntaxe incorrecte"){
$("form#maj_email input:[name=email]").css({border:"1px solid red"})
}
},
data:_data,
cache: false,
success: function(html){
$('div#error_maj_email').html(html)
$("div#ajax_icon_maj_email").css({background:"url('none')"})
$("div#error_maj_email").fadeIn()
}
})
})
It looks like the form is being submitted via the form instead of your ajax call. You need to prevent this behavior for this to work:
$("form#maj_email").submit(function(e){
var _data= $(this).serialize();
$.ajax({
type: 'POST',
url: 'validation_profil.php?var=maj_email',
beforeSend: function(){
$("div#ajax_icon_maj_email").css({background:"url('http://localhost/www3/images/ajax_loader.gif')"})
$("div#error_maj_email").hide()
if( $("div#error_maj_email").text()=="Email syntaxe incorrecte"){
$("form#maj_email input:[name=email]").css({border:"1px solid red"})
}
},
data:_data,
cache: false,
success: function(html){
$('div#error_maj_email').html(html)
$("div#ajax_icon_maj_email").css({background:"url('none')"})
$("div#error_maj_email").fadeIn()
}
});
e.preventDefault();
return false;
})
I am at a loss here with some simple jQuery. I have the following code in a separate file:
$(document).ready(function(){
//$("#trackit").click(function() {
$.ajax({
type: "POST",
url: 'include/trackit.php',
data: "trackingNum=123456",
success: function(data) {
alert(data);
}
});
//});
});
That works just fine, on page load it returns the expected data to the alert window.
But, if I remove the comments from this line:
$("#trackit").click(function() {
to capture the form's submit button, I get no return data.
Here is the abbreviated form info:
<form action="<?php htmlentities($_SERVER['PHP_SELF']);?>" name="tForm" id="tForm" method="post">
<button type="submit" class="buttonTracking" id="trackit" name="trackit">Track It!</button>
</form>
Am I just overlooking some simple error here?
Correct working code would be this:
$(document).ready(function(){
$("#trackit").click(function() {
$.ajax({
type: "POST",
url: 'include/trackit.php',
data: "trackingNum=123456",
success: function(data) {
alert(data);
}
});
return false; // to cancel form submission and subsequent page refresh
});
});
I'd say it's because the #trackit button is also a submit button. The click action is executing your javascript AND submitting the form. You need to change the code to:
$(document).ready(function(){
$("#trackit").click(function() {
$.ajax({
type: "POST",
url: 'include/trackit.php',
data: "trackingNum=123456",
success: function(data) {
alert(data);
}
});
return false;
});
});