Results 1 to 4 of 4

Thread: Alternative to html element onload

Hybrid View

  1. #1
    Join Date
    Apr 2011

    Question Alternative to html element onload

    I have a text box that gets its name, id, and value from a SQL query.
    PHP Code:
    echo "<input type='text' id=" $row['ID'] . "field" $counter " name=" $row['ID'] . "field" $counter " value='" $response['I' $counter] . "' style='width:12px' onblur='upperCase(this.id,this.value,correct" $counter ".value);' onload='upperCase(this.id,this.value,correct" $counter ".value);'>" 
    When a response is typed into the box and the user advances to the next, the function upperCase(id,answer,correct) capitalizes the user's input and then checks the input against the correct response (stored in a hidden input whose value is pulled from a SQL query). If the response is correct, the box's background color changes:
    function upperCase(id,answer,correct){
    		var x=document.getElementById(id).value
    		if(answer.toUpperCase() == correct.toUpperCase()){
    			document.getElementById(id).style.background = "#00FF00" //green
    		else {
    			document.getElementById(id).style.background = "#FF0000" //red
    My problem is that when the page loads with the values from the SQL query, I want all of the text boxes to run their upperCase function to change their respective background colors and I cannot figure out how to do this when the correct responses and initial text box values are set from the SQL query.

    I thought I was genius when I put the onload event in there (which obviously doesnt work with an input element). I need an alternative solution. Please help!

  2. #2
    Join Date
    Apr 2011
    This might help to see the text box code more easily:
    <input type='text' 
    	   id=" . $row['ID'] . "field" . $counter . " 
    	   name=" . $row['ID'] . "field" . $counter . " 
    	   value='" . $response['I' . $counter] . "' 
    	   onblur='upperCase(this.id,this.value,correct" . $counter . ".value);' 
    	   onchange='upperCase(this.id,this.value,correct" . $counter . ".value);'>

  3. #3
    Join Date
    Mar 2010

    A couple quick things. Technically, this should not work
    onblur='upperCase(this.id,this.value,correct" . $counter . ".value);'
    The reason is that you call correctX.value, but in the system it looks like you are calling variable, not selecting a value form the input element. Instead, you should either pass the id and select the element with document.getelementbyid, or you just do
    onblur='upperCase(this.id,this.value,document.getElementById('correct" . $counter . "').value);'
    Take note of the apostrophes around the id name, otherwise errors will occur. Other than that your code looks to work fine.

    Now, each element does indead have an onload event, however it can not be called from the element itself. Instead, you must catch the onload event by calling the windows onload. This will allow you to run a function on any element when the document loads. This will require you to change your code around, because you will need to loop through your sql result twice. Once to fill the window.onload event, and another to create the inputs. An alternative method, which I provided below, is to simply open a script tag and make the function call below each input. This is not the way I would do it, I would use the onload event, however this method will not require you to change your code, albeit a bit sloppy.

    I modified your code so that I could test to make sure it was working.
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    function upperCase(id,answer,correct){
    		var x=document.getElementById(id).value
    		if(answer.toUpperCase() == correct.toUpperCase()){
    			document.getElementById(id).style.background = "#00FF00" //green
    		else {
    			document.getElementById(id).style.background = "#FF0000" //red
    window.onload = function(){
    	// here is where you run a php loop to run the function for each of the elements on the page
    	//<?php foreach($row as $key => $value){ echo "upperCase('field".$key"',document.getElementById('field".$key."').value,document.getElementById('correct".$key."').value)"; } ?>
    <input type="text" id="correct1" value="testing" />
    <?php $counter = 1; echo "
    "; ?>
    <div onload='alert(1)'>This onload doesn't work.</div>
    On a final note, I am not sure what type of security you imagine for your answers, but if a user wanted to find the answer to your question they could simply view the source code, since all the answers are stored within the document. It may not be a big deal, but just thought I would point that out. If you wanted to make sure that the user was answering correctly without submitting the form and without having the answers available in plain site you could include an ajax event in your function that calls the server to capture the answer.

  4. #4
    Join Date
    Apr 2011
    Thanks for the help with the bad code, JavaScript is not my strong suit.

    Your solution worked perfectly! Thanks so much for the time and effort you put into it. I had no idea it was possible to use php within a JavaScript function. Obviously you can use it anywhere.

    As for the security of the answers - this page will only be seen by the user (teacher) who had set the correct answers in the first place. The students will not have access to this page. Thanks for the heads up though!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09402 seconds
  • Memory Usage 2,988KB
  • Queries Executed 13 (?)
More Information
Template Usage (31):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (5)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates