We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

jquery ajax with passing data not working

Development and Programming

seegan's avatar
seegan
10 posts
13 years ago
seegan's avatar seegan

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....

controller function

{
        $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....

controller function

{
        $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..

       
nishant1234's avatar
nishant1234
14 posts
13 years ago
nishant1234's avatar nishant1234

don’t use it.

       
seegan's avatar
seegan
10 posts
13 years ago
seegan's avatar seegan

Then how to solve this problem plz reply some one…

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
13 years ago
Kevin Cupp's avatar Kevin Cupp

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.

       
Bhashkar Yadav's avatar
Bhashkar Yadav
727 posts
13 years ago
Bhashkar Yadav's avatar Bhashkar Yadav

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,

       
yhumisan's avatar
yhumisan
1 posts
3 years ago
yhumisan's avatar yhumisan

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.

Cheers, Limo Service for corporate Orange County

       
agesnoso's avatar
agesnoso
3 posts
2 years ago
agesnoso's avatar agesnoso

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.

       
holeioonline's avatar
holeioonline
1 posts
10 months ago
holeioonline's avatar holeioonline

I am looking forward to your reply on how to resolve this issue. hole io

       
degoyib's avatar
degoyib
2 posts
about 6 months ago
degoyib's avatar degoyib

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?

       
Richardas's avatar
Richardas
1 posts
5 months ago
Richardas's avatar Richardas

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!

       
Aidly1955's avatar
Aidly1955
2 posts
2 months ago
Aidly1955's avatar Aidly1955

99 Nights in the Forest is an immersive survival adventure where players explore a mysterious forest, face dangerous creatures, and uncover hidden secrets across 99 thrilling nights.

       
lanaderay's avatar
lanaderay
1 posts
4 weeks ago
lanaderay's avatar lanaderay

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.

       
LoisMattox's avatar
LoisMattox
1 posts
2 weeks ago
LoisMattox's avatar LoisMattox

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.