botiapa botiapa - 3 months ago 6
jQuery Question

I'm trying to echo javascript with php

I'm a beginner, so i hope you can understand me.

I want it to echo a javascript code when the cookie named usr is not false.
The first echo works perfectly but the jquery won't work at all.

<?php
require('checker.php');
$result = checker::isExpired($_COOKIE["hash"]);
$_COOKIE["usr"] = $result;
if ($result != false) {
echo '<span style"color:red;">szeretlek java</span>';
echo '<script src="js/jquery.js">';
echo 'var result = $.cookie("usr");';
echo 'var added = "Hello, " + result;';
echo '$(function {
$("#brand_div").html("alma");
});';
echo '</script>';
}
else {
echo "";
}
?>

Answer

You are trying to echo 2 scripts in one <script></script> tag set.

So amend your code like this adding the script tag for your hand coded script after you include jQuery

<?php 
require('checker.php');
$result = checker::isExpired($_COOKIE["hash"]);
$_COOKIE["usr"] = $result;
if ($result != false) {
    echo '<span style"color:red;">szeretlek java</span>';
    echo '<script src="js/jquery.js"></script>';
    echo '<script type="text/javascript">';
        echo 'var result = $.cookie("usr");';
        echo 'var added = "Hello, " + result;';
        echo '$(function() {
                $("#brand_div").html("alma");
            });';
    echo '</script>';
}
else {
    echo "";
}
Comments