Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  99] [ 3]  / answers: 1 / hits: 37326  / 11 Years ago, wed, july 10, 2013, 12:00:00

I am trying to save data submitted from a form into my mysql database and and then update the div element with the last posted item prepended to the list in the div.



Right now I am only trying to get a response back, I'm not worried about having the formatting correct at the moment.



My problem is the form won't submit with e.preventDefault(); in place, but without it the form does the normal method of posting to the db then refreshing the page.



Here is my AJAX call:



$(document).ready(function() {

$('form#feedInput').submit(function(e) {

e.preventDefault();

$.ajax({
type: POST,
url: <?php echo site_url('dashboard/post_feed_item'); ?>,
data: $('.feed-input').val(),
dataType: html,
success: function(data){
debugger;
$('#feed-container').prepend(data);
},
error: function() { alert(Error posting feed.); }
});

});
});


I don't think it's necessary for me to post my controller code, seeing as how my issue is the form won't make it past the e.preventDefault(); function.



How can I get this form to submit via AJAX if the e.preventDefault() function is stopping it before it can reach the $.ajax() function?


More From » php

 Answers
38

The data attribute of the ajax call is invalid. It should be either in JSON format { key: $('.feed-input').val() } or in query format 'key='+$('.feed-input').val().
Also there is an unnecessary debugger variable in the success method.



A working code could be:



$('form#feedInput').submit(function(e) {

var form = $(this);

e.preventDefault();

$.ajax({
type: POST,
url: <?php echo site_url('dashboard/post_feed_item'); ?>,
data: form.serialize(), // <--- THIS IS THE CHANGE
dataType: html,
success: function(data){
$('#feed-container').prepend(data);
},
error: function() { alert(Error posting feed.); }
});

});

[#77091] Tuesday, July 9, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
ravenl questions
Thu, Feb 18, 21, 00:00, 3 Years ago
Tue, Jan 12, 21, 00:00, 3 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;