-1

I am trying to right some some JavaScript to define a variable using PHP. The variable has a little jQuery in it which should get rendered by the client. I say "should" but really mean I want it to. What should I do to make o2 the same as o1 (i.e. covert the jQuery)?

Thank you!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 
<title></title> 
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> 
<script>
$(function() {
    var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
    console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "'.$d.'?id=\"+$(\"#id\").val()"}}';
//echo($json);
$json=json_decode($json);
//echo(print_r($json,1));
$json=json_encode($json);
//echo($json);

echo("var o2=".$json.";");
?>

console.log(o2);
});

</script>

</head>

<body>
<input type="hidden" id="id" value="123" />
</body> 
</html> 
6
  • 3
    PHP is rendered server side while javascript is rendered by the client (i.e. browser). You'll need to send the json to a php script using ajax. Commented May 20, 2012 at 23:09
  • Are you getting this "123" value in your hidden input from PHP side? How are you printing this id? from something like $row['id']? Commented May 20, 2012 at 23:25
  • @Ryan. Understand the ajax stuff. Just want to use PHP to pre-write a variable to be available to the client. Commented May 20, 2012 at 23:41
  • @Oscar. Yes, from PHP side. I guess I can just write this variable directly into the json, but it is bugging me why this doesn't work. Commented May 20, 2012 at 23:44
  • @user1032531 Ok, then I think it will be easier, take a look at my answer maybe helps. Commented May 20, 2012 at 23:54

3 Answers 3

0

You have this:

<input type="hidden" id="id" value="123" />

So I can assume that you are getting that id from a db right? Maybe like this:

<input type="hidden" id="id" value="<?php echo $row[id];?>" />

If this is the case do the following (I tested and works):

You don't need hidden inputs to do it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 
<title></title> 
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> 
<script>
$(function() {

    <?php
        //From your SQL query you obtained
        //$row['id'] = 123;
        $id = $row['id'];
    ?>

    var o1={"foo":{"bar": "abc/index.php?=" + <?php echo $id ?>}};
    console.log(o1);

    <?php
    $d='abc/index.php';
    $json='{"foo":{"bar": "'.$d.'?='.$id.'"}}';
    //echo($json);
    $json=json_decode($json);
    //echo(print_r($json,1));
    $json=json_encode($json);
    //echo($json);
    echo("var o2=".$json.';');
    ?>

    console.log(o2);
});
</script>
</head>
<body>
</body> 
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Oscar, I'm with you. Just still curious why my original implementation didn't work. I spent hours trying to get it to do so. Thinking it might have been a striptag type of thing.
@user1032531 No problem :-) sometimes happens.
Thanks again Oscar. Your solution works but still curious why my earlier attempt didn't. Any ideas why not?
@user1032531 Well, at first sight, jQuery can't be rendered in a simple PHP string as you expect when doing this: "$json='{"foo":{"bar": "'.$d.'?id=\"+$(\"#id\").val()"}}';" I think that was the error :-) but maybe someone around here will argue lol
0

Untested, may contain errors:

<script>
$(function() {
    var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
    console.log(o1);

    <?php
    $d='abc/index.php';
    $json='{"foo":{"bar": "' . $d . '?="+$("#id").val()}}';

    echo("var o2=".$json.';');
    ?>

    console.log(o2);
});
</script>

1 Comment

While this works, it doesn't if $json is first json_decode() and then json_encode().
0

I prefer make json with the json_encode, to avoid unexpected problems with parse and validation of json.

the json_encode and json_decode only work with UTF-8, another charset will result in a blank string, if it has special chars.

<script>
<?php

$d = "abc/index.php";
$json['foo']['bar'] = $d . '?id=$("#id").val()';

echo "var o2 = ".json_encode($json);

?>
console.log(o2);
</script>

1 Comment

Thanks Roger, My definition of $json was very small, but the real one had many more variables and sub-variables. Also, the original definition is based on a client only side application, and I wanted to just cut-and-past it with as little modification as possible. Maybe a limitation of json_decode?

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.