Jump to content

Basic AJAX coding


jhecht

Recommended Posts

I dont have a problem about javascript(at the moment), im just writting down some functions i use as bases when i use an AJAX program so that others can use it as well and aren't so lost when all of us on here talk about AJAX coding and so on

Javascript file<script type="text/javascript"><!--function createRequestObject() {	var ro;	var browser = navigator.appName;	if(browser == "Microsoft Internet Explorer"){		ro = new ActiveXObject("Microsoft.XMLHTTP");	}else{		ro = new XMLHttpRequest();	}	return ro;}var updateId = createRequestObject();function ajax(argument) {   updateId.open('GET', "show.php?argument="+argument);   updateId.onreadystatechange = handleConts;   updateId.send('');}function handleConts() {	if(updateId.readyState == 4){	 	 document.getElementById("div_id").innerHTML=updateid.responseText;	 	}}//--></script>

show.php<?phpif(isset($_GET['argument'])){//Same name as the thing after the ? in the javascript, right? $arg = (int)$_GET['argument']; $sql = "SELECT * FROM table WHERE user_id=$arg"; $ans= mysql_query($sql) or die(mysql_error()); while($res = mysql_fetch_array($ans)){ echo " Welcome ."$res['user_name']." ! <br/>\n"; } }else{echo "Argument Variable not set.<br/>\n";}?>This echoes out a username from a database given by a user_id passed through the argument variable in show.php. Added by request.

By changing the script around a little bit you can suit this to do anything you need pretty much.

Link to comment
Share on other sites

I dont have a problem about javascript(at the moment), im just writting down some functions i use as bases when i use an AJAX program so that others can use it as well and aren't so lost when all of us on here talk about AJAX coding and so on
<script type="text/javascript"><!--function createRequestObject() {	var ro;	var browser = navigator.appName;	if(browser == "Microsoft Internet Explorer"){		ro = new ActiveXObject("Microsoft.XMLHTTP");	}else{		ro = new XMLHttpRequest();	}	return ro;}var updateId = createRequestObject();function ajax(argument) {   updateId.open('GET', "show.php?argument="+argument);   updateId.onreadystatechange = handleConts;   updateId.send('');}function handleConts() {	if(updateId.readyState == 4){	 	 document.getElementById("div_id").innerHTML=updateid.responseText;	 	}}//--></script>

By changing the script around a little bit you can suit this to do anything you need pretty much.

Very nice, now you should add the page show.php with maybe a switch or somethin, to show how its gettin the data and then returning it.
Link to comment
Share on other sites

Can't you use Ternary Operators to shorten it?like:var request = (window.ActiveXObject)? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();or:request.onreadystatechange = function() {var auth = (request.readyState==4 && request.status==200)? request.responseText : false;?

Link to comment
Share on other sites

You can, but really, what's the point? There's no limit to how much code you can put on the page, and it won't increase the loading time by much, if at all, since you're still going through the same process tree. :)And is that you, Outkast?

Link to comment
Share on other sites

Well, as far as I know, the only hosted forum software that gives you a limit on how much code you can stick in is Invisionfree, and almost no one here codes for. :)

Link to comment
Share on other sites

  • 3 months later...
You showed the code to use but how do we use it? I'm trying to find someone who has code that works or a tutorial that's easy to understand and so far I haven't found one. :)
Maybe if you had a specific question, or if you had some code that you haven't been able to implement, you could start a new post and someone here can help clear up any confusions you might have.
Link to comment
Share on other sites

Not to be mean, but if you know javascript at all, in any way, then you should know how to use this; If you don't, then whats the point in you reading and/or replying? If you don't know javascript, then don't complain to us that you can't figure out how to use it. Figure out basic javascript syntax(because this is quite honestly a very BASIC example), and THEN complain that something doesn't work.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...