tags:

views:

35

answers:

2

I'm writing this to a text file which is then read in by my browser. How do I assign the variable "i[0]" to the to the "submit" button so it is passed to my edit_python script?

k.write('<table>')
    for i in row:
        k.write('<tr>')
        k.write('<td><form action="edit_python" method="post" name="edit_python"><input type="submit" value="Submit" /><></FORM></td><td>'+i[0]+'</td><td>'+i[1]+'</td>')
        k.write('</tr>')
k.write('</table>')
+2  A: 

Put it in a hidden input element. In this case, it will be assigned to the ivalue post request variable.

k.write('<table>')
    for i in row:
        k.write('<tr>')
        k.write('<td><form action="edit_python" method="post" name="edit_python"><input type="hidden" name="ivalue" value="' + i[0] + '"/><input type="submit" value="Submit" /></form></td><td>'+i[0]+'</td><td>'+i[1]+'</td>')
        k.write('</tr>')
k.write('</table>')

I cleaned up your HTML a bit too, what was this about before the end form tag?

 <></FORM>

If you want a literal <> in your page, use &lt;&gt;. Also, the case of the tags should match for HTML, and should be lowercase for XHTML (which you appear to be using).

EDIT:

I'm writing this to a text file which is then read in by my browser

Also, you do realise the browser won't interpret Python? You need a web server set up correctly to do that.

Macha
Sorry I jusr forgot to remove that from my code, Python wont be present in my text file only html :). Thanks for the answer
Harpal
+2  A: 

You want to pass the i[0] attribute to your edit_python script? I think you're looking for a hidden field. So you would modify your script to write out:

Also when using Python variables in strings, I recommend not using the method you did. Check out Python string formatting for some great examples.

I also modified your write method to use dictionary passing for the attributes, which works well when you are calling the same variable multiple times in a stirng.

k.write('<table>')
for i in row:
    k.write('<tr>')
    k.write('<td><form action="edit_python" method="post" name="edit_python">
       <input type="hidden" name="some_attr" value="%(some_attr)s" />
       <input type="submit" value="Submit" /></form>
        </td><td>%(some_attr)s</td><td>%(some_other_attr)s</td>' 
        % {"some_attr": i[0], "some_other_attr": i[1]})

    k.write('</tr>')
k.write('</table>')
Bartek