when i m running my project on local host then the ajax is working properly. but when i m uploading it on server then its not working... please tell me the sollution
This is my Controller
public function select_subcategory()
{
$str='<option value="">Select Option</option>';
$id=$this->input->post('id');
$res=$this->db->query("Select * from tn_topic_subcategory where category='".$id."'")->result();
foreach($res as $key)
{
$str.='<option value="'.$key->subcategory_id.'">'.$key->sub_ctg_name.'</option>';
}
echo $str;
}
And this is my Ajax
<script type="text/javascript">
function change_cat(id)
{
$.ajax({type:'post',
url:'<?PHP echo base_url()?>admin/select_subcategory',
data:{id:id},
success:function(res){
// alert(res);
$('#topic-subcategory').html(res);
}
});
}
</script>
If you are supposed to get your response in json format then try below code.
it will help you to send your variable in json format.
{
$.ajax({type:'post',
url:'<?PHP echo base_url();?>admin/select_subcategory',
data:{id:id},
dataType: "json",
success:function(res){
// alert(res);
$('#topic-subcategory').html(res);
}
});
}
Also try to send your response in json encoded.
I hope it will work for you.
Thanks
Related
I want to give some alert message in my page but it working
This is my view code:
$.ajax({
url :"<?php echo base_url();?
>booking/dispatch_challan/DispatchChallanController/createDispatchChallan",
type:"POST",
dataType: "json",
data:$("#dispatch_challan_form").serialize(),
data:$("#dispatch_challan_form").serialize()+'&disp_ch_ids='+allVals,
success: function(data)
{
if(data.create_dispatch_challan.form_status=='false')
{
alertify.error('you may occured some server side errors');
}else if(data.create_dispatch_challan.form_status=='true')
{
alertify.success(data.create_dispatch_challan.form_message);
if (confirm())
{
alert("print");
}else{
alert("Do not print");
}
}
}
});
please give me same solution in this.
Its not OK to have a new line here
url :"<?php echo base_url();?
>booking/dispatch_challan/DispatchChallanController/createDispatchChallan",
Try this code: pastebin
I believe I have the right syntax but am missing something important. Been searching on here for a while but can't figure out why the POST variable is not being detected. Basically my .ajax is firing because my test statement has been changing due to the value but some reason can't receive variable via $_POST (i.e. my echo in php echo that it is not firing) Also the native file and php that I am sending it to are the same file blankFormTemplate.php but don't think that should be an issue.
$(document).ready(function()
{
var $selectedContexts = [];
$('.allContextField').change(function(){
//alert($(this).val());
hideField = $(this).val();
$('#'+hideField).remove();
$.ajax
({
type: "POST",
url: "blankFormTemplate.php",
data: addedContext=hideField,
cache: false,
success: function(addedContext)
{
$('#test').html($('#test').html()+hideField);
}
});
});
});
in my PHP blankFormTemplate.php:
<?php
if(isset($_POST['addedContext']))
{
echo 'hello';
}
else
{
echo 'why';
}
?>
Any help would be greatly appreciated.
Thanks,
Your javascript
$(document).ready(function()
{
$('.allContextField').change(function(){
hideField = $(this).val();
$('#'+hideField).remove();
});
if (hideField.trim()) {
$.ajax
({
type: "POST",
url: "blankFormTemplate.php",
data: (addedContext:hideField},
cache: false,
success: function(msg)
{
$('#test').html(msg);
}
});
}
});
Your PHP
<?php
if(isset($_POST['addedContext']))
{
echo 'hello';
}
else
{
echo 'why';
}
?>
Just starting to learn about ajax although I am running into trouble trying to return a success message in an array.
<script type="text/javascript">
$(function () {
$('#delete').on('click', function () {
var $form = $(this).closest('form');
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize()
}).done(function (response) {
if (response.success) {
alert('Saved!');
} else {
alert('Some error occurred.');
}
});
});
});
</script>
<?php
$array = array();
$array['success'] = TRUE;
echo $array;
?>
response.success should refer to $array['success'] correct?
You are trying to echo your array, which will just spit out "Array".
Instead you need to encode your array as JSON and echo that out.
Change this:
echo $array;
To this:
echo json_encode($array);
Also you probably need to add your dataType in your ajax params so jQuery auto-parses the response:
dataType : 'json' // stick this after "data: $form.serialize()" for example
Also, here's a good post to read on how to properly handle success/errors with your ajax calls (thanks #Shawn):
Jquery checking success of ajax post
I have a js script that does an ajax request and posts the data to a php script, this script with then echo something back depending if it works or not.
here is the JS
$(document).ready(function(){
var post_data = [];
$('.trade_window').load('signals.php?action=init');
setInterval(function(){
post_data = [ {market_number:1, name:$('.trade_window .market_name_1').text().trim()},
{market_number:2, name:$('.trade_window .market_name_2').text().trim()}];
$.ajax({
url: 'signals.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data:{markets:post_data},
dataType: "json",
success: function(response){
console.log("Response was " + response);
},
failure: function(result){
console.log("FAILED");
console.log(result);
}
});
}, 6000);
});
here is the php:
if(isset($_POST["json"]))
{
$json = json_decode($_POST["json"]);
if(!empty($json))
{
echo "IT WORKED!!!!";
}
else
echo "NOT POSTED";
}
So basically, i thought the response in the `success: function(response)' method would be populated with either "IT WORKED!!!" or "NOT POSTED" depending on the if statement in the php. Now everything seem to work because the js script manages to go into the success statement but prints this to the console:
Response was null
I need to be able to get the return from the server in order to update the screen.
Any ideas what I'm doing wrong?
Try:
if(isset($_POST["markets"]))
{
$json = json_decode($_POST["markets"]);
if(!empty($json))
{
echo "IT WORKED!!!!";
}
else
echo "NOT POSTED";
}
use this in your php file
if(isset($_POST["markets"]))
{
}
instead of
if(isset($_POST["json"]))
{
.
.
.
.
}
Obiously the if(isset($_POST["json"])) statement is not invoked, so neither of both echos is executed.
The fact that the function specified in .ajax success is invoked, only tells you that the http connection to the url was successful, it does not indicate successful processing of the data.
You are using "success:" wrong.
Try this instead.
$.post("signals.php", { markets: post_data }).done(function(data) {
/* This will return either "IT WORKED!!!!" or "NOT POSTED" */
alert("The response is: " + data);
});
Also have a look at the jQuery documentation.
http://api.jquery.com/jQuery.post/
Look, You send data in market variable not in json. Please change on single.php code by this.
$json_data = array();
if(isset($_POST["markets"]))
{
// $json = json_decode($_POST["markets"]);
$json = ($_POST["markets"]);
if(!empty($json))
echo "IT WORKED!!!!";
else
echo "NOT POSTED";
}
And change on your ajax function
$(document).ready(function(){
var post_data = [];
$('.trade_window').load('signals.php?action=init');
setInterval(function(){
post_data = [ {market_number:1, name:$('.trade_window .market_name_1').text().trim()},
{market_number:2, name:$('.trade_window .market_name_2').text().trim()}];
$.ajax({
url: 'signals.php',
type: 'post',
// contentType: 'application/json; charset=utf-8',
data:{markets:post_data},
dataType: "json",
success: function(response){
console.log("Response was " + response);
},
failure: function(result){
console.log("FAILED");
console.log(result);
}
});
},6000);
});
You have to you change you $.ajax call with
//below post_data array require quotes for keys like 'market_number' and update with your required data
post_data = [ {'market_number':1, 'name':'name1'},
{'market_number':2, 'name':'name2'}];
//console.log(post_data);
$.ajax({
url: "yourfile.php",
type:'post',
async: true,
data:{'markets':post_data},
dataType:'json',
success: function(data){
console.log(data);
},
});
and you php file will be
<?php
if(isset($_POST['markets']))
{
echo "It worked!!!";
}
else
{
echo "It doesn't worked!!!";
}
//if you want to work with json then below will help you
//$data = json_encode($_POST['markets']);
//print_r($data);
?>
in your php file check the $_POST:
echo(json_encode($_POST));
which will tell if your data has been posted or not and the data structure in $_POST.
I have used the following code to covert the posted data to associative array:
$post_data = json_decode(json_encode($_POST), true);
I want to get a Twitter search with jQuery and pass it to a php script for formatting. I'm new to jQuery, so would love if someone could tell me if this hopelessly off?
This is my jQuery, which is supposed to call up Twitter, get the json, and then pass the json to php.
//jquery
$(document).ready(function() {
var twUrl = "http://search.twitter.com/search.json?q=twitter&rpp=5&callback=?";
$.jsonp({
url: twUrl,
data: {},
dataType: "jsonp",
callbackParameter: "callback",
timeout: 5000,
success: function(data){
$.post("search_back.php", {json_data: data},
function(data) { $("#search_word").html() });
}});
});
and the php is supposed to pick it up, format it (not included, but I know how to do that part), and pass it back into the #search_word.
//search_back.php
$output = json_decode($_POST["data"], true);
foreach ($output as $tweet){
echo $tweet;
}
Is this close?
Really appreciate some help!
Ok, excited now :) Got it to work, just posting for reference:
<script>
$(document).ready(function() {
var twUrl = "http://search.twitter.com/search.json?q=twitter&rpp=5&callback=?";
$.getJSON(twUrl,
function(data) {
console.log(data);
$.post("search_back.php", {json_data: data}, function(data) {
console.log(data);
console.log("ok");
$("#search_word").html(data) });
}
)});
</script>
and
<?php
$output = $_POST["json_data"];
foreach ($output[results] as $tweet){
echo $tweet[from_user] . "<br>";
}
?>
Seems what goes to php is already json_decoded,true? Thanks a lot for your help!