/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@BrainDonorDec 01.2011 — 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>[/CODE]
Copy linkTweet thisAlerts:
@jmc0228authorDec 02.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.
Copy linkTweet thisAlerts:
@jmc0228authorDec 03.2011 — I guess it wasn't so dumb and no body can answer it ?, weird, what I am trying to do sounds very simple.
Copy linkTweet thisAlerts:
@pactor21Dec 03.2011 — Well I'm not very good at native javascript, note that 's' in getElement[COLOR="Red"]s[/COLOR]ByTagName() 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.
Copy linkTweet thisAlerts:
@bionoidDec 03.2011 — 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;
}[/CODE]


And simplify the links generated by PHP to go like this...

[code=php]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>';
}[/code]


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.
Copy linkTweet thisAlerts:
@jmc0228authorDec 03.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).
Copy linkTweet thisAlerts:
@pactor21Dec 04.2011 — 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>';
}
}[/CODE]
Copy linkTweet thisAlerts:
@jmc0228authorDec 04.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!
Copy linkTweet thisAlerts:
@pactor21Dec 04.2011 — Glad to help out, and GL on learning jsp.
Copy linkTweet thisAlerts:
@ExamsleagueNov 14.2018 — Dear Students We are started the process of University Exam Form 2019 at Examsleague Center website. Check here for All University Admission online form 2019.

contect us for more details - https://www.examsleague.co.in/university-exam-form/
×

Success!

Help @jmc0228 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...