-1

I am currently having trouble with this. I would like to make one of my variables in Javascript have a PHP value. Here is what I mean:

<script>
    JSvariable = <?php echo $PHPvariable; ?>;
</script>

For some reason that is not working. Here is my full (snippet) of code:

<script>
    currentreplyid = <?php echo $allpostcomments[$key]['replyid']; ?>;
    $('#parentcommentholder').val(currentreplyid);
</script>

I am sure it is some stupid mistake, but I can not seem to find it! What is the problem? Thank you!

PS #parentcommentholder is an input field, and it just had the value 0 after the field is supposed to of been changed.

Here is some source:

<?php
$postcommentsquery = "SELECT * FROM comments WHERE parent = :parent AND postid = :postid ORDER BY datecreated DESC";
$postcommentsparams = array(':parent' => $allreplies[$key]["postid"],
                            ':postid' => $postid);
try{
    $postcommentsstmt = $connection->prepare($postcommentsquery);
    $postcommentsresult = $postcommentsstmt->execute($postcommentsparams);
}
catch(PDOException $ex){
    echo ("Failed to run query: " . $ex->getMessage());
}
$allpostcomments = $postcommentsstmt->fetchAll();
foreach ($allpostcomments as $key => $value) {
?>
    <script>
    var currentreplyid = <?php echo $allpostcomments[$key]['replyid']; ?>;
    $('#parentcommentholder').val(currentreplyid);
    </script>

    <input id="parentcommentholder"></div>
Michael Jones
  • 2,140
  • 15
  • 31
  • You forgot to use the `var` keyword. Are there *any* errors in the browser's console? – Jay Blanchard Jun 23 '15 at 14:46
  • 1
    Have you made sure $allpostcomments contains the correct array keys (e.g. var_dump($allpostcomments) ) to ensure the keys exists – Danny Broadbent Jun 23 '15 at 14:47
  • I tried the `var` keyword, but that did not fix anything. Yes the key is correct, and in the console there are 0 errors. – Michael Jones Jun 23 '15 at 14:48
  • The $PHPvariable is really set? – Renato Galvones Jun 23 '15 at 14:48
  • the variable is an int. – Michael Jones Jun 23 '15 at 14:50
  • Is your script at the top, or bottom, of the page? – Jay Blanchard Jun 23 '15 at 14:50
  • Below where the PHP variable is being declared, so towards the bottom of the page. – Michael Jones Jun 23 '15 at 14:52
  • Try putting the $PHPvariable in a

    inside the HTML. See if there's an echo.

    – Renato Galvones Jun 23 '15 at 14:52
  • 1
    what result do you get when you do `var_dump($allpostcomments);` ? placed after `$allpostcomments = $postcommentsstmt->fetchAll();` – Funk Forty Niner Jun 23 '15 at 15:25
  • @fred-ii I have fixed the big problem with it I believe, now I am trying your code and it says the function is not defined. Any idea why? It occurs when it runs this line `var currentreplyid = "";` – Michael Jones Jun 23 '15 at 16:07
  • sorry, JS isn't my strong point. Plus, you didn't tell me what var_dump gave out. add a function name to the js. `function function_name(){...}` type of thing then call that function. – Funk Forty Niner Jun 23 '15 at 16:11
  • Oh sorry, it is just a giant file from the database. Would you still like to see it? The field I am grabbing is there because I have used the same method before, and it has worked. The field is a plain old int. – Michael Jones Jun 23 '15 at 16:13
  • the problem doesn't seem to be db related, least I don't think so. but the var_dump shouldn't be that big. Have a look at this answer http://stackoverflow.com/a/11811079/ and other answers in that page and may help show you how to build a function. Again,.. JS isn't my bag but I am slowly getting around to it. See if Manwal can help. He may not be around right now. – Funk Forty Niner Jun 23 '15 at 16:15
  • Ok Thank You. Yea it is very odd, I have written functions before and they work. It is just that when I add that one like to THAT specific function it all stops. – Michael Jones Jun 23 '15 at 16:16
  • @Fred-ii I would LOVE to thank you! Your harassment into doing `var_dump()` made me figure out the issue. Well not `var_dump()` but `print_r()`, anyhow I figured out for some reason I was looking over using the wrong list. Thank You!!!! – Michael Jones Jun 23 '15 at 17:24
  • lol my harassment - that's a bit harsh hahaha! but I'm glad you were able to figure out what the issue was. *Cheers* and you're welcome ;-) Edit/P.s.: You should post your own answer then. – Funk Forty Niner Jun 23 '15 at 17:27

2 Answers2

3

Don't forgot for give quotes ' or ". Use following:

<script>
    var JSvariable = '<?php echo $PHPvariable; ?>';
    //or
    var JSvariable = "<?php echo $PHPvariable; ?>";
</script>

Reason: If php variable contains string and if while assigning it to javascript variable we shall not give quote like:

<?php $PHPvariable = 'String';?>
var JSvariable = <?php echo $PHPvariable; ?>;

Will transform into :

var JSvariable = String;//which will give error in javascript

But this will work fine if PHP variable contains a numeric value like:

<?php $PHPvariable = 2;?>
var JSvariable = <?php echo $PHPvariable; ?>;

Will transform into :

var JSvariable = 2;//which will work perfect

Complete code should be:

<script>
    var currentreplyid = "<?php echo $allpostcomments[$key]['replyid']; ?>";
    //or if you are sure your variable contains int value
    var currentreplyid = parseInt("<?php echo $allpostcomments[$key]['replyid']; ?>");
    $('#parentcommentholder').val(currentreplyid);
</script>
Manwal
  • 22,994
  • 11
  • 59
  • 91
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/81312/discussion-on-answer-by-manwal-assigning-php-variables-to-javascript-variables-n). – Martijn Pieters Jun 23 '15 at 17:01
0

Try the below instead of using javascript (as I don't think you need it):

<?php
$postcommentsquery = "SELECT * FROM comments WHERE parent = :parent AND postid = :postid ORDER BY datecreated DESC";
$postcommentsparams = array(':parent' => $allreplies[$key]["postid"],
                            ':postid' => $postid);
try{
    $postcommentsstmt = $connection->prepare($postcommentsquery);
    $postcommentsresult = $postcommentsstmt->execute($postcommentsparams);
}
catch(PDOException $ex){
    echo ("Failed to run query: " . $ex->getMessage());
}
$allpostcomments = $postcommentsstmt->fetchAll();
foreach ($allpostcomments as $key => $value) {
?>
<input id="parentcommentholder" value="<?php echo ((int)$allpostcomments[$key]['replyid']>0) ? $allpostcomments[$key]['replyid'] : 0; ?>" />
<?php
}
?>

If your defiantly sure $allpostcomments[$key]['replyid'] is bringing back a value, this should work without any issues.

Danny Broadbent
  • 1,189
  • 1
  • 9
  • 21