0

I have problem, that i want to get variable $_POST to add to value of an array. Then i will encode it to json. but the $_POST variable didn't give any value. EDIT i did edit the codes: Here my codes:

    <?php
    $temp = isset($_POST['temp']) ? $_POST['temp'] : '';
    $arr["temp_display"] = $temp;
    echo json_encode($arr);

and i hope the result will be like this

  {"temp_display": value_of_$temp}

I try to use same case but I add the value of $_POST to MySQL database and its Works. Here the code:

   <?php
    include("connect.php");
    $link=Connection();
    $temp=$_POST["temp"];
    $query = "INSERT INTO `templog` (`temperature`) 
    VALUES ('".$temp."')"; 
    mysql_query($query,$link);
    mysql_close($link);
    ?>

EDIT Here my jquery code:

     $(document).ready(function() {

        setInterval( update, 200);

          function update(){

               var cache = $('.deneme').children();

               $.getJSON('json.php', function(data) {
                $('.deneme').text(data.temp).append(cache);

             })

        }

    });

Thank you for Ur help...

markonah
  • 1
  • 2

2 Answers2

0

In the first code block this line $temp=$_POST["temp"]; is unnecessary as you have done the same thing in the previous line. You can try by displaying the json data using var_dump method like this:

echo "<pre>";
var_dump(json_encode('$arr');
echo "</pre>"; 
Imran
  • 4,011
  • 1
  • 15
  • 32
0
<?php
  $temp = isset($_POST['temp']) ? $_POST['temp'] : '';
  $arr["temp_display"] = $temp;
  echo json_encode($arr);
?>
Natalie Hedström
  • 2,476
  • 2
  • 21
  • 33
J.K
  • 1,350
  • 1
  • 11
  • 26