Results 1 to 10 of 10

Thread: [RESOLVED] dumb question, I'm sure anyone can answer it

Hybrid View

  1. #1
    Join Date
    Dec 2011

    resolved [RESOLVED] dumb question, I'm sure anyone can answer it

    I am new to javascript, and I am making a website with php but I can't get the javascript part to work the way I want it to.

    It is very simple, I am making a part of a website (where users are logged in) where users takes exams. In php I already have the query for the database and I already have extracted their exam number (1,2,3,4,ect..) on the variable $exam. I want javascript to check their current exam (which I provided with php on the code below) and do something depending on the exam number they clicked, if is below, above, or if is the exam number they are on.

    Now, remember I know nothing about javascript so I tried to write the code below based on my knowledge of php. This is what I wrote:

    function exval(){
    var examlinks = document.getElementByTagName('a');
    var examname = examlinks.getAttribute('name');
    if(examname.value > "<?php echo "$exam" ?>");
    alert("You are not on this exam yet!");
    else if(examname.value < "<?php echo "$exam" ?>");
    alert("You already took this exam");
    else if(examname.value == "<?php echo "$exam" ?>");
    alert("This is your exam"); // I want it to actually take them to their link in here
    return exval();

    the php part:

    if (($exam >= 1) && ($exam <= 10)) {
    echo "
    <a href=\"#\" name=\"1\" onclick=\"exval()\">Exam 1</a>
    <br />
    <a href=\"#\" name=\"2\" onclick=\"exval()\">Exam 2</a>
    <br />
    <a href=\"#\" name=\"3\" onclick=\"exval()\">Exam 3</a>

    Obviously the code above (javascript) doesn't do anything but that's because I don't know what I am doing, it's probably a dumb mistake but what I am trying to do is to give you an idea of what I am trying to accomplish.

  2. #2
    Join Date
    May 2003
    This is a sloppy reply as I was trying to get it done before I left for the day...but it should give you some ideas. I don't know if it works or not. Also, since I am on MS SQL here, I am using mssql php tags.

    Hope this helps.

    	$x = $_POST['exam'];
    	if (isset($x)) {
    		$exam = null;
    		$sql = "select exam_number from exam_table where user = '$user' and exam_number = $x ";
    		$result = mssql_query($sql);
    		while($row = mssql_fetch_array($sql)) 
    			$exam = $row['exam_number'];
    		if (isset($exam)) {
    			echo "<script type='text/javascript'>";
    			echo "alert('You have already taken exam number $exam');";
    			echo "</script>";
    <form name="form1" id="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    	<select name="select1" onchange="document.forms.form1.submit();">
    	<option value="-1" >Select One</option>
    	$sql = "select exam_number, exam_name from exam_table order by exam_number ";
    	$result = mssql_query($sql);
    	while($row = mssql_fetch_array($sql)) 
    		$exam_number = $row['exam_number'];
    		$exam_name = $row['exam_name'];
    		echo "<option value='$exam_number'>$exam_name</option>";

  3. #3
    Join Date
    Dec 2011
    Thanks for the reply BrainDonor!

    That's not quite what I am looking for, you see I already have all the php done, I'll make it that the link where the exam is at is going to query their exam number and if is not that level it will display different things but I want to add the javascript to stop them while still on the page and not let them go any further. If they disable javascript or something then it will take them to the exam page but they will be stopped because of the php code exiting the script if they are not the correct exam number.

    What I want is not necessary more like desire. Like I said the php part is done and they will be stopped on the next page if they get around the javascript, I just want to do that.

  4. #4
    Join Date
    Dec 2011
    I guess it wasn't so dumb and no body can answer it , weird, what I am trying to do sounds very simple.

  5. #5
    Join Date
    Sep 2009
    Well I'm not very good at native javascript, note that 's' in getElementsByTagName() method. Since there could be multiple number of elements specified by a tag, it has 's' in the name of the method, thus returns a set of elements.

    So basically you had a syntax error. However if you just want to select only one element out of the whole page, I suggest you use .getElementById() method, and give the element you want to select an id.

    I could probably give you example in jQuery to get this sort of job gone, but I need to run a couple of errands now. But try to use .getElementById() first to see how it works out.
    Got jQuery problem? jQuery vault can help you find solutions. Give it a try and find out.

    Click jQuery vault to help us help you.

  6. #6
    Join Date
    Dec 2011
    Centurion, South Africa
    Maybe a dumb answer but...

    I would change the Javascript code to be something like this...

    function exval(link_exam)
    	var exam_number = <?php echo $exam; ?>; //Assuming $exam exists at this point
    	if (link_exam > exam_number) {
    		alert("You are not on this exam yet!");
    	} else
    	if (link_exam < exam_number) {
    		alert("You already took this exam");
    	} else {
    		alert("This is your exam");
    		return true;
    	return false;
    And simplify the links generated by PHP to go like this...

    PHP Code:
    if (($exam >= 1) && ($exam <= 10)) {
    '<a href="#" onclick="return exval(1);">Exam 1</a>' .
    '<br />' .
    '<a href="#" onclick="return exval(2);">Exam 2</a>' .
    '<br />' .
    '<a href="#" onclick="return exval(3);">Exam 3</a>';

    Obviously you would populate the "href" values in each link with the correct exam page, and the exval function would either allow it or not.

    I tested the proposed scenario on my PC using IE and FF and all seems to be working.

  7. #7
    Join Date
    Dec 2011
    Thanks for your reply pactor21, that was an error on the syntax. It's still not working but at least is a step closer. I need getElementsByTagName('a') because there are hundreds of them (not kidding). But I ask you please look at my script as an idea, keep in mind I don't know anything about javascript and I am pretty sure that the way I am doing it is not the correct way. I merely wrote that based on the way I would get it on php (but since I need to display it client-side I need javascript and not php, I already got it working on php on the actual exam page after they click and are sent to the actual page) I want this script to stop them to getting to the next page (which is going to stop them with php anyways on the next page, the javascript is an extra protection).

  8. #8
    Join Date
    Sep 2009
    Looks like Bionoid did it for you. The only thing I would add is, not quite related to the issue, use a for loop or something to display links on your HTML page with different exam numbers.

    if (($exam >= 1) && ($exam <= 10)) { 
       for ($i=1; $i<=10; $i++)
          echo '<a href="#" onclick="return exval('.$i.');">Exam '.$i.'</a>';
    Got jQuery problem? jQuery vault can help you find solutions. Give it a try and find out.

    Click jQuery vault to help us help you.

  9. #9
    Join Date
    Dec 2011
    THANK YOU SO MUCH!!! I've been breaking my head, it worked perfectly! Thanks to both of you Bionoid and pactor21. Bionoid's script did the trick and pactor21's script is what I was planning on doing later (well now I don't have to ). I will take my time to learn javascript, I just needed this in a hurry. All I have to learn is the syntax and the different frameworks that are for javascript. I've been able to modify some scripts here and there but I never wrote any but I don't find it too complicated. Once I take my time to read what I can do with jquery and the difference between it and mootools or other frameworks I'll be helping others too although I am a person that likes to write all my codes from scratch I can see how those frameworks can be useful in different scenarios.

    Again thank you all for your support!

  10. #10
    Join Date
    Sep 2009
    Glad to help out, and GL on learning jsp.
    Got jQuery problem? jQuery vault can help you find solutions. Give it a try and find out.

    Click jQuery vault to help us help you.

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.11267 seconds
  • Memory Usage 3,214KB
  • 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
  • (3)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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