I have looked at suggested pages and used searched but cant find out solution. I have very simple code jQuery below, where it gets the IDs (array) of check-boxes and send to report.php page through Ajax, below is my code that works fine:
$('#report').submit(function (event) {
$.ajax({
url: "report.php",
data: $('.ids:checked'),
type: "post",
dataType: "json",
success: function(data) {
//alert(data);
if(data.success == 1) {
//upon success
} else {
// upon error
}
}
});
event.preventDefault();
});
however I want to include a textarea which as ID as message, below is my html code:
<textarea id="message" name="message"></textarea>
if I change the "data" variable and add "message" in it, it refreshes the page upon submit:
data: {ids: $('.ids:checked'), message: $("#message").val(text)},
How can I receive both array of IDs and Message variables without refreshing the page and working with Ajax. Any help or guidance would be appreciated. thanks
3
Answers
Thank you everyone for your help, I really appreciate this friendly community. The problem was I had array of checkboxes IDs which when passing to data was creating problem, so I found the following solution
and then pass this value
Not the ajax call but the submit is refreshing the page. So change the submit-button in your form to an ordinary button with
type="button"
, listen for the click on that button (instead of the submit event) and prevent inputs to submit the form by pressing "Enter"HTML:
JS:
Passing a jQuery object as a data item to $.ajax will cause an error,
event.preventDefault();
will not get called, and then your form submits and reloads the page(or load another page).If you want to see the error put
event.preventDefault();
Use the
.val()
method to get values to pass in the data object to pass to $.ajax