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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Location
    Kentucky
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX all on one page

    I have programmed a supersimple chat with the PHP and HTML all on a single page as shown below:

    Code:
    <?
    
    if(isset($_REQUEST['comment'])) {   // if there is a comment
    	$name=$_REQUEST['name'];
    	$comment=$_REQUEST['comment'];
    	$fh=fopen("comments.htm","r");
    	$c=fread($fh,3333);
    	fclose($fh);
    	$fh=fopen("comments.htm","w");
    	$msg="<h4>$name----$comment</h4>\n";
    	$c .= $msg;
    	fwrite($fh,$c);
    	fclose($fh);
    }else {  // no comment,  set the name to blank
    	$name="";
    }
    ?>
    <html>
    <body>
    	<iframe id="if" src="comments.htm#end" height=233px width=444px scrolling=no frameborder=0></iframe>
    	<hr>
    	<form action="chat0.php">
    		<table>
    			<tr><td>Name</td><td>Comment</td></tr>
    			<tr><td><input type = "text" name="name" size=12 value= <? echo $name; ?> > ---- </td> <td><input type = "text" name="comment" size=33></td></tr>
    		</table>
    
    		<input type=submit value="Add comment">
    	</form>
    <input type="button" value="refresh" onClick="document.getElementById('if').contentWindow.location.reload()">
    </body>
    </html>
    ---------------
    Is it possible to use AJAX and still keep it on a single page?

    Thanks

  • #2
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    You mean without having to create another server-side page to recruit to the AJAX function?
    Yes, you can that but you'll need to have your page written in somekind of server-side language.
    You can do that by add in the query when you try to recruit the page something like that: ?AJAX_Page=true.
    Then you check:
    Code:
    if($_GET["AJAX_Page"] == true)
    and then if it is true you show the page you want to view for the AJAX function.

    Sorry if the PHP function isn't right.. I'm an ASP/.NET programmer.


  •  

    Posting Permissions

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