0

Apologies for the NOOB question but I’m still learning.

Firstly I’m not asking for someone to program the following problem for me or to give me the code. I’m merely looking for a bit of help with the logic and a bit of advice.

What I am trying to do

This is just an example, what I am trying to do is different but will use the same logic: I am trying to display a movie poster on my website, the user can than rate that movie between 0 - 5 stars, his/her rating will be recorded in a database, along with other users’ ratings. When he/she has finished his/her rating, a new movie poster needs to be displayed where he/she can again rate the poster and so it goes on until he/she is tired of rating movies.

The Logic

  • I AM ASSUMING I will need to use PHP here to store and retrieve the data
  • A Table will be created which contains different images and ratings corresponding to that movie’s rating, along with the number of users who rated the movie to calculate the average for the total rating

The Problem

  • How do I get the image to change once the user rated the image?

  • Is it possible to change an image src onclick with PHP like it is with Javascript?

  • If so how do I do it? Is it possible that I will need to use a combination of PHP and Javascript here?

Any help or advice will be greatly appreciated. Thank you in advance.

Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
Marilee
  • 1,598
  • 4
  • 22
  • 52
  • possible duplicate of [How do I run PHP code when a user clicks on a link?](http://stackoverflow.com/questions/1280767/how-do-i-run-php-code-when-a-user-clicks-on-a-link) –  Feb 06 '14 at 01:09
  • 4
    You will use for sure a combination of php & javascript. With ajax (javascript side) you can send/receive data to/from php – Justin Iurman Feb 06 '14 at 01:11
  • possible duplicate of http://stackoverflow.com/questions/19323010/execute-php-function-with-onclick –  Feb 06 '14 at 01:11
  • @Xero I had a look at the question which you marked duplicate, but I dont see it as being a duplicate question – Marilee Feb 06 '14 at 01:14
  • @Marilee Your title: "PHP change image onclick", The _Duplicate of_ title: "PHP Onclick functions (second one)" –  Feb 06 '14 at 01:16
  • @Xero I guess then ill need to edit the title, with all due respect the actual question content is different – Marilee Feb 06 '14 at 01:18
  • @Marilee, okay, but see the second _Duplicate of_ for an idea on how to get the basics of it. –  Feb 06 '14 at 01:21

1 Answers1

3

You definitely need to use both javascript and PHP to accomplish this.

I would approach it like this:

a) Use jQuery javascript library to make the javascript easier.

b) Write some javascript which responds to the user selecting a rating. You'd probably need to bind to the onClick event of your rating buttons.

c) When a rating button is clicked, the javascript functions should use jQuery.ajax to send the selected rating to the server via an HTTP POST. The data sent to the server would probably need to contain an ID to identify the movie, an ID to represent the user (so you can stop people voting more than once for the same movie), and the rating they chose.

d) On the server-side, you can write a PHP script to handle the vote submissions. It would check for the movie, and user ids, (stored in PHP's $_POST variable), and then save the rating to some database. It could then also send a response back to the client which would contain the next movie id, and the next poster. I would recommend you use json_encode to store this info in a way that's easy for the javascript to interpret.

e) Finally, back on the client side, your javascript code can react to the data sent back by PHP, by putting up a message like 'thank you for your vote', then changing the movie details on screen to replace them with the new one.

Your client-side code would look something a bit like this:

<img id="movie-poster" src="movie poster src" />
<ul>
  <li class="rating-button">1</li>
  <li class="rating-button">2</li>
  <li class="rating-button">3</li>
  <li class="rating-button">4</li>
  <li class="rating-button">5</li>
</ul>
<script>
var currentUserId;
var currentMovieId;
$('.rating-button').on('click',function() {
  $.ajax({
    url: "URL of PHP script here",
    type: 'post',
    data: {'movieId':currentMovieId, 'userId':currentUserId, 'rating':$(this).text()},
    dataType: 'json',
    success: function(response) {
      // this runs after your PHP script has responded
      // update your page with the new movie here
      alert('thanks for voting');
      currentMovieId = response.newMovieId;
      $('#movie-poster').attr('src',response.newMoviePosterSrc);
    }
  });
}
</script>

Your PHP script would look something a bit like this (you'll have to figure out all the database and user authentication bits yourself)

<?php

$user_id = $_POST['userId'];
$movie_id = $_POST['movieId'];
$rating = $_POST['rating'];

// check that the user is allowed to vote - possibly examine cookie for this
// check that rating is between 0 and 5
// check that the movie exists
// store results of vote in database

// load up another random movie from the database and send it back (in JSON format)
// assume details of new movie are in $new_movie = 

header('Content-type: application/json');
echo json_encode(array('newMovieId'=> new movie id here, 'newMoviePosterSrc' => URL of new poster here));
exit;

You should also add some error handling to that code. EG, messages to display if there's a connection problem, or the movie ID isn't recognised or something.

This page has more info on how to select a random row from your database - IE to select at random the next poster to show: How to randomly select rows in SQL?

Hopefully that should be enough to get you started.

Community
  • 1
  • 1
Daniel Howard
  • 4,330
  • 3
  • 30
  • 23