0

I have studied some AJAX, and tried to understand it.

The AJAX works; i just want to write a right php document too. With the AJAX, I load the php file write.php. Here is what is in write.php:

<?php
$NAME = 'data.txt';
$HANDLE = fopen($NAME, 'w') or die ('CANT OPEN FILE');
fwrite($HANDLE,'1');
fclose($HANDLE);
?>

But how to choose what to write, instead of '1', with JavaScript? Like Having a variable in JavaScript with the content, and loading that content into the php file?:?:

Thanks!

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by danishanila
0

Hi,

here's a simple demo that will help you to accomplish your task.

this demo comes with 2 documents:
test.php

<?php

	echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://w3.org/TR/xhtm1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>	
		<meta http-equiv="X-UA-Compatible" content="EmulateIE6" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Cache-Control" content="no-cache" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<title>Javascript to PHP Example:: by essential</title> 
		<script type="text/javascript">
		// <![CDATA[
		var update = ( function() { 
			if ( { 4: 4, complete : 4 }[ this.readyState ] ) { // AJAX callback function
				if ( this.status === 200 ) { // Let them know that the data has been written on the .txt file >>>
					alert( "Your data has been saved!" );
					
					if ( this.responseText ) {
						var data = String( this.responseText ).split(/\n/);
						var len = data.length;
						var container = document.getElementById( "loadInfo" ) || document.all.loadInfo;
						var txt = "";
							for ( var x = 0; x < len; ++x ) {
								if ( data[ x ] ) {
									txt += data[ x ] + "<br />";
									continue;
								}
							
							} container.innerHTML = '<h3>AJAX Generated Content</h3><br />' + txt;
							// Do nothing: you can provide more lines below if you still need to do something asside from writing to the .txt file and reading its data
						return;
					}
				}
			}
		} );
		var createRequest = ( function() { // Building request object >>>
		var xhr = 0;
			try {
				try {
					if ( "XMLHttpRequest" in window ) {
						xhr = new XMLHttpRequest();
					} else { // Handles all major browser, exept IE >>>
						var PID = [
							"MSXML2.XMLHTTP.6.0",
							"MSXML2.XMLHTTP.5.0",
							"MSXML2.XMLHTTP.4.0",
							"MSXML2.XMLHTTP.3.0",
							"MSXML2.XMLHTTP.2.0",
							"Microsoft.XMLHTTP" ];
						var len = PID.length;
						for ( var x = 0; x < len; ++x ) { // Instance that will invoke IE browser to respond on the client request >>>
							if (( xhr = new ActiveXObject( PID[ x ] ))) {
								break;
							}
						}
					}
				} catch( e1 ) { // for some other browser's that do not understand the two types of calls above
					xhr = window.createRequest();
				}
			} catch( e ) { // invocation failed [EXIT FUNCTION]
				xhr = 0;
			} return xhr;
		} );
		
var senData = function( arg ) {
var req = createRequest();
	if ( req ) { 
		var obj = ( function( IDS ) {
			return IDS = document.getElementById( IDS ) || (( typeof IDS === "string" ) ? document.all[ IDS ] : document.all.IDS );
		} );
		var uname = obj( "names" ).value || "Created Name in varible ( uname )";
		var pword = obj( "password" ).value || "GeneratedPassword";
		var url = arg + "?names=" + uname + "&password=" + pword;
		(( "overrideMimeType" in req ) ? req.overrideMimeType("text/xml") : null );
		req.onreadystatechange = update;
		req.open( "GET", url, 1 );
		(( req.setRequestHeader ) ? req.setRequestHeader("Content-type", "application/www-x-form-urlencoded; UTF-8") : req );
		req.send( null );
	} 
	
	
	/* prevent the form on getting submitted when we invoke AJAX call from the ONSUBMIT handler */ return false;
};

		// ]]>
		</script>
		<style type="text/css" media="all">
		/* <![CDATA[ */
		html, body {
			font: normal normal normal 95%/1.5 Tahoma, Verdana, Arial, sans-serif;
			color: #555;
			margin: 0;
			padding: 0;
			border: none;
			text-align: center;
			width: auto;
			height: auto;
			vertical-align: baseline;
			}
		div, form {
			padding: 0;
			margin: 0;
			border: none;
			}
		form[id] {
			margin: 0 auto;
			width: 95%;
			margin-top: 1.5%;
			text-align: left;
			}
		
		acronym {
			color: #F80;
			font-weight: bold;
			}
		p {
			color: #006;
			font-size: 90%; }
		
		
		/* ]]> */
		</style>
	</head>
	<body>
	<div style="text-align: left; padding-left: 1.5em;">
		<p>In this example i will demonstrate on how you would passed those data into your <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> enabled page using the help of <acronym title="Asynchronous Javascript and XML">AJAX</acronym>.</p>
		<h3>Guidelines</h3>
		<ol>
			<li>All files on this demo must be saved on the same directory inside your document root.</li>
			<li>Make sure that you have the permission to write files in your target folder.</li>
			<li>This code has been tested on the following lists of browser:<br /><br />
				<ul style="font-size: 80%; font-weight: bold; color: #778899;">
					<li>Microsoft Internet Explorer 7+</li>
					<li>Mozilla Firefox 1+</li>
					<li>Opera 9+</li>
					<li>Google Chrome 4</li>
					<li>Apple Safari 4</li>
				</ul>
			</li>
		</ol>
		
	</div>
		<form id="formContent" method="get" action="./test.php" onsubmit="return senData('data.php')">
			<div id="content" style="margin-top: 1.200em; border: 2px solid #000; height: 30px; width: auto; background-color: #707070; line-height: 30px; color: #f1f1f1; padding-right: 2em; text-align: right;">
				<label for="names">username: <input type="text" size="30" id="names" name="names" maxlength="30" value="essential" /></label>&nbsp;&nbsp;
				<label for="password">password: <input type="password" size="30" id="password" name="password" maxlength="30" value="123456" /></label> 
				<input type="submit" value="save" id="smb" name="smb" />
			</div>
		<div id="loadInfo" style="margin-top: 1em; line-height: 1.6; padding-left: 1em;">
		<?php
		$NAME = 'data.txt';
		if ( file_exists( $NAME ) ) { ?>
		<h3>PHP Generated Content</h3>
		<?php
			$HANDLE = fopen( $NAME, 'r');
			while (!feof( $HANDLE )) {
				echo fgets( $HANDLE, 1000) . "<br />";
			} fclose( $HANDLE );
		} 
		?>
		</div>
		</form>
	</body>
</html>

data.php

<?php
$username = $_GET['names'];
$password = $_GET['password'];
$NAME = 'data.txt';
if (isset( $password ) || isset( $username )) {

	

$info = array( '<b>username:</b> '.$username, '<b>password:</b> '.$password );

$HANDLE = fopen( $NAME, 'wb' ) or die( "Can't open $NAME" );
	foreach( $info As $data ) {
		$writeData = fwrite( $HANDLE, "$data\r\n" );
	} fclose( $HANDLE );
	if ( file_exists( $NAME ) ) {
		$HANDLE = fopen( $NAME, 'r');
		while (!feof( $HANDLE )) {
			echo fgets( $HANDLE, 1000);
		}
	} fclose( $HANDLE );
}
?>

hope it does help...
essential

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.