-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>
16
  • You forgot to use the var keyword. Are there any errors in the browser's console? Commented Jun 23, 2015 at 14:46
  • 1
    Have you made sure $allpostcomments contains the correct array keys (e.g. var_dump($allpostcomments) ) to ensure the keys exists Commented Jun 23, 2015 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. Commented Jun 23, 2015 at 14:48
  • The $PHPvariable is really set? Commented Jun 23, 2015 at 14:48
  • 1
    what result do you get when you do var_dump($allpostcomments); ? placed after $allpostcomments = $postcommentsstmt->fetchAll(); Commented Jun 23, 2015 at 15:25

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
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.

4 Comments

Hello, thank you for the reply! Thing is though, I really do need the variables :)
Have you tried putting script below input and putting a blank value="" on the input
No, sorry. I need to grab the value in Javascript from PHP because of where I need it. That is sadly not going to work. It is not THAT simple (I wish it was :D).
If this input is also in a foreach statement make sure the id is set as a unique id for each result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.