javascript - Setting PHP variables from AJAX/Json -
at moment have code:
index.php
var refreshid = setinterval(function() { $.ajax({ url: ("http://www.example.co.uk/erc/user_data.php?imei="+incallingnum), datatype: "json", //the return type data jsonn success: function(data){ // <--- (data) in json format $('#right-side').html(data.rightside); $('#caller').html(data.caller); $('.location').html(data.location); $('.battery-level').html(data.battery); //parse json data } }); });
user_data.php
$profile = array(); $profile['rightside'] = $rightside; $profile['caller'] = $caller; $profile['nextofkin'] = $nextofkin; $profile['location'] = $location; $profile['battery'] = $battery; echo json_encode($profile);
this works fine add information div tags, need take php variable user_data.php file , use in index.php file. possible send/capture php variables in way?
e.g. in user_data.php have variable $test , want use $test variable in index.php
thanks help
there many ways can this, , easiest (and transparent way of doing so) setting session cookie. small file sits on client's computer, , readable sites on sub-domain (x.mydomain.com.), files in same folder file set it. can in php doing following things:
on every page want set, get, or otherwise check variables... use code
session_start(); // put @ top of page, below <? or before check variables.
on page want set variables... use code
$_session['variable'] = "data"; session_write_close(); // use after done setting session data, save before page execution finished. habit in to, it's kind of when fclose file instead of waiting script it.
on page want variables.. use code
$test = $_session['variable'];
you can use $_session array store variables want seen "global" on site. forums use store user id , session hash, later authentication on passwords. other sites use session cookies limit user activity within given timeframe.
--
there way can this, have link page generates value of $test send request index.php (for example, if user clicks link format link like:
index.php?test=value
then on index.php do:
$test = $_get{'test'];
this method users may not have cookie support, or may have cookies disabled; obvious, , users can change value of cookie (which can have unseen results.)
Comments
Post a Comment