The line i am reffering to is
url:"../php/loadmorecommentsuserpage.php?profilepageuserid=$profilepageuserid&lastComment="+$(".postedComment:last").attr("id"),
i want to send the $profilepageuserid via GET to the loadmorecommentsuserpage.php, but it is not working.
specifically the value that $profilepageuserid represents is not being sent. so is the syntax wrong?
$profilepageuserid is a variable in PHP and i want to send it via JS
<script>
$(document).ready(function(){
$('div#text').click(function(){
$("div#text").hide();
$('div#loadMoreComments').show();
$.ajax({
url:"../php/loadmorecommentsuserpage.php?profilepageuserid=$profilepageuserid&lastComment="+$(".postedComment:last").attr("id"),
success:function(html){
if(html){
$("#postedComments").append(html);
$('div#loadMoreComments').hide();
$('div#text').show();
}else{
$('div#box').replaceWith("<div class='box'><center>Finished Loading the Comments</center></div>");
}
}
});
});
});
</script>
$(function() {
//your variable
//if it's from PHP, you need to echo it to a JS variable
var profilepageuserid = '<?= $profilepageuserid ?>'
$('div#text').on('click',function() {
//store required data into variables
//keeps your code readable
var commentid = $(".postedComment:last").attr("id");
$(this).hide();
$('div#loadMoreComments').show();
$.ajax({
url:"../php/loadmorecommentsuserpage.php",
//instead of a very long querystring, just do "data"
data : {
'profilepageuserid' : profilepageuserid,
'lastComment' : commentid
},
success: function(html) {
if (html) {
$("#postedComments").append(html);
$('div#loadMoreComments').hide();
$('div#text').show();
} else {
$('div#box').replaceWith("<div class='box'><center>Finished Loading the Comments</center></div>");
}
}
});
});
});
Alright. This is how PHP interacts with Javascript. Simply put, it barely does. PHP is run by the server, which generates text. This text is sent to the browser, which runs any Javascript, grabs images, etc. Thankfully, since the Javascript is part of that text, we can cram any data we want into there, including any server state.
So to initialize a Javascript variable (var foo
) on the client side (to, say, the current value of $bar
), you will need to cram that data into a Javascript variable declaration. What you want the client to see is as follows:
var foo = 3; // or whatever else, depending on $bar
So in order to do that, you'll need to echo
the value of $foo
at the correct time, surrounded by the Javascript syntax:
echo "var foo = $bar;";
Remember that you're merely sending data to the client, to be run (or not run) at a later time. You can never "pull" the data back from the browser to the still-running script. By the time the browser gets the data, the script has finished running and the server has moved on to other useful things, such as enumerating the database.