3

I have one test.html:

<form action="/cgi-bin/test.py" method="GET">
<input type="text" name="search">
<input type="submit" value="Submit">
</form>
<div id="res"></res>

And one python script test.py:

#....
print Result
#....

I want instead of printing result on the test.py, return it to the test.html and write it in the div with id res... How I can do that?

asked Oct 31, 2012 at 23:26
5
  • you are probably looking for json ... Commented Oct 31, 2012 at 23:26
  • @Joran Beasley What you mean? Can show example? Commented Oct 31, 2012 at 23:29
  • @JoranBeasley I think he needs a python template engine and not json. Commented Oct 31, 2012 at 23:29
  • Actually I have no clue to what you're asking. Could you give us more details on what is your intent exactly? Commented Oct 31, 2012 at 23:36
  • I made a script that its result give an embed player <embed ....></embed> and instead of printing it there I want to print in the html that called the script and show that embed player inside div with id res Commented Oct 31, 2012 at 23:38

3 Answers 3

4
<form id="myform">
<input id="my_text">
<button id="submit_btn">
</form>
<div id="result">
</div>
<script>
//you need to use jquery for this part
$("#submit_btn").click( function() {
 $("#result").load("/path/to/cgi?my_text="+$("#my_text").val())
})
</script>

something like that... (this is real hacky 1 minute post that is untested so it may need some work ...

your question really has nothing to do with python ... it is just web technology question ... it would be the same if it was a php or C# or whatever processing the form data

it has nothing to do with python because it doesnt matter how you are processing the form data ... this is just web technology it would work exactly the same with a perl script instead of a python script or with a C# instead of python or whatever ... you want to return some data back to a web page ... how you got the data is really not relevant

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<form action="someother.html" method="get">
<input id="my_text">
<input type="button" id="my_button" value="click me">
</form>
<div id="result">
</div>
<script>
$("#my_button").click(function(){
 $("#result").load("");
})
</script>
</body>
</html>

here is an example ... just paste it into a file.html and go to it and click the button ... its just loading its self load("") so it should duplicate the data thats on the page when you click it ...

answered Oct 31, 2012 at 23:43
Sign up to request clarification or add additional context in comments.

6 Comments

Why it has nothing to do with python? The script that I writing is using python language...
Ajax it's a tool. If you want it, just use it. This is a way to accomplish what you want.
you have a fundamental gap in understanding of web server client relationships .... I would study up on web design in general ...
@ Joran Beasley Ok I see... Can you explain me the jquery part? As I understand its says when I click the button it will load the script and will hold the value in the result or what?
As the partner response you up, Ajax make asynchronous requests to take or send data from X or to X. If X is Python, Perl, PHP, ASP .NET, NodeJS or whatever, it doesn't matter.
|
1

Well I understood your question in this way, maybe you are looking for other thing but i know a way that do exactly what are you looking for: Ajax.

Just make an ajax request to the cgi python and put the response into a div. Luckily, this is very easy nowadays.

If you know something about jQuery (a javascript framework) you should visit this link to getting started: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax

byeeee

answered Oct 31, 2012 at 23:31

6 Comments

Unfortunately I don't know ajax :S
It's not so hard, if you want we can stay on touch and i help you to learn JavaScript, almost the basics so you can start with Ajax... Today if you are a web developer, you need Javascript.
I know the basics of javascript... I just can't understand how I can use Ajax with Python... Its sound strange to me
Well, i told you, i understand your question in my own way so maybe ajax is not what are you looking for... but the result that you wish is perfectly workable for Ajax. But in other way, maybe you want a template engine!
like in my example :) but you put a link to jquery infos so he can learn(+1) ... but this question has nothing to do with python imho ...
|
0
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello World</h2>"
print "<p>Executing HTML code from python script</p>"
print "</body>"
print "</html>"

3 Comments

Write this code at the start of your test.py, please ensure that first line of your test.py file should be print "Content-type:text/html\r\n\r\n"
and run your test.py in your browser
Hi, try to avoid appending information for your answer in the comments section. Instead, edit your answer to add the information.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.