-3

I am working on a project for a crm(just to learn) and i have trouble with an ajax function.

<div class="dropdown">
    <label>Name</label>
    <select class="named" name ="named" id="named" onchange="userData('<?php echo $client['client_name']; ?>')">
        <?php
        $clients=mysqli_query($db->db,"SELECT client_name FROM clients");
        foreach($clients as $client): ?>
        <option value="<?= $client['client_name'];?>"><?= $client['client_name']; ?></option>
    <?php endforeach; ?>
    </select>

</div>
<div class="form-group">
    <label>Location</label>
    <input type="text" class="form-control" name="locationd" id="locationd"/>

and the ajax function

function userData(name){
    alert('debug');
    $.ajax({
        type: 'POST',
        dataType:'JSON',
        url: 'adminAction.php',
        data: 'action_type=clientdata&name='+name,
        success:function(data){
            alert('debug');
            $('#locationd').val(data.client_location);
        }
    });
}

its a mysql generated dropdown and on change it should fill the location input with this clients location but it doesnt work.Apache returns no errors but i am kinda noob especially when it comes to debugging ajax calls(how the ** can i see what data it sends and receives ? :D).Thanks for your help in advance !! I post the adminaction.php code too

if($_POST['action_type'] == 'clientdata'){
            $tblName = 'clients';
            $conditions['where'] = array('client_name' =>$_POST['named']);
            $conditions['return_type'] = 'single';
            $client = $db->getRows($tblName,$conditions);
            echo json_encode($client);
            $tblName  = 'clients_contact';

for anyone interested i fixed it like that

function userData(){
            userData = $("#named").serialize()+'&action_type=clientdata';
            $.ajax({
                type: 'POST',
                dataType:'JSON',
                url: 'adminAction.php',
                data: userData,
                success:function(data){
                    $('#locationd').val(data.client_location);
                }
            });
        }

2 Answers2

1

Usually the main mistake with ajax calls is, that they don't send the expected requests or don't receive the expected results. Both of those can at least be seen with almost every decent browser by:

  • pressing F12,
  • going to the network tab and then
  • check the requests that are made from this point on (sometimes a reload of the page is necessary).

Also: almost every decent browser has a javascript debugger somewhere, where you can set breakpoints and step through your code, while watching all the variables of interest.

Jakumi
  • 8,043
  • 2
  • 15
  • 32
0

If your AJAX output is in JSON format you can use $.parseJSON(result) to decode your JSON data to Array.

Marin Atanasov
  • 3,266
  • 3
  • 35
  • 39
Manish Arora
  • 259
  • 1
  • 4
  • 11