Page 1 of 1

how to pass data from javascript to php

Posted: Wed Apr 13, 2011 2:22 am
by cargao
I have a site with Javascript code, which will collect some data and make a structured table from it, then send it to the server to store in MySql database. The problem here is, how to send data to the web server using javascript? It must happen on load event. I googled some answers how to pass data with javascript submit button, but I have no form and no button on my site. There were also answers how to pass javascript data between pages, but I want to sent directly to the server. Maybe I just couldn't find the right example in the net, best would be to give a hint what should be done on javascript side and on php side.

Re: how to pass data from javascript to php

Posted: Wed Apr 13, 2011 10:46 am
by waytoecommerce
you can user ajax to send you php data to server.

Please look at the given example :

Code: Select all

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(s){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var status = s;
	ajaxRequest.open("GET", "status.php?pfid=" + "<?=$_REQUEST['pfid']?>&status=" + status, true);
	ajaxRequest.send(null); 
}

//-->
</script>
my be this help you...