0

Complete rewrite of my question

The task I am trying to do is update the display of five variable without reloading the whole page.

I have a php file (check_conf_update.php) which runs a query that produces data and I am scripting it into JSON. In the PHP file that runs the query I have at the end:

echo json_encode($record);

The JSON result looks like this:

[{"ClientName":"Another","RoomFromDateTime":"2016-02-25 01:00:00","RoomToDateTime":"2016-03-13 23:00:00","ClientImageName":"anothernew.png","DisplayText":"System Testing"}]

I now need to use the data on a page called "template.php). How can I read the data from the Json result and assign each of the result elements in variables I can use on my "template.php" page. I need the Json script to run every x seconds so the display is always shows up todate information.

I have five php variables:

$CientName
$ImageName
$DisplayText
$FromTime
$ToTime 

which I use on my web page to display the data on the same page as the script below.

$(document).ready(function() { 
    function runupdate() { 
       $.ajax({ 
         url: 'check_conf_update.php',
         type: 'GET',
         data: 'record',
         dataType: 'json',
            success: function(data){ 
                // not sure what I need to do here
             } 
         });  
    };  

    // run it initially
    runupdate();

    // run it every 30 seconds
    setInterval(runupdate, 30 * 1000);
});

Sorry if have confused anyone, and it looks like I did.

Can anyone help. Many thanks in advance for your time.

Regards

9
  • So where would these values come from? There is nothing shown in your javscript or html to know what you want to post Commented Feb 27, 2016 at 18:36
  • Hi, What I am tring to do is get the data from mysql which I have already done and then use the JSON result in the PHP variables. I think the POST in the jquery should be GET. Commented Feb 27, 2016 at 18:50
  • creation of PHP variables aren't possible, but instead you will be able to result individual values like data[0].ClientName would return you with "Another" Commented Feb 27, 2016 at 18:51
  • So what is the specific problem...how to create the array in php to send as json? Commented Feb 27, 2016 at 18:55
  • specify what you wanna do with PHP variables? Commented Feb 27, 2016 at 18:56

1 Answer 1

2

It's not really clear on what happens in your PHP script that produces the data. If you can update the post with the complete code for PHP also, it would be helpful. However, I'm assuming you want to use the data in the produced json string to populate the PHP variables in the destination file (check_conf_update.php)? In this case,

// check_conf_update.php
// $_POST['record'] == '[{"ClientName":"Another","RoomFromDateTime":"2016-02-25 01:00:00","RoomToDateTime":"2016-03-13 23:00:00","ClientImageName":"anothernew.png","DisplayText":"System Testing"}]'

$json = $_POST['record'];
$array = json_decode($json, true)[0];

$ClientName = $array['ClientName'];
$ImageName = $array['ClientImageName'];
$DisplayText = $array['DisplayText'];
$FromTime = $array['RoomFromDateTime'];
$ToTime = $array['RoomToDateTime'];

echo $ClientName . ', ' . $ImageName . ', ' . $DisplayText . ', ' . $FromTime . ', ' . $ToTime;

Edit:

All the PHP code in the template.php file is run on the server side before its rendered in the browser, so it will be too late to assign the updated json data into PHP variables by then. The only way to update information without reloading the page is to replace text or elements with javascript. After each successful ajax request, you can update the values in the page,

$('.clientname').html(data[0].ClientName);
$('.childbox').html(data[0].ClientImageName);
$('.clientndisplaytext').html(data[0].DisplayText);
$('.clientndisplaytime').html(data[0].RoomFromDateTime);
$('.clientndisplaytime').html(data[0].RoomToDateTime);
Sign up to request clarification or add additional context in comments.

5 Comments

Hi and thanks for your replies. I have completely rewritten the question to try and make it more clear what I am trying to do.
All the PHP code in the template.php file is run on the server side before its rendered in the browser, so it will be too late to assign the updated json data into PHP variables by then. The only way to update information without reloading the page is to replace text or elements with javascript. After each successful ajax request, you can update the values in the page, like for example $('.client-name').html(data[0].ClientName);.
Hi and thanks for your time solving my issue. I have used the code you kindly provided and as you say it works. If I then use the following data[1],data[2],data[3] and data[4] do not. This is how I have it in the scripts. $('.clientname').html(data[0].ClientName); $('.childbox').html(data[1].ClientImageName); $('.clientndisplaytext').html(data[2].DisplayText); $('.clientndisplaytime').html(data[3].RoomFromDateTime); $('.clientndisplaytime').html(data[4].RoomToDateTime);. I have all the DIV's in place.
In Chrome debugging I get the following: Uncaught TypeError: Cannot read property 'ClientImageName' of undefined. Any ideas?
The Uncaught TypeError occurs because data[1] is not set. All values are inside data[0], see my edit above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.