I cant define data in ajax jquery function, where can be problem? I try everything, choose other server, other browser, remove cache, add data manually and always it is undefined...
Server: Laragon
App.js , add-new.php
(function($){
var form = $('#add-form');
form.on('submit',function(){
event.preventDefault();
$.ajax({
url: form.attr('action'),
type: 'POST',
data: form.serialize()
})
.done(function(data){
console.log("Data: " + data);
});
});
}(jQuery))
<?php
die("test");
//include
require('./config.php');
$id = $database->insert('items',[
'text' => $_POST['message']
]);
if($id){
die('success');
}
?>
Response screen:
response screen
I think you have missed success parameter in ajax function.
$.ajax({
url: form.attr('action'),
type: 'POST',
data: form.serialize(),
success: function(data) {
console.log("Data => ", data);
}
});
Hope it may work for you.
Related
I need to post large amount of json data to php file via ajax, but its is showing me 413 entity too large error.
I have tried using data type as json but it still shows the same error.
$.ajax({
url: ajaxURL,
data: "ajax=true&action=manageSavedLayouts&a=" + result + "&b=" + encodeURIComponent(productdb),
success: function(result) {
console.log(result);
// alert(result);
}
});
This is the code i am using to make the ajax call and the variable which is causing problem is productdb.
Any help will be greatly appreciated.
Thanks in advance.
$.ajax({
url: ajaxURL,
type: "POST",
data: {
ajax: true,
action: "manageSavedLayouts",
a: result,
b: productdb
}
success: function(result) {
console.log(result);
}
});
Use type POST, e.g.:
$.ajax({
url: ajaxURL,
type: 'POST',
data: "ajax=true&action=manageSavedLayouts&a=" + result + "&b=" + encodeURIComponent(productdb),
success: function(result) {
console.log(result);
// alert(result);
}
});
How can i send data like this to php using ajax
["{"title":"mr","fname":"john","lname":"Annah","oname":"Clement","staffid":"123"}"]
try json_encode
for more refer -
http://php.net/manual/en/function.json-encode.php
Do it like so, using jQuery(which you need to include in your script):
<script>
var data={};
data= {
"title":"mr",
"fname":"john",
"lname":"Annah",
"oname":"Clement",
"staffid":"123"};
$.ajax({
url:"somwhere.php",
type:"POST",
dataType:"JSON",
data:data,
async: true});
</script>
And on the page where you want to catch this data, do it like this:
<?php
$title=$_POST['title'];
$fname=$_POST['fname'];
?>
And so on.
stringify before sending
Eg :
var postData = [
{ "id":"1", "name":"bob"},
{ "id":"2", "name":"jonas"}]
this works,
$.ajax({
url: Url,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(postData) //stringify is important,
});
Try this
$(document).on("click", "#your element", function () {
$.ajax({
type: 'POST',
url: "your_url",
data : {"title":"mr","fname":"john","lname":"Annah","oname":"Clement","staffid":"123"},,
success: function (result) {
### your action after ajax
},
})
})
you can pass it in data like this,
$.ajax({
url: 'url',
type: 'GET',
data: { title:"mr",fname:"john",lname:"Annah",oname:"Clement",staffid:"123" } ,
contentType: 'application/json; charset=utf-8',
success: function (response) {
//your success code
}
});
I want to send the data via ajax to other page. I have isolated the problem. This is the code.
Thank you all for your help..But no effect..
updated code
It worked...
<script>
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
var size=123;
var itemname=123;
var potency=123;
var quantity=12333;
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
success: function(data) {
alert(data);
},
error: function(data) {
alert(data);
}
});
});
});
</script>
So I click the link,it navigates, to dd.php which has
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']));
echo $_POST['itemname'];
?>
I get Object Object as alert. What am doing wrong? Pls throw some light here..thanks you..
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault();
var data = {"box":1233,
"size":565,
"itemname":565,
"potency":876,
"quantity":234};
$.ajax({
url: "dd.php",
type: "post",
data: data,
dataType: "json",
success: function(data) {
if(console){
console.log(data);
}
},
error: function(data) {
if(console){
console.log(data);
}
}
});
});
});
few things to consider... you can post data as object..which is clean and easier to use
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
....
var dataString ={'box':box,'size':size,'itemname':itemname,'potency':potency,'quantity':quantity};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json", //<--- here this means the response is expected as JSON from the server
success: function(data) {
alert(data.itemcode); //<--here alert itemcode
},
error: function(data) {
alert(data);
}
});
so you need to send the response as json in PHP
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Here you are using querystring as sent in GET request.
If you want to send the data in same form, you can use this with GET request type:
$.ajax({
url: "dd.php"+dataString,
type: "get",
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
Or for POST request,you will have to put data in json object form, So you can use :
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
});
And put echo in your php code :
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Javascript alert shows [Object object] for object. You can see response using console.log or can use that key with alert.
For more information, refer jQuery.ajax()
I am trying to send data to my PHP script to handle some stuff and generate some items.
$.ajax({
type: "POST",
url: "test.php",
data: "album="+ this.title,
success: function(response) {
content.html(response);
}
});
In my PHP file I try to retrieve the album name. Though when I validate it, I created an alert to show what the albumname is I get nothing, I try to get the album name by $albumname = $_GET['album'];
Though it will say undefined :/
You are sending a POST AJAX request so use $albumname = $_POST['album']; on your server to fetch the value. Also I would recommend you writing the request like this in order to ensure proper encoding:
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.post('test.php', { album: this.title }, function() {
content.html(response);
});
and if you wanted to use a GET request:
$.ajax({
type: 'GET',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.get('test.php', { album: this.title }, function() {
content.html(response);
});
and now on your server you wil be able to use $albumname = $_GET['album'];. Be careful though with AJAX GET requests as they might be cached by some browsers. To avoid caching them you could set the cache: false setting.
Try sending the data like this:
var data = {};
data.album = this.title;
Then you can access it like
$_POST['album']
Notice not a 'GET'
You can also use bellow code for pass data using ajax.
var dataString = "album" + title;
$.ajax({
type: 'POST',
url: 'test.php',
data: dataString,
success: function(response) {
content.html(response);
}
});
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
What am i doing wrong. PHP doesn't seem to catch title and wrapper from $.ajax. Does the code look correct. The success message i get indicate an error that title is not found.
jQuery main.html
$.ajax({
type: "POST",
url: "process.php",
data: 'title=test&wrapper=testing',
success: function(msg){
alert( "Data Saved: " + msg );
}
});
PHP process.php
<?php
$title = $_REQUEST['title'];
$wrapper = $_REQUEST['wrapper'];
...
?>
Take a look: jQuery.ajax()
The data parameter is better to be a Key/Value pairs object, it's cleaner and easier to debug :)
$.ajax({
type: "POST",
url: "process.php",
data: {
title: 'test',
wrapper: 'testing'
},
success: function(msg){
alert( "Data Saved: " + msg );
}
});
Thats a good solution.but if I try to send data through a form in a webservice.
$.ajax({
type: "POST",
url: "process.php",
data: {
title: $('#title').val,
name: $('#name').val
},
success: function(data){
alert(data );
}
});
Here title and name are forms element in client side.but i am not able to get post value in json based webservice file say process.php