[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>
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.
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>
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.
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.
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...
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).
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.
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.
Bookmarks