-1

Basically, I have a list of teacher names, departments and a rating for them. Currently, all ratings are 5/10, however that'll change soon.

So basically, 2 teachers will come up. Mr Smith and Mr White for example. 2 pictures will appear representing each teacher, besides the image, the teacher's name & department comes up. You click the image for the teacher that you think has a higher rating, if you get it right, you go to correct.php, otherwise, you go to incorrect.php.

Here's the code. It just doesnt work when I click the icon, could somebody possibly edit the code so that it'll work, or tell me how I'd do it? Thanks.

<p class="large">Who's got a higher rating?</p>
<img src="icon.png" onclick="one()" class="icon 1"><br>

<div class="text-1"><?php echo "Name: " . $teacher1 . "<br>" . "Department: " . $teacher1department . "<br>" . "Rating: " . $teacher1rating;?></div></br>
<div class="text-2"><br><?php echo "Name: " . $teacher2 . "<br>" . "Department: " . $teacher2department . "<br>" . "Rating: " . $teacher1rating;?></div></br>

<img src="icon.png" onclick="two()" class="icon 2"><br>

<?php
    /* Tells you who got the higher rating */
    if($teacher1rating > $teacher2rating) {
        $higherRating = $teacher1; 
    }
    if($teacher1rating < $teacher2rating) {
        $higherRating = $teacher2;
    }
    function one() {
        $guessed = $teacher1;
        if($guessed == $higherRating) {
            header( 'Location: correct.php' ) ;
        }
        else {
            header( 'Location: incorrect.php')
        }
    }
    function two() {
        $guessed = $teacher2;
        if($guessed == $higherRating) {
            header( 'Location: correct.php' ) ;
        }
        else {
            header( 'Location: incorrect.php')
        }
    }
?>
jophab
  • 5,356
  • 14
  • 41
  • 60
  • Or [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) -- you should read both these topics – Qirel Dec 23 '16 at 21:13
  • Thanks. I'll take a look now! – Hugh Chamers Dec 23 '16 at 21:17

1 Answers1

0

I think you are confusing the concept of a server side language. Your php code and html code are generated on the server and sent to the client, which is your computer. Now when you look at the images of those teachers php code has already been executed and cannot be executed on the client side. You should learn Javascript, which can be used for client side tasks. To cut it short, you php code is already done running when you are presented with the html page in front of you. The solution may be is to add event listeners to your images and fire events based on the id of the image. Seems like you are new to this so take a look at Jquery.

  • So I should use Javascript to do this? – Hugh Chamers Dec 23 '16 at 21:20
  • 1
    Actually I think it is the only solution for your task. Learn about event listeners and ajax. Jquery is a library that you can include in your html which helps you use Javascript functions, just an abstraction layer over Javascript functions. Using pure JS functions right now may be overwhelming a little. With Jquery you can write less code and do the same thing. – Isa Ishangulyyev Dec 23 '16 at 21:23