View Full Version : accessing php object variable

06-25-2010, 01:46 AM
hello, I currently wrote a php file that retrieve data from the mysql database.
and this data is a object type.

Here is the code that does that

//select data from table
$sql ="select * from test";
$result = mysqli_query($link,$sql);
if (!$result){
$output = "cannot select table";
include 'output.html.php';
//function showTable(){
//get number of fields in table
$num_fields = mysqli_num_fields($result);
$num_rows = mysqli_num_rows($result);
echo $num_fields."<br>";
echo $num_rows;
echo "<table border='1'>";
//fetch fields
$info = mysqli_fetch_fields($result);
foreach($info as $field){
echo "<th>".$field->name."</th>";
//store field names
$field_name[] = $field->name;
echo "check fieldname";
echo $field_name[$i];
echo "<br>";
//fetch data is true
echo "fetch data";
echo "<tr>";
echo "<td>".$obj->id."</td>";
echo "<td>".$obj->name."</td>";
echo "<td>".$obj->mydate."</td>";
echo "</tr>";
}//end of function showTable

my idea is to have a javascript for example
showTable( object)
taking an object parameter and display the data

in the html file there will be a button that has an onclick event
for example onclick=" showTable(object);

is this possible? if not is there any suggestion that I can do it ? I am a little new to both languages.

06-25-2010, 01:55 AM
PHP and javascript are not capable of talking together. AJAX can be used to access a php page in the background. You could write php that accepts get/post for a specific record, use AJAX to request, and handle the results. Make sure the results are well formed xml or html if you want to DOM it with js (otherwise, just text).
Search up for 'php ajax tutorial' for a good start. BTW, AJAX is a js related technology.

06-25-2010, 02:05 AM
Here is a little script I have written for AJAX if your interested. There are 3 parts below, the top part is an example AJAX call which you write into a javascript function, the middle is the function called once the call has completed and the bottom is the AJAX API. Hope you find it useful!

The script is the php file you wish to run, the inputs you want to send to the php script are seperated by "|" and you should set and action which is the function that will be called when the php script has finished.

script: "/_scripts/priceAvail.php",
inputName: "startDate|endDate|adults|children|babies|dogs|highChairs|cots|lodge",
inputData: startDate+"|"+endDate+"|"+adults+"|"+children+"|"+babies+"|"+dogs+"|"+highChairs+"|"+cots+"|"+1,
type: "POST",
action: return2b

function return2b(result){
//your code here on return.

This function creates an ajax request
function ajaxRequest_JD(params)
Avalible params --------------------------------------

multiple inputs are seperated by a '|'.

- script (the path to the required script)

- inputName (the variables used in the request)
- inputData (the data used in the request)
- type (GET or POST - GET is default)
- action (if it isn't set the executuing code will have to wait for a reply before continuing.)

if(params.script == undefined){
alert("JD System Design AJAX API Error! No script has been specified.");

var script = params.script || "";
var inputName = params.inputName || "";
var inputData = params.inputData || "";
var type = params.type || "GET";
var action = params.action || "false";

script = trim_JD(script);

var numName = getNumInputs_JD(inputName);
var numData = getNumInputs_JD(inputData);

if(numName != numData){
alert("JD System Design AJAX API Error! The number of variables doesn't match the number of inputs.");

var singleName = new Array;
var singleData = new Array;

for(var i=0; i<numName; i++){
singleName[i] = getRequiredParam_JD(inputName,i);
singleData[i] = getRequiredParam_JD(inputData,i);

if(type == "GET"){
var readyData = "?";
var readyData = "";

readyData = readyData + singleName[0] + "=" + singleData[0];
for(var i=1; i<numName; i++){
readyData = readyData + "&" + singleName[i] + "=" + singleData[i];

if(action == "false"){
var synchronous = false;
var synchronous = true;

var xmlhttp;

if(type == "GET"){
xmlhttp.open(type, script+readyData, synchronous);
xmlhttp.open(type, script, synchronous);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", readyData.length);
xmlhttp.setRequestHeader("Connection", "close");

if(synchronous == true){
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
return xmlhttp.responseText;


function GetXmlHttpObject()
if (window.XMLHttpRequest)
return new XMLHttpRequest();
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
return null;

This function will return the required property
function getRequiredParam_JD(input, num){
input = input + '';
var properties = new Array();
properties = input.split("|");
return properties[num];

This function will trim a string
function trim_JD(string){
return string.replace(/\s+/g,'');

This function will return the number of properties specified for
an option.
function getNumInputs_JD(input){
input = input + '';
var properties = new Array();
properties = input.split("|");

var i = 0;
while(properties[i] != undefined){
return i;

This function encodes variables ready for URL transfer
function urlencode_JD(str)
str = str.replace(/\+/g,String.fromCharCode(8));
str = escape(str);
str = str.replace(/\%20/g,"+");
str = str.replace(/\%08/g,"%2B");
return str;

06-27-2010, 06:55 PM
thank you semiskin, will play around with it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum