Welcome Guest, Not a member yet? Register   Sign In
Ajax help please
#1

[eluser]RobertB.[/eluser]
View (vSelector_view)
Code:
$(document).ready(function() {
    
    $('#item').change(function(){
        
        var item = $('#item').val();
        //alert(item);

        $.ajax({
            url: "vSelector/ajax",
            type: "POST",
            dataType: "json",
            success: function (response){
alert(response.element);
                //$('#' + response.element).append(response.output);
              
            }
                  
        });
        
    });
    
});

<?=form_open('vSelector', $form_data)?>
<div class="span-7">
<select id="item" name="Item">
<option value="-">-</option>
   &lt;?php foreach($items as $row => $item): ?&gt;
    <option value="&lt;?=$item->item_id?&gt;">&lt;?=$item->item?&gt;</option>
   &lt;?php endforeach; ?&gt;
</select>
</div>
&lt;?=form_close()?&gt;

Controller (vSelector)
Code:
function __construct()
    {
        parent::__construct();
        
        $this->load->model('vSelector_model', 'vSelector');
    }

function ajax()
    {
        
        $item = $this->input->post('item');
        $data['objects'] = $this->vSelector->getObjects($item);

        $output['output'] = '';        
            foreach($data['objects'] as $key => $object){
                $data['output'] .= '<option value="'.$object->objectId.'">'.$object->object.'</option>';
            }
            $data['element'] = 'object';
        
        echo json_encode($data);
    }

The only way that I can get an alert is if I comment out.
Controller (vSelector)
Code:
function __construct()
    {
        parent::__construct();
        
        $this->load->model('vSelector_model', 'vSelector');
    }

function ajax()
    {
        
        $item = $this->input->post('item');
#        $data['objects'] = $this->vSelector->getObjects($item);

#        $output['output'] = '';        
#            foreach($data['objects'] as $key => $object){
#                $data['output'] .= '<option value="'.$object->objectId.'">'.$object->object.'</option>';
#            }
            $data['element'] = 'obj';
        
        echo json_encode($data);
    }
Then I will get an alert with "obj"

Obviously this not what I want to do, I'm just trying to understand why is not returning any data or errors.
#2

[eluser]cahva[/eluser]
You are not passing the item at all to your url. You've set the var item = '...' but not used it in the ajax call.

Try this:
Code:
$(function() {
    $('#item').change(function(){
        $.post('vSelector/ajax',{ "item": $(this).val() }, function(response) {
            alert(response.element);
        }
        ,'json');
    });
});
#3

[eluser]RobertB.[/eluser]
This
Code:
$(function() {
    $('#item').change(function(){
        $.post('vSelector/ajax',{ "item": $(this).val() }, function(response) {
            alert(response.element);
        }
        ,'json');
    });
});

or
Code:
$('#item').change(function(){
   $('#item').change(function(){
        $.post('vSelector/ajax',{ "item": item }, function(response) {
alert(response.element);      
        }
        ,'json');
    });    
});



I get this response with FireBug
Code:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index: output</p>
<p>Filename: controllers/vSelector.php</p>
<p>Line Number: 34</p>

</div>{"objects":[{"objectId":"27","object":"objectName"}],"output":"<option value=\"27\">objectName<\/option>","element":"obj"}

But no alert();



Ps. By the way I'm having the hardest time posting the ajax code, it keeps telling me that no urls are allowed
#4

[eluser]cahva[/eluser]
The undefined index is because:
Code:
$output['output'] = '';
U Probably meant $data['output'].

But as you can see, its already getting you the json part(after the notice).


And for the js part this:
Code:
$(function() {
..is the same as this:
Code:
$(document).ready(function() {
Its just a shorthand.
#5

[eluser]RobertB.[/eluser]
Cahva that was the problem, that's what happens when you copy codes and thanks about the js part I did not know about the shorthand code.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB