Hi bors, plz help me. i have no more strength for fight with this..
when i use like below its working fine (without passing data)..
<div id='result_table'>
</div>
.....script type='text/javascript' language='javascript'....
$('#getdata').click(function(){
$.ajax({
url: '<?php base_url() ?>message/sam',
type:'POST',
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
....script....{
$data = 'success';
echo json_encode($data);
}But when i use like below its not working.. (passing with data)
<div id='result_table'>
</div>
.....script type='text/javascript' language='javascript'....
$('#getdata').click(function(){
var name= "johns";
$.ajax({
url: '<?php base_url() ?>message/sam',
type:'POST',
dataType: 'json',
data: { name: name },
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
....script....{
$name = $this->input->post('name');
echo json_encode($name);
}also i tried wit some other methods but did’nt got the result.. friends plz i need your help..
Code looks fine from what I can tell, no obvious problems. Can you use Firebug or something to inspect the request and make sure the POST data is all there and it’s posting to the correct address? Check the return data of the request too, and maybe test the request with cURL.
Hi Seegan,
Try it:
$('#getdata').click(function(){
var name= "johns";
$.ajax({
url: '<?php base_url() ?>message/sam',
type:'POST',
data: "{name:'" + name + "'}",
contentType: "application/json",
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});I hope, it would help you.
Also, as Kevin suggested, use Firebox console to see the request/response.
Best Regards,
It looks like you’re trying to pass data through AJAX to a PHP controller function and display the output on the web page. Based on the code you provided, it seems like there may be a couple of issues with your implementation.
First, it looks like you’re not returning the base URL in your AJAX URL. You should include the echo statement before base_url() so that it returns the base URL string. Here’s how you can modify your AJAX code:
php Copy code $.ajax({ url: ‘<?php echo base_url(); ?>message/sam’, type: ‘POST’, dataType: ‘json’, data: { name: name }, success: function(output_string){ $(‘#result_table’).append(output_string); } // End of success function of ajax form }); // End of ajax call Second, in your PHP controller function, you’re returning the value of the $name variable as JSON, but you’re not wrapping it in an array or object. When you pass data through AJAX, it’s usually best to wrap the data in an array or object, so that you can pass multiple values if needed. Here’s how you can modify your controller function:
php Copy code $name = $this->input->post(‘name’); $data = array(‘name’ => $name); echo json_encode($data); With these changes, you should be able to pass data through AJAX to your PHP controller function and display the output on your web page.
There don’t seem to be any issues with the code as far as I can determine. Is there a way to check the request using Firebug or another tool to verify that the POST data is included and sent to the proper address? Additionally, you should examine the request’s return data and slope, if possible, run the request using cURL.
What is the exact URL that your AJAX call is hitting when data is included does Stickman Hook it differ from the working one without data?
Hey seegan, been there! Ajax can be a real headache sometimes. It sounds like you’re on the right track. Double-check that your PHP sam function is actually receiving and processing the POST data. Sometimes the server-side handling gets overlooked. Speaking of crazy things happening, anyone ever feel like coding is like herding Crazy Cattle 3D? Good luck, hope you figure it out!
Hey seegan, been there! Ajax can be a real headache sometimes. It sounds like you’re on the right track. Double-check that your PHP sam function is actually receiving and processing the POST data slide down free.
Having trouble with jQuery AJAX and passing data? It’s a common hurdle. Ensure your data is correctly formatted as a JavaScript object. Double-check the data type in your AJAX settings, usually ‘json’ or ‘urlencoded’. Think of it like navigating a tricky level in Slope Game; precise movements and understanding the environment are key to success. Debugging tools can be your best friend here, helping you spot those hidden syntax errors stopping your data from reaching the server.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.