1

I have a problem, how to pass an argument with ajax in url: <?php echo base_url() . 'user_m/getAdministrator'; ?>" argument, Because I tried to populate a selectbox based on another but I didn't have success.

html:

<div class="form-group">
                        <label>Denumirea intreprinderii:</label>
                        <select class="form-control marg-left-10" id="firm">
                            <?php if($all_firms): ?>
                                <?php foreach($all_firms as $firm): ?>
                                    <option value="<?php echo $firm['id_firm']; ?>"><?php echo $firm['name_firm']; ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Administrator</label>
                        <select class="form-control marg-left-10" id="admin">

                        </select>
                    </div>

php:

  public function getAdministrator()
{
    $id_firm = $_POST['id_firm'];
    $this->load->database();
    $sql = $this->db->query("SELECT * FROM users,firms WHERE firms.id_firm = users.fk_firm and users.id = ");
    while($row=mysql_fetch_array($sql))
    {
        $id=$row['id'];
        $data=$row['name'];
        echo '<option value="'.$id.'">'.$data.'</option>';
    }
    return $this;
}

script:

    $(document).ready(function()
{
    $("#firm").change(function()
    {
        var id_firm=$(this).val();
        var dataString = 'id_firm='+ id_firm;
        $.ajax
        ({
            type: "POST",
            url: "<?php echo base_url() . 'user_m/getAdministrator'; ?>",
            data: dataString,
            cache: false,
            success: function(html)
            {
                $("#admin").html(html);
            }
        });

    });

});

Help me please.The first select box gets populated but the second doesn't.Please Help me.Exist a method to pass an argument to user_m/getAdministrator method? HELP ME PLEASE GUYS!!!!!

erhanelu ion
  • 71
  • 1
  • 4
  • 12
  • Are you sure your script is called just `getAdministrator`? You might be forgetting the `.php`. – Andrea Aug 20 '14 at 11:30
  • 1
    userm_m is class and getAdministrator is the method – erhanelu ion Aug 20 '14 at 11:31
  • Also, you might want to use your browser's developer tools and look at the Network tab to see if the ajax request errored. – Andrea Aug 20 '14 at 11:31
  • here you are return $this. It is an array. not option tag – Vinod VT Aug 20 '14 at 11:33
  • Yes, I deleted return $this – erhanelu ion Aug 20 '14 at 11:34
  • duplicate found here: http://stackoverflow.com/questions/19957823/ajax-call-to-populate-form-fields-from-database-query-when-select-value-changes – Swapnil Aug 20 '14 at 11:48
  • you forgot `base_url()` with trailing slash `/`. `base_url() . '/user_m/getAdministrator'` if still on error open your browser debugging tools and look at the network tab, you will find if the server returned a 404 or a 500 or something else. And please if you are using a framework utilize the function `$this->input->post()` for better results – tomexsans Aug 20 '14 at 16:07

1 Answers1

0

Php in js is not a good practice, try to write this on your main php file before calling the script

<script>
var base_url = "<?=base_url()?>";
</script>

and then in your js file:

$.ajax
({
        type: "POST",
        url: base_url+"user_m/getAdministrator",
        data: dataString,
        cache: false,
        success: function(html)
        {
            $("#admin").html(html);
        }
    });

also, you can send data this way:

data: { id_firm: id_firm }, 
Gerardo Rosciano
  • 901
  • 5
  • 11
  • Debug a little bit more to make sure what's happening, the code i put there might fix the base url problem, but with admin tools (like firebug) you should be able to see 1) if the url is correct, 2) if the variable id_firm is being send, 3) if the ajax page is replying correctly. Then you can pinpoint your error. there can be a lot of things going on, for example, if you are using CodeIgniter, $_POST['id_firm'] will be blank, you need to use $this->input->post("id_firm"); – Gerardo Rosciano Aug 20 '14 at 12:44