0

This is my example code

<script>
    var item = "yooow";
    jQuery("#view").html(item);
</script>

<?php  $str_html = '<p id="view"></p>';

     echo $str_html; //it will output "yooow" 
?>

but...

when i'm trying to compare this into another php variable

<?php $str_php ="yooow";

    if($str_html == $str_php)
    {
         echo "true";
    }
    else
    {
        echo "false"; 
    }
?>

but it returns to false

how can they be an equal?

5
  • You're setting $str_html to '<p id="view"></p>', not 'yooow' Commented Aug 26, 2014 at 9:08
  • $str_html is <p id="view"></p> and $str_php is yooow in your case. Commented Aug 26, 2014 at 9:08
  • PHP parses the script first. After the HTML (and Javascript_) is loaded in the browser, Jquery works Commented Aug 26, 2014 at 9:13
  • is there a chance to make them equal since their output is the same? Commented Aug 26, 2014 at 9:14
  • how can they be equal? Commented Aug 26, 2014 at 9:21

3 Answers 3

2

PHP is run serverside which means it's not aware of any changes you make in the DOM using Javascript.

Sign up to request clarification or add additional context in comments.

Comments

1
<script>
var item = "yooow";

Value set only in clinet side, PHP don't know about changes here

jQuery("#view").html(item);   
</script>

<?php  $str_html = '<p id="view"></p>';

WRONG, it will output "<p id="view"></p>"
echo $str_html; //it will output "yooow" ?>

<?php $str_php ="yooow";

Comparing "<p id="view"></p>" and "yoow" and that is not equal!

 if($str_html == $str_php)

2 Comments

@Aislinn no, you are setting #view innerHtml to "yoow". Remove JS and will see
i must not remove the JS.
0

Better, you give the item value in cookie/session. And, compare the cookie value with $str_php.

<?php session_start(); ?>
<script>
$(document).ready(function(){
    var item = "<?php  echo $_SESSION['html']="yooow"; ?>";
    $("#view").html(item);
});
</script>
<?php  
    $str_html = '<p id="view"></p>';
    $strHtml=$_SESSION['html'];
    $str_php ="yooow";
    if($strHtml == $str_php)
    {
         echo "true";
    }
    else
    {
        echo "false"; 
    }
?>

3 Comments

i have another question. how can i pass a string variable from php to javascript then javascript to php ?
@Aislinn I believe it's impossible to convert javascript to PHP. Since PHP is server side and JavaScript is client side.
@Déjàvu how about ajax?

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.