How To Use JQuery In Python Without Importing New Modules

In this post I am going to show you how you can add jquery to a Python script without adding any new modules.

You just need this:

#jquery
print "<script src='https://code.jquery.com/jquery-3.5.0.js'></script>"

#basic alert box:
print "<script>"
print "$(document).ready(function(){"
print "    // Get value on button click and show alert"
print "    $('#submit').click(function(){"
print "        alert('hello');"
print "    });"
print "});"
print "</script>"

Click to see it in action

The first part is just a call to the jquery library which is online, and the script is sort of like javascript, but it’s more recent.

Any html document script, like jquery needs to be put in the “header” tags within the “script” tag.

The pseudo code can be interpreted as “after the document is loaded on the user’s screen, and the element with the ‘id’ of ‘submit’ is clicked, then show the alert box”.

Here’s all the code in action:

#!/usr/bin/python
print "Content-type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>Enter Questions For Quiz</title>"
#jquery
print "<script src='https://code.jquery.com/jquery-3.5.0.js'></script>"

#basic alert box:
print "<script>"
print "$(document).ready(function(){"
print "    // Get value on button click and show alert"
print "    $('#submit').click(function(){"
print "        var str = $('#action').val();"
print "        alert('hello');"
print "    });"
print "});"
print "</script>"

print "</head>"
print "<body>"

#GLOBAL VARIABLES
USERNAME   = 'admin'
PASSWORD = 'pwd'
DB = 'online-quiz'

import MySQLdb

def init(userid):
	#DEBUG:	
	#print("show form" + str(userid) + "<br>")

	print "<form action = 'jquery.py' method = 'post'>"

	print "<table width='500px' border='0px' bgcolor=lightgreen>"
	print "<tr>"
	print "<td><strong>User:</strong></td>"        
	print "<td><input type = 'text' style='background-color:grey' readonly name = 'userid' value='" + str(userid) + "'></td>"
	print "</tr>"

	print "<tr>"
	print "<td><strong>Select Quiz From List:</strong></td>"        

	print "<td>"
	
	conn = MySQLdb.connect('localhost', USERNAME,PASSWORD,DB)
	sql="SELECT qui_id,qui_quiz FROM tblQuizzes WHERE qui_user_id = " + str(userid)

	#many errors occur because you called a variable that doesn't exist (ex. you call "sql2", but it doesn't exist anywhere)

	cursor = conn.cursor()
	cursor.execute(sql)

	#Add A 'Select Quiz' (Blank Row) At The Top Of The Dropdown Box 
	print "<select name='qui_id' id='qui_id' class='ddquiz'>"
	print "<option value=''><i>(Select Quiz)</i></option>"

	for row in cursor:
		qui_id = row[0]
		qui_quiz = row[1]

		print "<option value=" + str(qui_id) + ">" + str(qui_quiz) + "</option>"

	
	print "</select>"
	print "</td>"
	print "</tr>"
	
	print "<tr>"
	print "<td><input id='submit' type = 'submit' value = 'Submit' /></td>"
	print "</tr>"

	print "</table>"

	print "</form>"
	
#start here:
init(1)


print "</body>"
print "</html>"

Click to see it in action

Facebooktwitterredditpinterestlinkedinmail
Tags: , , , ,
Previous Post

How To Read A CSV Into A HTML Table With Python

Next Post

How To Make A Select Drop Down List In Python