MAUAHAHAH! My ajax test!!

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

MAUAHAHAH! My ajax test!!

Post by Isaac »

http://testing.isaacg.net/ajax_test/ajaxtest.html
I'm going to be able to do tons with this, now that I understand it!!!
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6539
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: MAUAHAHAH! My ajax test!!

Post by Jeff250 »

Yup, it's nifty. You can use $.post() wrapper instead of $.ajax() to make it even simpler.

edit: Ack, you're also closing your <div> tag with a </button> tag. :P
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: MAUAHAHAH! My ajax test!!

Post by Isaac »

Hahaha. Fixed. Thanks!

here's the python source, for the record:

Version 1 of code. Simple mode. This is all you need to get the example above:

Code: Select all

#!/usr/bin/python
import cgi,cgitb
form = cgi.FieldStorage()
mystr=form.getvalue("try")
print  """ Content-Type: text/html\n\n
<b>This is Python responding! Hello!!!</b> Here's what you sent:
<i>"%s"</i><br/><hr></hr>
""" % mystr

Here's the python code I'm running, with an if statement that responds differently if you try to access the cgi file directly.

Code: Select all

#!/usr/bin/python
import cgi,cgitb
form = cgi.FieldStorage()
mystr=form.getvalue("try")
print "Content-Type: text/html\n\n"
display ="pppppttt!!!<i> cheater</i>"
if mystr<>None:
	display = """<b>This is Python responding! Hello!!!</b> Here's what you sent:
<i>"%s"</i>
<br/>
<hr></hr>
""" % mystr
print display
HTML:

Code: Select all

<html>
<head><title>test</title></head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
<script>
$(document).ready(function(){
	$("#go").click(function(){
		$.ajax ({type:"post",data:"try=This text is from jQuery",url:"ajaxtest.cgi",
			success:function(result){ $("#mydiv").append(result);}
			});
		});

});

</script> 
<body>

<button id="go">Send string "This text is from jQuery"</button>
<div id="mydiv"></div>

</body>
</html>
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Foil
DBB Material Defender
DBB Material Defender
Posts: 4900
Joined: Tue Nov 23, 2004 3:31 pm
Location: Denver, Colorado, USA
Contact:

Re: MAUAHAHAH! My ajax test!!

Post by Foil »

I haven't done web development in years, but I remember AJAX seemed like a pretty efficient way to do things on-demand for users.
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6539
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: MAUAHAHAH! My ajax test!!

Post by Jeff250 »

It's also time to start declaring a doctype and making sure your code validates at validator.w3.org. It looks like you're using HTML5-isms, so the HTML5 doctype would be a good choice. You don't want to spend an hour debugging your Javascript just to find out it's not working because the browser isn't parsing your HTML correctly. Older browsers are the most inconsistent about parsing invalid HTML--invalid HTML might work fine on one browser, say the one you use by default, but when you go and test it on another browser, it might mysteriously barf. Another reason to declare a doctype is that older versions of IE are even worse than ordinary at rendering HTML/CSS unless you declare a doctype.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: MAUAHAHAH! My ajax test!!

Post by Isaac »

I will do this and read more on it too. This concept is new to me.


Also, should I allow a single webpage to call many different python scripts, streaming data back and forth with ajax, or should I have one jQuery ajax function talking to a single cgi file, so there's only one stream per user? I can imagine having different cgi files doing different jobs, all pushing data to the browser. That would make adding features easier, because each jQuery program could have its own Python script. On the other hand, I could have a master cgi file that deals specifically with all streaming data and imports all my py files.

Maybe it doesn't matter, because it might be streaming the same amount of data anyway.
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6539
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: MAUAHAHAH! My ajax test!!

Post by Jeff250 »

Whatever seems the most elegant. If one approach turns out uglier than you expected, you can always switch to the other more beautiful.

On a technical note, since you seemed to imply otherwise, if you go with the one master cgi script route, you can still have multiple jQuery functions (or multiple calls to the same function) querying it simultaneously. In other words, multiple scripts does not get you any additional parallelism.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: MAUAHAHAH! My ajax test!!

Post by Isaac »

Yeah, I think. On the other hand, even if I had a single jQuery function that relayed lists to a single cgi file, it would still behave as many jQuery functions, because each user would run a different instance of that function.

Another thing.
http://testing.isaacg.net/commentboxa/comment.html

I think this comment box is easier to work with if it's python script isn't part of a massive collection of scripts.
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6459
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Re: MAUAHAHAH! My ajax test!!

Post by fliptw »

There is always maintainability to consider

ponder looking into lxml, it has a html tree builder.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7737
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: MAUAHAHAH! My ajax test!!

Post by Isaac »

fliptw wrote:There is always maintainability to consider

ponder looking into lxml, it has a html tree builder.
Hey!! I looked it up and that's PYTHON!!!! sweeet! Yeah, I'll check it out.
for my notes:
http://lxml.de/intro.html
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
Post Reply