I am writing a php script which generates html with a javascript onkeyup event, as such:
// generate the function text
$fn_text = "checkContentQuizAnswer(\"#".$var_name."\",\"".$equation_solutions_array[0][0]."\",\"{'incorrect_values':[";
for($r=1;$r<count($equation_solutions_array);$r++) {
// THIS ISNT WORKING CORRECTLY, NEED A BETTER WAY TO PASS INCORRECT INFO
$fn_text .= "{'equation':'".$equation_solutions_array[$r][0]."','message':'".$equation_solutions_array[$r][1]."'}";
if(($r+1)<count($equation_solutions_array)) {
$fn_text .= ",";
}
}
$fn_text .= "]}\")";
However this isnt working... the code it generates looks something like this:
onkeyup="checkContentQuizAnswer("#theonlyvariable","27.62*5","{'incorrect_values':[{'equation':'27.62+5','message':'you added 5'},{'equation':'27.62-5','message':'you subtracted 5'}]}")"
however, it hasn't been working... the errors have included complaints about missing }'s, MathJax (an eval() style library) is undefined.
Im sure that it is how I am parsing the onkeyup part of the tag but i cant get the combination/syntax correct.
Can anybody see a glaring problem? :S
Thanks
Alex
Although you are escaping the quotes in the PHP file, the quotes in the javascript parser are not being escaped. Change the quotes inside the onKeyUp to single quotes to fix this. You will also need to escape the quotes in the JSON as well - the json_encode() function can help with that. You need to output something like:
onkeyup="checkContentQuizAnswer('#theonlyvariable','27.62*5','{\'incorrect_values\':[{\'equation\':\'27.62+5\',\'message\':\'you added 5\'},{\'equation\':\'27.62-5\',\'message\':\'you subtracted 5\'}]}')"
Related
Hi Guys I've been dealing with an estrange thing while trying to pass string parameters to a JavaScript function from code behind, this is what I have actually in code behind which is wrong:
thumbnail = "<a href = 'javascript:RemovePirctureDetail(" + field1 + ",'" + tempname + "');' class='label label-default' rel='tooltip'>Remove</a>";
So this is the bad result I'm getting in the browser:
Remove
Meas that for some reason when I try to pass the string parameter, the html comes out bad formatted. The result should looks like this:
Remove
I tried already send the quotation marks like this /' from code behind, it did not work neither. How can I achieve this?
Thanks.
string thumbnail = "Remove";`
You need to use \ to escape the quotes inside, not /..
With javascript attribute I wouldn't use single quote, because it could be messy
Try to change in this way:
thumbnail = "Remove";
PS: Actually, I would NEVER use single quotes with attributes, it will cause an HTML validation error, HTML is not well formed with single quotes for attributes (although with inspection tools you see double quotes.. because inspection tools have the need to work with a well formed HTML, so if you want to see the real HTML of your page, view the source code (generally the option is on right-click on the page)
I don't even know how to ask this question correctly, I tried to put quotes around a part of my string however it always break.
I am creating html dynamically and I am encountering error when I try to do this:
onclick='deleteRow("item"+count+"")'
I am trying to pass item1 as a string to a deleteRow function however best I could do is pass it like this deleteRow(item1) with no quotes. I am not sure how to escape them so that they would show.
This line of code is generated inside my JavaScript file.
I would recommend to use jQuery event handlers instead of inline one..
But in this case the below should do it
"onclick='deleteRow(\"item"+count + "\")'"
I'm asking this because I'm using a trick that works but I think my be problematic in the future.
I generate a html form in php using SimpleXML and DOM to manage the source (a html file with all forms needed). So I got my form and got to fill some properties. Here is an example:
<input type="hidden" name="arquivo_tipos" value="{arquivo_tipos}" />
{arquivos_tipos} is a JSON string, won't work inside double-quotes. However, if I create the source code like this:
<input type="hidden" name="arquivo_tipos" value='{arquivo_tipos}' />
After being processed by php, it will return to double-quotes (I pick the form from a lot as a xml node). So my solution is replacing the property with this kind of script:
html = html.replace('"{arquivo_tipos}"', '\'[{"ext":"jpg","nome":"Imagem JPEG"},{"ext":"jpeg","nome":"Imagem JPEG"},{"ext":"gif","nome":"Bitmap GIF"},{"ext":"png","nome":"Imagem PNG"}]\'');
This is JavaScript, but in php I use the same trick with str_replace.
The point is, this is the final code, so it works, but smells like it will fails in the future if another process take this result. Is there a better way, a right way of doing this?
Use php htmlspecialchars function, it can escape
double-quotes to "
single-quotes to '
http://tw1.php.net/htmlspecialchars
I'm trying to pass a json_encode-d array from PHP to JavaScript using JQuery's getJSON, and then display it in the HTML. It works fine in all cases except when I want to pass basic HTML tags, e.g. 34 shows up as:
3<sup>4</sup>
It works fine when I have:
var myVar = <?php echo json_encode(jsonArray); ?>;
But I need to use getJSON as well. Is there a simple way to do this? Maybe a regex I failed to figure out?
Try this:
$('#YourTarget').html( $('<div/>').html(yourStringFromJSON).text());
For example:
$('h1').html( $('<div/>').html('Hola<sup>Hey</sup>').text());
Here's the regex to unescape the forward slash:
var cleanString = dirtyString.replace(/\\\//g,"/");
I need to pass a variable to a javascript function,but I got a little trouble.
In the .cs file,I write like this:
string id = "someid";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction("+id+")\">"));
I need to use the value of this id,but in the console.log(id),it just shows "object",not the "someid",what's the problem?
Look at the generated HTML:
<input type="button" onClick="myFunction(someid)">
You are generating a variable name when you want a string literal.
Add some quote marks.
Whenever you have a problem that manifests in the browser: Look at the code the browser is dealing with first. You should always start by determining if the JS you want is not working or if the server side code is not generating the JS you want.
just add '' around id as i did below will resolve your issue
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
Use the single quotes around id:
'"+id+"' (notice the single quotes then double quotes)
The problem is that it is looking for a saved variable on the page named someid, which is why you are getting the object in the console. Pranay Rana has a good solution which is to make sure the string someid is surrounded by single quotes.