0

I have a problem like this.

<select onchange="select()" id="select">
    <option value="1">test 1</option>
    <option value="2">test 2</option>
</select>

I take the value of #select with javascript and send with ajax to php

  var clientVal = $('#select').val();
  $.ajax({
        type:'post',
        url:'../../works/addWork.php',
        dataType:'JSON',
        data:{
            client_val:clientVal
        },
        success:function (res) {
            console.log(res);
        }
    })

php

 $client_id = $_POST['client_val'];
$row_subClient = $conn->query("SELECT `id`,`name` from `subClient` where `client_id`=$client_id");
$sub_client_arr = [];

while($result_client = $row_subClient->fetch_assoc()){
    $sub_client_arr[$result_client['id']] = $result_client['name'];
}
echo json_encode($sub_client_arr);

There isn't any problem here.After getting in php,I select from database my datas.There isn't problem here too.But when I do json_encode($array),and console.log(res) to javascript.It gives me back all html page with my arrays.Where is my problem,that I get with html? Please help me.Thank you in advance.

V.Aleksanyan
  • 427
  • 1
  • 4
  • 6
  • can you post the PHP? – Samuel Cook Aug 01 '17 at 17:04
  • Hard to tell without seeing ../../works/addWork.php – mplungjan Aug 01 '17 at 17:05
  • $client_id = $_POST['client_val']; $row_subClient = $conn->query("SELECT `id`,`name` from `subClient` where `client_id`=$client_id"); $sub_client_arr = []; while($result_client = $row_subClient->fetch_assoc()){ $sub_client_arr[$result_client['id']] = $result_client['name']; } echo json_encode($sub_client_arr); – V.Aleksanyan Aug 01 '17 at 17:07
  • Looking at your question, most probably, you are not setting the header to json. Please refer this answer to see how to set header as json. https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script – shreyas tigdi Aug 01 '17 at 17:10
  • header('Content-Type: application/json'); Add this in addwork.php, please find my answer below – Channaveer Hakari Aug 01 '17 at 17:20

2 Answers2

2

So you have added dataType:'JSON' which expects JSON data from the addwork.php file.

So in your adwork.php file if you have an array data then you need to make a json data which can be done in php with json_encode.

$arrayData = array(
'key' => 'value',
'key' => 'value',
'key' => 'value',
'key' => 'value'
);

To encode this in php you can use 

header('Content-Type: application/json');
return json_encode($arrayData);

Okay so another problem as Sergej said the problem might be as follows in your php file your doing

echo json_encode($arraDetails);

<html content below here>

You can over come that problem by

echo json_encode($arraDetails);
exit; // This will make sure that the below content won't execute
<html content below here>
Channaveer Hakari
  • 2,338
  • 1
  • 26
  • 34
  • The header is not necessarily required if `dataType: "json"` is set. And if your code is not surrounded by a function, you have to `echo` the encoded json instead of returning it. – Dan Aug 01 '17 at 17:31
  • I had similar type of problem. Where I wanted JSON data which should return from my php. When I added the above approach it solved my issue. So thought I should add that. – Channaveer Hakari Aug 01 '17 at 17:34
0

Try to put exit; after you print the json in the backend. The HTML is sent by backend. So the problem is definately there.

Sergej
  • 1,559
  • 11
  • 21