Page 1 of 1

Button click opens new webpage

Posted: Fri Mar 25, 2016 3:48 pm
by DanLap
I currently have a python script that when clicking a button on our webpage causes the python script to move a motor. However, each time the button is clicked it opens a new page rather than just refreshing the page. Each time I have to press the back button to go to our main page. I have tried a bunch of things but would like to have the page just refresh when clicking the button. Any suggestions? My python code is below.

Code: Select all

#! /usr/bin/env python
import cgi
import serial

ser = serial.Serial('/dev/ttyACM0',9600)
ser.write('z050\n') #this is what moves motor (connected to arduino)
ser.close()

print("Content-type: text/html\n\n"
print<html>These words show up on the new page once button is clicked></html>"
 

Re: Button click opens new webpage

Posted: Tue Mar 29, 2016 5:22 pm
by TheConsciousness
Are you sure that is the only code you're using?
Also, your print statements don't have the write about of quotation marks or parenthesis.

Re: Button click opens new webpage

Posted: Wed Mar 30, 2016 10:44 am
by scotty101
Can you share the HTML of the page that has the button on it.

As the previous poster mentioned, the python code you posted will not work.

You might want to consider using python-flask rather than CGI.

Re: Button click opens new webpage

Posted: Wed Mar 30, 2016 4:46 pm
by DanLap
I had just put those print statements as an example, fixed below

Code: Select all

#! /usr/bin/env python
import cgi
import serial

ser = serial.Serial('/dev/ttyACM0',9600)
ser.write('z050\n') #this is what moves motor (connected to arduino)
ser.close()

print("Content-type: text/html\n\n"
print"<html>These words show up on the new page once button is clicked></html>"
Our HTML is just a simple page that has buttons to move the motor up/down/left/right. When the button is clicked the motor moves correctly but the webrowser goes to a page showing the html in the python script. Here is a sample of where we have in HTML for the buttons:

Code: Select all


#HTML buttons with up/down/left/right arrows
    <table> 
		<tr><td></td><td><a href="/cgi-bin/moveup.py"><button> &#9650 </button><br></td></tr><td></td>
		<tr>
			<td><a href="/cgi-bin/moveleft.py"><button>&#9668</button><br></td>
			<td></td>
			<td><a href="/cgi-bin/moveright.py"><button>&#9658</button><br></td>
		</tr>
		<tr><td></td><td><a href="/cgi-bin/movedown.py"><button>&#9660</button><br></td></tr><td></td>
    </table>

Re: Button click opens new webpage

Posted: Wed Mar 30, 2016 4:49 pm
by DanLap
I have been thinking about using flask or even webiopi but wanted to see if I could get everything to work using just the CGI and HMTL. Basically just want to see if I could get the button to refresh the html page or just open the page in a second tab rather than going right to the html in the python code. If we don't put the line of HTML, it just goes to a blank page when the button is clicked.

Is this even possible? I am not sure if the fix would be with python or the index.html page

Re: Button click opens new webpage

Posted: Thu Mar 31, 2016 8:27 am
by scotty101
Problem is with the python script.

When you press the button it navigates to the new page. You have have it redirect back to your index page once the motor has been moved.
http://stackoverflow.com/questions/6122 ... cgi-python

Again. This is much simpler with python-flask.

Re: Button click opens new webpage

Posted: Fri Apr 01, 2016 11:35 am
by mysen
This is what you code needs to look like...

Code: Select all

#! /usr/bin/env python
import cgi, cgitb
import serial

ser = serial.Serial('/dev/ttyACM0',9600)
ser.write('z050\n') #this is what moves motor (connected to arduino)
ser.close()

print "Content-type: text/html\n\n"
print "<head>"
print "<meta http-equiv="refresh" content="0; url=http://button-url-or-ip" />"
print "</head>"
content = how many seconds to wait until going to the specified URL. I think in your case 0 is fine. and the url should just be where to you want it to redirect to, in your case back to the url your just came from.

Hope this helps

Re: Button click opens new webpage

Posted: Fri Apr 01, 2016 1:36 pm
by elParaguayo
This line

Code: Select all

print "<meta http-equiv="refresh" content="0; url=http://button-url-or-ip" />"
is going to give you a SyntaxError because of you're using the same quotation marks to start your string and also inside the string.

Try:

Code: Select all

print """<meta http-equiv="refresh" content="0; url=http://button-url-or-ip" />"""
or

Code: Select all

print '<meta http-equiv="refresh" content="0; url=http://button-url-or-ip" />'
I'd also second the comments about Flask, it's very easy to use and cgi always seemed overly complicated to me. But go with what you know!

Re: Button click opens new webpage

Posted: Fri Apr 01, 2016 5:16 pm
by DanLap
Thank you so much!! This worked perfectly for what I was looking for using just the cgi. Will definitely be looking into flask very soon.