sending data through ajax to CI controller - php

I am new to ajax and CI .I want to send data and image through ajax . In my view i have 3 input fields and one image upload button .
var val1 = $("#val1"+id).val();
var val2 = $("#val2").val();
$.ajax({
type: "POST",
url: "page/save_data",
data: "{ val1 :'"+val1+"',val2:'"+val2+"}",
success: function(msg) {
alert(msg);
}
});
and in controller when i try this it shows me nothing
function save_data()
{
$val = $this->input->post('val1');
echo $val1;
}
In console it gives me nothing .

Try this :
$.ajax({
type: "POST",
url: "page/save_data",
data: { "val1 ":val1,"val2": val2},
success: function(msg) {
alert(msg);
}
});

Your ajax URL must be refer to your method name;
...
$.ajax({
type: "POST",
url: "page/save_data", //change this to your method;
...
should be like:
...
$.ajax({
type: "POST",
url: "page/save_iudata",//or whatever your method's name;
...
EDIT:
try this way:
function save_data(){
$val1 = $_REQUEST['val1'];
echo $val1;
}

Hello to send data via ajax is easy:
var data = {
val1: $('#val1').val(),
val2: $('#val2').val(),
};
$.ajax({
url: "<?php echo base_url().'page/save_data'; ?>",
dataType: 'json',
type: 'POST',
async : false,
data: data,
success: function(msg){
...
}
});
return false;
But to send an image you have to do some research...
here is a good tutorial

Related

php jquery dynamic dropdown

I am having problem sending/reading ajax variable.
Ajax code is as below:
$.ajax({
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
I have tried to read it in another php file like this:
$InvoiceType = $_REQUEST['it'];
//$InvoiceType = $_POST['it'];
//$InvoiceType = $_GET['it'];
But none of above works. The variable $InvoiceType always stays empty.
What is the problem?
The best way is to use POST method like this :
$.ajax({
method: "POST",
url: ""/wp-content/themes/canvas-child/get-clients-dropdown.php",
data: { it: 1, param2: "value2" }
})
You can get your value in $_POST['it']
Please try it with full url of file with get_template_directory_uri().
$.ajax({
type: 'GET',
url: '<?php echo get_template_directory_uri(); ?>/get-clients-dropdown.php',
data: {it: 1},
success: function(data) {
$("#ClientID").html(data);
}
});
$.ajax({
type: "GET",
data: {it: "1"},
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
try this
You have to use it like this:
$.ajax({
type: "POST",
url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
success: function(data) {
$("#ClientID").html(data);
}
});
Then in PHP File use this:
$InvoiceType = $_POST['it'];

How to pass value to PHP Script

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'];
?>

jQuery is not sending data to codeigniter

I am trying to get ajax working with a codeigniter installation.
This is my PHP function:
function test() {
return print_r("hey");
}
This is the JS:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
success: function(data) {
alert(data);
}
});
This works perfectly but, as soon as I add data it doesn't work.
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: {bar:"foo"},
success: function(data) {
alert(data);
}
});
Thanks in advance!
please check the following
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "&bar=foo&isAjax="+true,
success: function(data) {
alert(data);
}
});
And controller...
function test() {
if($this->input->post('isAjax')){
return print_r("hey");
}
else{
//do another thing
}
}
And another thing if you want to add data in json format then you have to add another property in your $.ajax object that is datatype: "json"
Use following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
dataType: 'json',
data: {'bar':'foo'},
success: function(data) {
alert(data);
}
});
Or you can use shorthand version:
$.post("http://localhost/code/test", {'bar':'foo'}, function(data) {
alert(data);
});
And your php code should be:
function test() {
echo "hey";
}
Try the following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "bar=foo&name=cyberbob",
success: function(data) {
alert(data);
}
});

jQuery ajax fetch data from PHP into a jQuery variable

I need to receive the status of vote using ajax and php and jquery. Following is my code :
var VoteStatus= GetStatus() ;
var ID = $('#ID').val();
function GetStatus() {
var res = '';
$.ajax({
type: "POST",
data: {VoteID:ID} ,
url: "/vote_status.php",
async: false,
success: function(result) { res=result; }
});
return res;
}
alert('Vote Status= ' + VoteStatus);
In my php file:
$VoteID = $_POST['VoteID'];
$Property = $_POST['Property'];
if ( $VoteID == 0 )
echo 'No Vote provided - Property = '. $Property;
exit;
The alert box shows: Vote Status = No Vote Provided
Please help.
I have posted the VoteID, but the php file doesn't seem to receive it.
Try the alert in here and check if its working
$.ajax({
type: "POST",
data: {"VoteID":ID} ,
url: "/vote_status.php",
async: false,
success: function(result) {
alert(result); }
});
The name of the POST variable needs to be in quotes, as in
data: {"VoteID":ID}
Try this and check jquery ajax manuals
$.ajax({
type: "POST",
data:"VoteID=" + ID +"&secondparam=" + secondvalue,
url: "/vote_status.php",
async: false,
success: function(result) { alert(result); }
});

Ajax passing data to php script

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);
}
});

Categories