Jquery Ajax submitting empty textarea text to the database












0















I am trying to insert a textarea's text to my MySQL database, but my problem is when my program tries to submit empty text to the database.



When I remove e.preventDefault(); it is working just fine. What am I doing wrong?



var frm = $('#newsform');

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});
});

<form action="updatenews.php" method="POST" id="newsform">
<textarea name="news"></textarea>
<input type="submit">
</form>









share|improve this question

























  • when you remove e.preventDefault it does a normal postback instead of ajax, so assuming you don't want that to happen then leave it in place. Then check your network tab in the browser developer tools and look inside the ajax request to see what was sent in the request body, and whether it matches what you expect

    – ADyson
    Jul 12 '17 at 13:23






  • 3





    What do you get if you do console.log(frm.serialize()); just before the ajax call?

    – N. Ivanov
    Jul 12 '17 at 13:24











  • just before ajax call gave me this news=

    – SamaraSsr
    Jul 12 '17 at 14:08











  • @ADyson when i see in the network body it is showing news=

    – SamaraSsr
    Jul 12 '17 at 14:10











  • I can't reproduce your issue. See this example: jsfiddle.net/umsq4oud Watch the network tab for that (the request will fail because the target URL doesn't exist but that's not the point, you can see the request body is populated). Maybe something else is interfering with your data, but it's not anything in the code you've posted. What version of jQuery are you using, out of interest?

    – ADyson
    Jul 12 '17 at 14:22


















0















I am trying to insert a textarea's text to my MySQL database, but my problem is when my program tries to submit empty text to the database.



When I remove e.preventDefault(); it is working just fine. What am I doing wrong?



var frm = $('#newsform');

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});
});

<form action="updatenews.php" method="POST" id="newsform">
<textarea name="news"></textarea>
<input type="submit">
</form>









share|improve this question

























  • when you remove e.preventDefault it does a normal postback instead of ajax, so assuming you don't want that to happen then leave it in place. Then check your network tab in the browser developer tools and look inside the ajax request to see what was sent in the request body, and whether it matches what you expect

    – ADyson
    Jul 12 '17 at 13:23






  • 3





    What do you get if you do console.log(frm.serialize()); just before the ajax call?

    – N. Ivanov
    Jul 12 '17 at 13:24











  • just before ajax call gave me this news=

    – SamaraSsr
    Jul 12 '17 at 14:08











  • @ADyson when i see in the network body it is showing news=

    – SamaraSsr
    Jul 12 '17 at 14:10











  • I can't reproduce your issue. See this example: jsfiddle.net/umsq4oud Watch the network tab for that (the request will fail because the target URL doesn't exist but that's not the point, you can see the request body is populated). Maybe something else is interfering with your data, but it's not anything in the code you've posted. What version of jQuery are you using, out of interest?

    – ADyson
    Jul 12 '17 at 14:22
















0












0








0








I am trying to insert a textarea's text to my MySQL database, but my problem is when my program tries to submit empty text to the database.



When I remove e.preventDefault(); it is working just fine. What am I doing wrong?



var frm = $('#newsform');

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});
});

<form action="updatenews.php" method="POST" id="newsform">
<textarea name="news"></textarea>
<input type="submit">
</form>









share|improve this question
















I am trying to insert a textarea's text to my MySQL database, but my problem is when my program tries to submit empty text to the database.



When I remove e.preventDefault(); it is working just fine. What am I doing wrong?



var frm = $('#newsform');

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});
});

<form action="updatenews.php" method="POST" id="newsform">
<textarea name="news"></textarea>
<input type="submit">
</form>






php jquery mysql ajax forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 21:43









Mark Amery

63.8k31255304




63.8k31255304










asked Jul 12 '17 at 13:21









SamaraSsrSamaraSsr

2918




2918













  • when you remove e.preventDefault it does a normal postback instead of ajax, so assuming you don't want that to happen then leave it in place. Then check your network tab in the browser developer tools and look inside the ajax request to see what was sent in the request body, and whether it matches what you expect

    – ADyson
    Jul 12 '17 at 13:23






  • 3





    What do you get if you do console.log(frm.serialize()); just before the ajax call?

    – N. Ivanov
    Jul 12 '17 at 13:24











  • just before ajax call gave me this news=

    – SamaraSsr
    Jul 12 '17 at 14:08











  • @ADyson when i see in the network body it is showing news=

    – SamaraSsr
    Jul 12 '17 at 14:10











  • I can't reproduce your issue. See this example: jsfiddle.net/umsq4oud Watch the network tab for that (the request will fail because the target URL doesn't exist but that's not the point, you can see the request body is populated). Maybe something else is interfering with your data, but it's not anything in the code you've posted. What version of jQuery are you using, out of interest?

    – ADyson
    Jul 12 '17 at 14:22





















  • when you remove e.preventDefault it does a normal postback instead of ajax, so assuming you don't want that to happen then leave it in place. Then check your network tab in the browser developer tools and look inside the ajax request to see what was sent in the request body, and whether it matches what you expect

    – ADyson
    Jul 12 '17 at 13:23






  • 3





    What do you get if you do console.log(frm.serialize()); just before the ajax call?

    – N. Ivanov
    Jul 12 '17 at 13:24











  • just before ajax call gave me this news=

    – SamaraSsr
    Jul 12 '17 at 14:08











  • @ADyson when i see in the network body it is showing news=

    – SamaraSsr
    Jul 12 '17 at 14:10











  • I can't reproduce your issue. See this example: jsfiddle.net/umsq4oud Watch the network tab for that (the request will fail because the target URL doesn't exist but that's not the point, you can see the request body is populated). Maybe something else is interfering with your data, but it's not anything in the code you've posted. What version of jQuery are you using, out of interest?

    – ADyson
    Jul 12 '17 at 14:22



















when you remove e.preventDefault it does a normal postback instead of ajax, so assuming you don't want that to happen then leave it in place. Then check your network tab in the browser developer tools and look inside the ajax request to see what was sent in the request body, and whether it matches what you expect

– ADyson
Jul 12 '17 at 13:23





when you remove e.preventDefault it does a normal postback instead of ajax, so assuming you don't want that to happen then leave it in place. Then check your network tab in the browser developer tools and look inside the ajax request to see what was sent in the request body, and whether it matches what you expect

– ADyson
Jul 12 '17 at 13:23




3




3





What do you get if you do console.log(frm.serialize()); just before the ajax call?

– N. Ivanov
Jul 12 '17 at 13:24





What do you get if you do console.log(frm.serialize()); just before the ajax call?

– N. Ivanov
Jul 12 '17 at 13:24













just before ajax call gave me this news=

– SamaraSsr
Jul 12 '17 at 14:08





just before ajax call gave me this news=

– SamaraSsr
Jul 12 '17 at 14:08













@ADyson when i see in the network body it is showing news=

– SamaraSsr
Jul 12 '17 at 14:10





@ADyson when i see in the network body it is showing news=

– SamaraSsr
Jul 12 '17 at 14:10













I can't reproduce your issue. See this example: jsfiddle.net/umsq4oud Watch the network tab for that (the request will fail because the target URL doesn't exist but that's not the point, you can see the request body is populated). Maybe something else is interfering with your data, but it's not anything in the code you've posted. What version of jQuery are you using, out of interest?

– ADyson
Jul 12 '17 at 14:22







I can't reproduce your issue. See this example: jsfiddle.net/umsq4oud Watch the network tab for that (the request will fail because the target URL doesn't exist but that's not the point, you can see the request body is populated). Maybe something else is interfering with your data, but it's not anything in the code you've posted. What version of jQuery are you using, out of interest?

– ADyson
Jul 12 '17 at 14:22














1 Answer
1






active

oldest

votes


















-3














var frm = $('#newsform'); 

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: 'post',
url: frm.attr('action'),
data: {'frm':frm},
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});


});





share|improve this answer


























  • Uncaught RangeError: Maximum call stack size exceeded getting above error when i tried above code

    – SamaraSsr
    Jul 12 '17 at 13:41











  • data: {'frm':frm}, er no, why would you submit the whole HTMLElement object? The form data is what's required.

    – ADyson
    Jul 12 '17 at 14:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45058875%2fjquery-ajax-submitting-empty-textarea-text-to-the-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-3














var frm = $('#newsform'); 

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: 'post',
url: frm.attr('action'),
data: {'frm':frm},
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});


});





share|improve this answer


























  • Uncaught RangeError: Maximum call stack size exceeded getting above error when i tried above code

    – SamaraSsr
    Jul 12 '17 at 13:41











  • data: {'frm':frm}, er no, why would you submit the whole HTMLElement object? The form data is what's required.

    – ADyson
    Jul 12 '17 at 14:19
















-3














var frm = $('#newsform'); 

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: 'post',
url: frm.attr('action'),
data: {'frm':frm},
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});


});





share|improve this answer


























  • Uncaught RangeError: Maximum call stack size exceeded getting above error when i tried above code

    – SamaraSsr
    Jul 12 '17 at 13:41











  • data: {'frm':frm}, er no, why would you submit the whole HTMLElement object? The form data is what's required.

    – ADyson
    Jul 12 '17 at 14:19














-3












-3








-3







var frm = $('#newsform'); 

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: 'post',
url: frm.attr('action'),
data: {'frm':frm},
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});


});





share|improve this answer















var frm = $('#newsform'); 

frm.submit(function (e) {

e.preventDefault();

$.ajax({
type: 'post',
url: frm.attr('action'),
data: {'frm':frm},
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});


});






share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 12 '17 at 13:35









Musa

80.7k1588106




80.7k1588106










answered Jul 12 '17 at 13:32









TamilTamil

13




13













  • Uncaught RangeError: Maximum call stack size exceeded getting above error when i tried above code

    – SamaraSsr
    Jul 12 '17 at 13:41











  • data: {'frm':frm}, er no, why would you submit the whole HTMLElement object? The form data is what's required.

    – ADyson
    Jul 12 '17 at 14:19



















  • Uncaught RangeError: Maximum call stack size exceeded getting above error when i tried above code

    – SamaraSsr
    Jul 12 '17 at 13:41











  • data: {'frm':frm}, er no, why would you submit the whole HTMLElement object? The form data is what's required.

    – ADyson
    Jul 12 '17 at 14:19

















Uncaught RangeError: Maximum call stack size exceeded getting above error when i tried above code

– SamaraSsr
Jul 12 '17 at 13:41





Uncaught RangeError: Maximum call stack size exceeded getting above error when i tried above code

– SamaraSsr
Jul 12 '17 at 13:41













data: {'frm':frm}, er no, why would you submit the whole HTMLElement object? The form data is what's required.

– ADyson
Jul 12 '17 at 14:19





data: {'frm':frm}, er no, why would you submit the whole HTMLElement object? The form data is what's required.

– ADyson
Jul 12 '17 at 14:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45058875%2fjquery-ajax-submitting-empty-textarea-text-to-the-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine