3

I'm trying to pass an array of data from php to java script for "onclick" event. I do it by converting the array data into JSON string in order to parse it back in the js function and work on it.

The problem is that JSON string contains double quotes , so it arises an error as the double quotes break the html string (Uncaught SyntaxError: Unexpected token ILLEGAL ). I did see several questions similar to this, but didn't find a solution to what I need, or maybe I didn't understood the correct solution. So I bring it up here with my specific case.

<?php 
    ..some php code here..
    $aData = array("You","Me",76,array(3,6));
    $sJSONstr = json_encode($aData);
?>

<input type="button" name="formSubmit" value="Delete" onclick="analyze('<?php echo $sJSONstr; ?>')">

 <?php 
    ..some php code here..
 ?>

and the js function is as follows:

function analyze(i_sInputDataJSONStr) 
{   
var aInputData = JSON.parse(i_sInputDataJSONStr);

    .. So something with the input data array..
}

3 Answers 3

3

Use single quotes for the onclick attributes instead of double quotes. Single quotes is equally valid as double quotes.

One more thing, since you already have your data in JSON format, there is no need to put it as a string in the analyze function call, since your JSON data is a valid JavaScript array (that's what JSON stands for: JavaScript Object Notation). Therefore, you don't have to parse the input string in your analyze function declaration.

Consider the following example, this is perfectly valid code.

<?php
    $arr = ["Hello", "World"];
    $json = json_encode($arr); // $json = '["Hello","World"]'
?>
<div id="myDiv" onclick='doSomething(<?php echo $json; ?>)'>Click me</div>

<script type="text/javascript">
function doSomething(data){
    for (var i = 0; i < data.length; i++) {
        alert(data[i]);
    }
}
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

How can you say that? in onclick="analyze('<?php echo $sJSONstr; ?>')" he already give a single quote inside onclick()
Thanks, but I need also single quotes around the <?php echo $sJSONstr; ?> in order that it will be a string and not an object. So how should I do it?
@Kannan please see my updated answer. Since his data is JSON, there is no need to put it as a string (and parsing it again).
Great...thanks Anton. Indeed I did an unneeded round through JSON "stringing". But as you said ,I can just send it as object and that's all. So I can put the single quote.
Yes exactly, i did the same mistake myself; not thinking about the fact that JSON actually is valid JavaScript
|
0

create a javascript string and pass it:

<script type="text/javascript">
var myjson = '<?php echo $sJSONstr; ?>';
</script>

and then:

onclick="analyze(myjson)"

Comments

-1
<input type="button" name="formSubmit" value="Delete" onclick='analyze(<?php echo $sJSONstr?>)'>

Replace the double quotes with single quotes in onclick=''; Worked like a charm for me.

Comments

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.