I am sending checked checkboxes value to php in array.
// tag =['Apple','Mango','Tomato']
var tag = $(this).children().siblings().children().children('input[name="cb"]:checked');
var tagData = [];
$.each(tag, function() {
tagData.push($(this).val());
});
console.log(tagData);
$.ajax({
type: "POST",
url: "script.php",
data: {tag: tagData },
cache: false,
success: function(){
alert("OK");
}
});
Console.log data
(2) ["Apple", "Apple"]
0: "Apple"
1: "Apple"
length: 2__proto__: Array(0)
I'm getting this array in php like this.
$list = $_POST['tag'];
$imgTag = implode( ", ",$list);
// i want like this - $imgTag = "Apple,Mango,Tomato".
But i getting empty line in php.
Serialize your tagData using JSON.stringify while sending in ajax request. like this
$.ajax({
type: "POST",
url: "script.php",
data: {tag: JSON.stringify(tagData) },
cache: false,
success: function(){
alert("OK");
}
});
Related
I have 2 ajax is an array and single char:
var jsonEncode = JSON.stringify(TableData); --> output: [{"name":"Ristha","age":"30"},{"name":"Niken","age":"25"}]
var code = $('#mutiplearray-code_reg').val(); --> output: 1RF46TA
How to send ajax post when I use 2 data like that:
$.ajax({
type: "POST",
data: "pTableData=" + jsonEncode + "code1=" + code,
success: function(msg){
// alert(msg);
},
});
When I get using in my controller:
$tableData = stripcslashes($_POST['pTableData']);
$tableData = json_decode($tableData, true);
$name1 = $tableData['name'];
$age1 = $tableData['age'];
$code1 = $_POST['code1'];
It's have error dev tool undefined code1 and pTableData?? What I'm do wrong with use multiple data in my ajax?
When I'm just using post data one of them is work correctly
Pass data as json. You passed the data as string.
$.ajax({
type: "POST",
data: {pTableData: jsonEncode, code1: code},
success: function(msg){
// alert(msg);
},
});
$.ajax({
type: "POST",
data:{'pTableData':jsonEncode,'code1':code},
success: function(msg){
// alert(msg);
},
});
I have code in my html file as below. I am using jQuery Mobile
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
// On success
}
});
owner_pickup.php returns me data by executing query. Now i need to pass a value which i would read in my owner_pickup.php file.
Kindly suggest me how would we pass the value
in your php file:
$value = array(
"dat_1" => "this is data number 1",
"dat_2" => "this is data number 2"
);
echo json_encode($value);
in your jquery finction:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
please look at this answers:
retrieve multiple values from ajax call
if you don't know how to use JSON please google it.
edit:
pass a value to the php:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
data: {
first_value:50,
second_value:55
}
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
in the php:
if(isset($_GET['first_value']))
$first = $_GET['first_value'];
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data: {param1: 123, param2: "text value"},
cache: false,
dataType:'json',
success: function(data) { // On success }
});
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data:{key1:value1}
cache: false,
dataType:'json'
success: function(data)
{
}
});
In php aaccept it as
<?php
$_REQUEST['key1'];
?>
i want to pass array which is generated by javascript..just say i have an array named vals2=('john','peter') and i want to pass this array to my php page(insert_paket_f.php).
this is my ajax code :
$.ajax({
type: "POST",
url: "insert_paket_f.php",
data: { data : vals2 },
cache: false,
//vals=('john','peter','andrea');
success: function(){
alert("OK");
}
});
insert_paket_f.php
$data1 = $_POST['data'];
$data1 = explode(",", $_POST['data']);
print_r($data1);
when i run my browser, its show empty array, and just looks like this Array ( [0] => )
how can i fix this?
thanks..
Try this:
Use join to send the javascript array as an string.
Javascript
var vals2 = ['john','peter'];
$.ajax({
type: "POST",
url: "insert_paket_f.php",
data: { data : vals2.join(',') },
cache: false,
success: function(){
alert("OK");
}
});
PHP
$data1 = $_POST['data'];
$data1 = explode(",", $_POST['data']);
print_r($data1);
Hope it helps.
Try:
$data1 = json_decode($_POST['data']);
print_r($data1);
In the Data Feild try to use variables
as
echo data: data[]=john&data[]=peter&data[]=andrea
Code:-
$.ajax({
type: "POST",
url: "insert_paket_f.php",
data: "data[]=john&data[]=peter&data[]=andrea",
cache: false,
success: function(){
alert("OK");
}
});
Is It working?
I have a php program where I just test some sample data. I am getting error as missing ] after element list. How can I read this?
$dataDetailsList = array();
array_push($dataDetailsList, array('a' =>'1','b' =>'2','c' =>'3','d' =>'4','e' =>'5'));
echo json_encode(array("DataDetailsList"=>$dataDetailsList));
Then in my jQuery processor I am doing like this.
function requestData() {
$.ajax({
url: 'live-server-data.php',
success: function(data) {
//alert(json);
var jsonData = eval(" (" + data + ") ");
},
cache: false
});
function requestData() {
$.ajax({
url: 'live-server-data.php',
success: function(data) {
//alert(json);
var jsonData = data;
},
cache: false,
dataType: 'json' //data type that it will return
});
}
Don't use eval is evil.
Instead of this use:
JSON.parse(data); // not supported in IE7 and below
I think you need to try
dataType: 'json'
That is,
$.ajax({
url: 'live-server-data.php',
dataType: 'json',
success: function(data) {
var jsonData = data;
console.log(jsonData);
$.each(jsonData.DataDetailsList, function(key, val) {
var key = Object.keys(val)[0],
value = val[key];
console.log(key); // output: a, b, c ...
console.log(value); // output: 1, 2, 3,...
// alternative
for(var key in val) {
console.log(key);
console.log(val[key]);
}
});
},
cache: false
})
You should just set the dataType to json and jQuery will do the trick for you..
$.ajax({
url: 'live-server-data.php',
dataType: 'json', //Added dataType json
success: function(data) {
//Now data is a javascript object (JSON)
},
cache: false
});
Form sending AJAX code:
var str = $("form").serialize();
alert(str);
// var uns=#unserialize(str);
//alert(uns);
$.ajax({
type: "POST",
url: "update.php",
data: "box1="+str,
success: function(value)
{
$("#data").html(value);
}
HTML Form:
<form>
<input type=checkbox name=box[] value='1'/><input type=checkbox name=box[] value='2'/>
</form>
In my PHP:
$box=$_POST['box1'];
How can I access each of the box variable values in PHP side?
Your js should be like this:
var str = $("form").serializeArray();
$.ajax({
type: "POST",
url: "update.php",
data: str,
success: function(value) {
$("#data").html(value);
}
});
With php you should loop your result array.
$box = $_POST['box'];
foreach ($box as $x) {
echo $x;
}
Edit:
You have to use serializeArray function in jQuery. Then it will work with this code.
Provided that your server is receiving a string that looks something like this
$("form").serialize();
"param1=someVal¶m2=someOtherVal"
...something like this is probably all you need:
$params = array();
parse_str($_GET, $params);
$params should then be an array modeled how you would expect. Note this works also with HTML arrays.
See the following for more information: http://www.php.net/manual/en/function.parse-str.php
Hope that's helpful. Good luck!
your JS should be like this -
var str = $( "form" ).serializeArray();
var postData = new FormData();
$.each(str, function(i, val) {
postData.append(val.name, val.value);
});
$.ajax({
type: "POST",
data: postData,
url: action,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert(data);
}
});
Now do this in your php script -
print_r($_POST);
you will get all form data in alert box.
$data = array();
foreach(explode('&', $_POST[data]) as $value)
{
$value1 = explode('=', $value);
$data[$value1[0]] = validateInput($value1[1]);
}
var_dump($data['box']);
your data in php will contain a string like this
field1=value1&field2=value2&....
so you can get your value1 using $_POST['field1] , value2 with $_POST['field2']
Change
data: "box1="+str,
into
data: str,
serialize() will produce a string like: input1=value1&input2=value2. So in your php you can access each value with, for instance $value1 = $_PHP['input1'];
values=$("#edituser_form").serialize();//alert(values);
$.ajax({
url: 'ajax/ajax_call.php',
type: 'POST',
dataType:"json",
data: values,
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
$box=$_POST['box'];
and $box is an array.