www.webdeveloper.com
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
    Posts
    8

    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:

    <script>
    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();
    }
    </script>

    the php part:

    <?php
    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>
    Ect...

    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
    Location
    Milwaukee
    Posts
    342
    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.

    Code:
    <html>
    <head>
    <?php
    	$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>";
    		}
    	}
    ?>
    <head>
    <body>
    <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>
    <?php
    	$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>";
    	}
    ?>	
    	</select>
    </form>
    </body>
    </html>

  3. #3
    Join Date
    Dec 2011
    Posts
    8
    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
    Posts
    8
    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
    Posts
    159
    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
    Location
    Centurion, South Africa
    Posts
    776
    Maybe a dumb answer but...

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

    Code:
    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)) {
        echo
            
    '<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
    Posts
    8
    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
    Posts
    159
    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.

    Code:
    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
    Posts
    8
    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
    Posts
    159
    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



Recent Articles