Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    accessing php object variable

    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
    PHP Code:
    //select data from table 
            
    $sql ="select * from test";
            
    $result mysqli_query($link,$sql);
            if (!
    $result){
                
    $output "cannot select table";
                include 
    'output.html.php';
                exit();
            }
            
    //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;
                }    
                for(
    $i=0;$i<count($field_name);$i++){
                    echo 
    "check fieldname";
                    echo 
    $field_name[$i];
                    echo 
    "<br>";
                }
                
    //fetch data is true
                
    while($obj=mysqli_fetch_object($result)){
                    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.
    Last edited by Fou-Lu; 06-25-2010 at 12:49 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Feb 2010
    Posts
    104
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Ajax code!

    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.
    Code:
    ajaxRequest_JD({
    			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
    		})
    Code:
    function return2b(result){
    //your code here on return.
    }
    Code:
    /*------------------------------------------------------------------
    This function creates an ajax request
    ------------------------------------------------------------------*/
    function ajaxRequest_JD(params)
    {
    	/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    	Avalible params --------------------------------------
    	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    	
    	multiple inputs are seperated by a '|'.
    	
    	REQUIRED
    	- script (the path to the required script)
    	
    	OPTIONAL
    	- 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.");
    		return;
    	}
    	
    	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);
    	inputName=trim_JD(inputName);
    	type=trim_JD(type);
    	
    	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.");
    		return;
    	}
    	
    	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 = "?";
    	}else{
    		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;
    	}else{
    		var synchronous = true;	
    	}
    
    
    	var xmlhttp;
    	xmlhttp=GetXmlHttpObject();
    	
    	if(type == "GET"){
    		xmlhttp.open(type, script+readyData, synchronous);
    		xmlhttp.send(null)
    	}else{
    		xmlhttp.open(type, script, synchronous);
    		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		xmlhttp.setRequestHeader("Content-length", readyData.length);
    		xmlhttp.setRequestHeader("Connection", "close");
    		xmlhttp.send(readyData);
    	}
    		
    
    	if(synchronous == true){
    		xmlhttp.onreadystatechange = function(){
    			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    				action(xmlhttp.responseText);
    			}
    		}
    	}else{
    		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){
    		i++;
    	}
    	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;
    }

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you semiskin, will play around with it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •