Changing a PHP value dynamically with Javascript

Solution:
You would not actually be changing a PHP value--once the page is output to the browser, consider PHP gone--you can't interact with it because it's on the server. Instead, think of interacting with the document that's in the browser.
The best way to do this is to make an ajax call to a server-side script. That server-side script can commit the like or dislike to the database, and then return the new rating, which you can insert in place of the old one using javascript.
You'll probably want to check out some tutorials on javascript and ajax, as it seems you have a more general need for a tutorial than for a specific problem. In other words, if you fill in your knowledge gaps on the general subject, you'll be able to solve your specific problem quite easily.
Answer
Solution:
You will want to create some PHP code for handling the saving to the database on the Server side. You will POST the like / dislike value information to this server side script. If possible, I would use jQuery's AJAX helper to post data to the PHP page you just created.
Something like this:
$.ajax({
url: "whatever.php",
type: "POST",
data: {Like: true},
success: function(data){ /* update view */}
});
Answer
Solution:
You will have to use ajax to accomplish this. Its not possible to alter PHP variables VIA javascript.
You will have to call an Ajax function that will handel the database work and after its done that, you will have to update the count using javascript. This will give the allusion that the count was updated while the count will also be updated in the database (from the Ajax)
Here is a great example of how you could accomplish this:
Example Code
Live Demo
Answer
Solution:
$('.like').click(function(){
rate(1);
})
$('.dislike').click(function(){
rate(-1);
})
function rate(_val){
$.ajax({
url: 'ajax/rate.php?val='+_val,
success: function(data) {
alert('Rate was performed.');
$(".narrow_right_container").find(".yellow_bg").append("Rating: "+data+"%");
}
});
}
inrate.php
:
if(isset($_GET['val'])){
$sql = "UPDATE.........."; //do an update to your rate table
echo get_popularity($row); //return rating to ajax
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.