Welcome Guest, Not a member yet? Register   Sign In
Does anyone see why my jquery ajax call is not posting?
#1

[eluser]Bart v B[/eluser]
Hello all,

I have a problem with my ajax call.
He does not post to my controller where I should get an output.
someone might see what I overlooked?
a simple print_r ($ _POST) gives an empty array.
When I request him to the controller I get the proper value back.

My jquery code:
Code:
$(document).ready(function() {
    $('select').change(function() {
          //alert('Changed!');
        var item = $('#item').val();
        //alert(item); works
        
        $.ajax({
           type: "POST",
           url: "http://localhost/cobra-scripters.nl/hostingplan/",
           data:$(this).val(), //works also
           success: function(data) {
           $("#display").html(data);          
           }
          
         });

    });
    
});

My form code:
Code:
<fieldset>
          
            <legend>Maak een keuze</legend>
            <div id="display"></div>
            
            &lt;?php echo form_open('webhosting/bestel');?&gt;
            <label class="forminput" for="item">Keuze</label>
            <select name="item" id="item">
              <option value="" >--</option>
              <option value="1">shared hosting</option>
              <option value="2">wegwerp hosting</option>
              <option value="3">sleur pleur hosting</option>
            </select><br />
             &lt;input type="submit" value="Volgende" class="knop" /&gt;&lt;br />
            &lt;?php echo form_close(); ?&gt;
            
        </fieldset>

my controller
Code:
&lt;?php

class Hostingplan extends Controller
{
    public function index()
    {
        print_r($_POST);
        $this->load->model('hosting_model');
        
            if($this->input->post('item') == 1 )
            {
                $id = ' per jaar';
            }
            if($this->input->post('item') == 2 )
            {
                $id = ' per call/sms per maand';
            }
            if($this->input->post('item') == 3)
            {
                $id = ' voor het standaard pakket';
            }
        
            
             $hostings = $this->hosting_model->getHosting();
              foreach($hostings as $hosting):
              $data=  '<h2><i>Uw keuze:</i> '.$hosting->name.'</h2>';
              $data.= '<p>De kosten zijn: &euro; <b>'.$hosting->price.' </b>'.$id.' inclusief BTW.</p>';
              $data.= '<p><a href="'.base_url().'webhosting/bestel/">Nee, toch een andere keuze</a></p>';
              endforeach;
            
            echo $data;
        }
    
}

So in short i don't get anything back from jquery.
thx for the help.
#2

[eluser]Bastian Heist[/eluser]
I remember having issues with this as well, have you tried using jquery.post() instead of jquery.ajax() ?
#3

[eluser]Bart v B[/eluser]
I think i solved the problem..

Code:
$(document).ready(function() {
    $('select').change(function() {
          //alert('Changed!');
        var item = $('#item').val();
        //alert(item); //works
    
        $.ajax({
                type: "POST",
              url: "http://localhost/cobra-scripters.nl/hostingplan/",
               data: "item="+ item,
               success: function(data){
              $("#display").html(data);
  }
});
        });

    });

It seems like jquery uses an array from the data so i had to put item= as reference.
#4

[eluser]danmontgomery[/eluser]
This is covered in detail in the jQuery documentation.

http://api.jquery.com/jQuery.ajax/
#5

[eluser]Bart v B[/eluser]
@Noctrum.

Your right. Found figured it out from the manual.
As alway's first try and then read the manual. Wink
I had the function working a couple of weeks ago, but mij development pc had some weird problems.
I did an system restore and this function was gone without backup. So i had a big panic last night and this morning.




Theme © iAndrew 2016 - Forum software by © MyBB