$.ajax serialize() does not pass data to php file - ajax

What is incorrect in the code? Can not pass data to _autosave.php
<script type="text/javascript">
$(document).ready(function(){
autosave();
});
function autosave() {
var t = setTimeout("autosave()", 5000);
var inputValues= $('.input_form').serialize();
$.ajax( {
type: "POST",
url: "_autosave.php",
data: inputValues,
} )
.done(function(data){
alert(data);
});
...
    
Input is this
<form id="input_form" autocomplete="off" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) ?>"
method="post">
<input type="text" name="input" id="input">
_autosave.php is this
$input = $_POST['input'];
echo $input .' input<br>';
If I enter some value in input, get input<br> instead of entered value
Update
If for someone may be necessary here is working code
$.post("_autosave.php", $("#form1").serialize(), function(data) {
$('#load').html(data);
$('#is_row_changed1').val(0)
});

You have a trailing comma here resulting in invalid javascript:
data: inputValues,
Here's how you could fix (and improve your current code):
<script type="text/javascript">
$(document).ready(autosave);
function autosave() {
window.setTimeout(autosave, 5000);
var inputValues = $('.input_form').serialize();
$.ajax({
type: "POST",
url: "_autosave.php",
data: inputValues
})
.done(function(data) {
alert(data);
});
}
</script>
or if you prefer a shorthand:
<script type="text/javascript">
$(document).ready(autosave);
function autosave() {
window.setTimeout(autosave, 5000);
var inputValues = $('.input_form').serialize();
$.post("_autosave.php", inputValues, function(data) {
alert(data);
});
}
</script>

Have you tried with serializeArray() instead?
<script type="text/javascript">
$(document).ready(autosave);
function autosave() {
window.setTimeout(autosave, 5000);
$.post("_autosave.php", $('.input_form').serializeArray(),
function(data) {
alert(data);
});
}
</script>

Related

How to call an action method upon selection from the <select> dropdwon in ASP.NET Core 6 MVC?

I created a dropdown using the <select> HTML element. Now I want to call an action after user makes a selection from the list.
<select name="ddAircraft" id="ddAircraft" class="form-control form-select-sm form-select"
asp-items="#(new SelectList(ViewBag.ddaircraft,"id","name"))">
</select>
I would also like to know if user enter a value in a input box. Then I want to run a Javascript method. How I can do that?
I tried to do onClick but I am getting usual error.
It would help if you could show the details of your error,
I Tried with the codes below:
#{
var sel = new List<SelectListItem>()
{
new SelectListItem(){Text="1",Value="1" },
new SelectListItem(){Text="2",Value="2" },
new SelectListItem(){Text="3",Value="3" }
};
}
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script>
$(function () {
$('#selectlist').change(function () {
window.location.href = "Privacy";
})
});
</script>
<script>
$(function () {
$('#input').change(function () {
window.location.href = "Privacy";
})
});
</script>
<input id="input" value=""></input>
<select id="selectlist" asp-items=sel></select>
The result:
To pass the selected item value ,you could try :
<script>
$(function () {
$('#selectlist').change(function () {
window.location.href = "Home/Privacy?sel=" + $(this).val();
})
});
</script>
The result:
if you want to make a post request, you could try with ajax as below:
<script>
$(function () {
$('#selectlist').change(function () {
var sel = $(this).val();
var input = document.getElementById("input").value;
$.ajax({
url: "Home/Test",
contentType: "application / json; charset = utf - 8",
type: "post",
data: JSON.stringify({
sel: sel,
input: input
}),
datatype: "json",
success: function (data) {
console.log(data);
}
})
})
});
</script>
The result:

Ajax function not triggering

Why my 'Ajax' function is not triggering when changing the 'state' please help. even the 'console' is not showing the value. When i comment Ajax function I am getting the stateID in console
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(document).on("change", "#state", function(){
var stateID = $(this).val();
console.log('value of StateID'+stateID);
$.ajax({
url:"user/getCity"
async: false,
type: "POST",
data: {stateID,stateID},
dataType: "html",
success: function(data) {
//$('#city').html(data);
console.log(data);
}
})
});
});

How to use Ajax in Smarty?

This is ajax request
<script type="text/javascript">
{literal}
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
alert(IDCat);
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
alert(data);
alert("success");
}
});
});
});
{/literal}
</script>
And this is php codes
if(isset($_GET['IDCat'])){
$idc= $_GET['IDCat'];
echo $idc;
}
there is the problem echo $idc; doesn't work ? where is the problem ?
var IDCat=this.value;
should be
var IDCat=$(this).val();

Ajax not posting variable to php file

I am trying to use a drag-and-drop function in 'index.php' and post a variable, 'element' to 'store.php', where in the final version it should update a database.
'store.php' is called and runs but the variable is not being passed. I have attached below a shortened version of 'store.php' with a trap to catch this, so in this version I get the response "Element value not set".
INDEX.PHP:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="scripts/jquery-1.3.2.min.js"></script>
<script src="scripts/jquery-ui-1.7.1.custom.min.js"></script>
<link rel="stylesheet" href="style.css">
<div class="content_box" id="content_box_drag" onMouseOver="drag();">
Drag label
<?php for($i=0; $i<5;$i++) {
echo "<p class='dragelement' id='dragelement_$i'>Ferrari_$i</p>";
} ?>
</div>
<div class="content_holder_box" id="content_box_drop">
Drop here
<p class="dropper"></p>
</div>
<div style="clear:both;"></div>
<br/><br/>
<div id="search_result"></div>
<script>
//initialize the drag and drop functions.
function drag(){
$( "#content_box_drag p" ).draggable({
appendTo: "body",
helper: "clone",
revert: "invalid"
});
$( "#content_box_drop p" ).droppable({
activeClass: "dropper_hover",
hoverClass: "dropper_hover",
accept: ":not(.ui-sortable-helper)",
drop: function( event, ui ) {
var ele = ui.draggable.text();
$.ajax({
url: "store.php",
method: "POST",
data: "element=" + ele,
success: function(result) {
alert(result);
}
});
}
});
}
</script>
STORE.PHP (shortened):
<?php
if(isset($_POST['element'])){
$element=$_POST['element'];
} else {
echo "Element value not set";
exit;
}
?>
Any ideas why the variable is not being set?
Parameter method doesn't exist, change to type: "POST"
Set your data as JSON map, change data: "element=" + ele to data: {element : ele}
http://api.jquery.com/jQuery.ajax/ (Search type description)
$( "#content_box_drop p" ).droppable({
activeClass: "dropper_hover",
hoverClass: "dropper_hover",
accept: ":not(.ui-sortable-helper)",
drop: function( event, ui ) {
var ele = ui.draggable.text();
$.ajax({
url: "store.php",
type: "POST",
data: {element : ele},
success: function(result) {
alert(result);
}
});
}
});

Why Ajax Jquery form click not working vs div that works?

Ajax Jquery form not working vs div why its happen and how can i fix my error?
view.html-Code with form
not working
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
</head>
<body>
<form id="parse-form" action="#" method="post">
<button type="submit" id="submit-html">submit ajax request without parameters</button>
</form>
<div>array values: <div id="array-values"></div></div>
<script type="text/javascript">
$(document).ready(function() {
$('#submit-html').click(function() {
$.ajax({
url: 'controller.php',
type: 'POST',
dataType:'json',
success: function(data) {
alert("response begin");
alert(data);
$.each(data, function (i, elem) {
$('#array-values').append('<div>'+elem+'</div>');
});
}
});
});
});
</script>
</body>
</html>
view.html -form replaced by div
working
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
</head>
<body>
<div id="parse-form">
<button type="submit" id="submit-html">submit ajax request without parameters</button>
</div>
<div>array values: <div id="array-values"></div></div>
<script type="text/javascript">
$(document).ready(function() {
$('#submit-html').click(function() {
$.ajax({
url: 'controller.php',
type: 'POST',
dataType:'json',
success: function(data) {
alert("response begin");
alert(data);
$.each(data, function (i, elem) {
$('#array-values').append('<div>'+elem+'</div>');
});
}
});
});
});
</script>
</body>
</html>
controller.php -simple php file that return json array:
<?php
$arr=array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
Thanks
The form has a default action with a type="submit" button, which is submitting, so you'll need to stop that from happening by adding return false or event.preventDefault() , like this:
$(document).ready(function() {
$('#submit-html').click(function(e) {
$.ajax({
url: 'controller.php',
type: 'POST',
dataType:'json',
success: function(data) {
alert("response begin");
alert(data);
$.each(data, function (i, elem) {
$('#array-values').append('<div>'+elem+'</div>');
});
}
});
return false;
//or e.preventDefault();
});
});
Without this, the form is submitting as it normally would with no JavaScript, leaving the page. So effectively it's doing a refresh, instead of AJAX submitting your form (which doesn't have time to complete...because you left :)
An element of type=submit inside a <form> will perform the form request when clicked on.
You need to abort the default behavior by running event.preventDefault() inside the click callback.
My guess is that the form is submitting and refreshing the page before the ajax has a chance to respond.
Try putting return false; at the end of the click handler.
$('#submit-html').click(function() {
$.ajax({
url: 'controller.php',
type: 'POST',
dataType: 'json',
success: function(data) {
alert("response begin");
alert(data);
$.each(data, function(i, elem) {
$('#array-values').append('<div>' + elem + '</div>');
});
}
});
return false;
});
Of course you'll have the same issue if the user hits Enter in one of the fields. Unless you're preventing the Enter key from submitting the form, you may want to the handle the event using the submit() handler.
$('#parse-form').submit(function() {
$.ajax({
url: 'controller.php',
type: 'POST',
dataType: 'json',
success: function(data) {
alert("response begin");
alert(data);
$.each(data, function(i, elem) {
$('#array-values').append('<div>' + elem + '</div>');
});
}
});
return false;
});
Try using submit() http://api.jquery.com/submit/ , this should work with keyboard events as well as clicks. You can use serialize() to get any form data into the ajax objects data variable.

Resources