What is the proper way of doing this?
$chatterhtml .= '<span style="float:right;" >
x
</span>';
I am receiving an error in Firefox SyntaxError: syntax error deletecmnt(this, but this worked up until I changed from a input text to text area.
The problem is that you are using quotes to delimit your values in the javascript AND in your HTML. The result is something like:
onClick="deletecmnt(this, "0", "someurl.html");"
which is not valid. The onClick here becomes truncated to just onClick="deletecmnt(this, " and the rest is treated as invalid HTML attribute data
Instead, you should do something like:
$chatterhtml .= "<span style=\"float: right;\">" .
"x" .
"</span>";
Your Resultant HTML is not valid. You have double quotes within double quotes un escaped. Try this
$chatterhtml .= '<span style="float:right;" >x</span>';
Related
I am pulling my hair with this simple task...
From a php variable that contains double quotes (") which is echoed in a div, the html source shows that " have been replaced by ". So far OK.
However, when the html code within the div is copied with jQuery, some of the double quotes are repeated.
I need to fix that in order to use MindMup editor properly.
$Text = 'This is an <span class="myclass">example</span> where double-quotes are added by "I don\'t know what".'
<a onclick="javascript:copyEditor();">copy code</a>
<div id="editor">
echo $Text;
</div>
<script>
function copyEditor() {
$('#hiddenEditor').html($('#editor').html());
}
</script>
If the text is typed directly into the html page with double quotes, then it is fine. See the difference in that example.
So my question is how do I stop php/html to convert the double quotes into " when displaying that variable in the page?
There is also an empty line added on top in the link above after copying the code... why?
Replace double quotes with "
To replace the quotes in user input:
$escaped_quotes = str_replace( "\"", """, $string );
source
I have a large string of valid HTML code that will be inserted into the DOM, the string also contains PHP code. I want the php code to be displayed as plain text, Chrome automatically comments out the PHP code. Obviously the PHP code is wrapped in <?php ... ?> so my question is:
How can I replace the open <?php with a <span> tag, and replace the ?> with a </span> to close the open <span> tag so that the PHP code is within the <span> tags and therefor visible as plain text?
EDIT: I will be wrapping the PHP code back in <?php ?> by replacing the span, so escaping the characters will make that more difficult.
You could escape it.
From:
https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
As you say that the HTML is a string, you can easily just use the replace() function.
var html = // your html
var sanitized = html.replace(/<\?php/g, "<span>").replace(/\?>/g, "</span>");
Use Regular Expressions!
RegExp101 is a great playground.
Remember to change the flavor on the left menu to JavaScript.
The easiest way is using the .replace() function.
var str = str.replace(/(<\?php)|(<\?)|(<\?=)/g, '<span>').replace(/(\?>)/g, '</span>');
Edit: As requested by Get Off My Lawn, I've added support to <?= nd <? tags. The pipe | represents an or in Regular Expressions.
can someone help me with this please. I need to get this stored in a variable as a long string. I have a function called that simple inserts this into the DOM appended to another div. Everything works perfectly fine except I cant work out how to store this string with all the 's and "s inside it. I have tried using \ and unescape methods i have found in my search but cant seem to get anything to work. Can anyone help me out please?
'<div class="centerContent"><P>Please fill in the form below to send an email to H.O.V.A.R.<br/>All fields are required.</p><?php if(!empty($errors)): ?><div class="errorPanel"><ul><li><?php echo implode('</li><li>',$errors); ?></li></ul></div><?php endif ?><form action="contact.php" method="post" accept-charset="utf-8"><label>Your Name:<input type="text" name="name" style="display:block" autocomplete="off"<?php echo isset($fields['name']) ? 'value="' . e($fields['name']) . '"' : '' ?>></label><label>Your email address:<input type="text" name="email" style="display:block" autocomplete="off"<?php echo isset($fields['email']) ? 'value="' . e($fields['email']) . '"' : '' ?>></label><label>Your Message:<textarea name="message" rows="10" style="display:block"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea></label><input type="submit" value="Send" style="display:block"></form></div>'
Although the following shows how to escape quotes, having such a long string is quite unwieldy and it's unclear what you are trying to ultimately achieve...
Also, as you are also attempting to insert content from PHP, this will also need to be escaped in a similar fashion.
var HTML = "<div class=\"centerContent\"><P>Please fill in the form below to send an email to H.O.V.A.R.<br/>All fields are required.</p><?php if(!empty($errors)): ?><div class=\"errorPanel\"><ul><li><?php echo implode('</li><li>',$errors); ?></li></ul></div><?php endif ?><form action=\"contact.php\" method=\"post\" accept-charset=\"utf-8\"><label>Your Name:<input type=\"text\" name=\"name\" style=\"display:block\" autocomplete=\"off\"<?php echo isset($fields['name']) ? 'value=\"' . e($fields['name']) . '\"' : '' ?>></label><label>Your email address:<input type=\"text\" name=\"email\" style=\"display:block\" autocomplete=\"off\"<?php echo isset($fields['email']) ? 'value=\"' . e($fields['email']) . '\"' : '' ?>></label><label>Your Message:<textarea name=\"message\" rows=\"10\" style=\"display:block\"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea></label><input type=\"submit\" value=\"Send\" style=\"display:block\"></form></div>";
console.log(HTML);
Since your string starts with single quote, you need to escape every single quote during the time you are building the string (if you were starting the string with double quote you had to escape the double qotes). I.e inside the string every single quote needs to be escaped by a back-slash in front of it (like so: \'). You cannot do this when using the string, since PHP and/or Javascript will complain about the string having illegal tokens.
When you do so, you can echo it in PHP, but since it is a string it will not give you the result you wanted. You'll either get a distorted form or an error depending on the technique used.
This all boils down to having PHP calls in the string. What you therefor should do is eliminating the php calls in your string. So insted of entering "<?php .... ?>" in the string you should enter the outcome of the PHP call into the string.
I have array of string returned by Codeigniter's controllers upon user's login.
Some of the string(s) are in html format. I would like to append those strings to html element when there is an event done by the user.
Here is the example of the html string in php variable returned by controller
$form_dropdown = form_dropdown('selCat_GP[]', $subcats2,'0', 'class="selCat_GP"');
$Dropdown_GP = '<div class="tr">'
. '<div class="td">Category :</div>'
. '<div class="td">'. $form_dropdown .'</div>'
. '<div class="td">Sub Category :</div>'
. '<div class="td">'
. '<select class="selSubCat_GP" name="selSubCat_GP[]">'
. '<option value="0" selected="true">-Select a category first-</option>'
. '</select>'
. '</div>'
. '<div class="td">Score :</div>'
. '<div class="td"><input class="score" name="score_GP[]" value="0" type="number" size="1" step="10" min="0" max="100" maxlength="2"/> Remove [-]</div>'
. '</div>';
and what I did in jQuery is :
jQuery('#preference'+preference).append(<?php echo json_encode($Dropdown_GP); ?>);
But it gives me this error
I have tried to add both single quote and double quotes but it gives me
Uncaught SyntaxError: Unexpected token ILLEGAL
jQuery('#preference'+preference).append('
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
jQuery('#preference'+preference).append("
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
Do you how can I echo the php html string and append it to html element in the view using jquery (when there is an event fired) ?
I know I can use ajax post to request the data from the server instead of returning it beforehand. It is just because the same data will be requested often so I don't wish to give more work to the server. I just need to load it one time and just simply display it when user requests it.
You missed single quotes.
jQuery('#preference'+preference).append('<?php echo json_encode($Dropdown_GP); ?>');
You are missing quotes, I just tried:
$('#header').append('<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">Stackoverflo,ooo,ooow</div> ');
on this SO page (replaced #preferences with #header) and it worked without any problem.
Please check again it string you are trying to append is quoted correctly.
I have found a solution to my problem.
Since I placed the jQuery script right after body and before my html codes, all the variables returned by codeigniter controller are not recognized by jQuery.
There are two steps to solve my problem :
Place the jQuery after html code but before the closing tag of body
I still need to use json_encode(..) to avoid any break line of html string and it is used without any single nor double quotes.
Thank you everyone for the answers and comments
I have this piece of code, which doesn't work, I couldn't find where is the problem?
$texttt = "blabla";
echo "<div onclick='select(\" page d \' accueil \");'>".$texttt."</div><br/>";
function select(text){
alert(text);
}
" is breaking your php statement
echo "<div onclick=\"select(' page d\' accueil ');\">\"Page d'accueil\"</div><br/>";
function select(text){
alert(text);
}
try to use addslashes and htmlentities to escape the quotes between text
$texttt = htmlentities(addslashes($text), ENT_QUOTES);
echo "<div onclick='select(\"$texttt\");'>$texttt</div><br/>";
Use single quotes for PHP. I use single quotes for PHP (which is server side script) and double quotes for client side scripts unless it is not obligatory.
echo '<div onclick="select(\'page d\'accueil\');">"Page d\'accueil"</div><br/>';
the problem is sourced by single quote in d'accueil:
I dont know if it is possible to escape a string which is located in another escaped string.
The possibilities:
You can change the grammar rules of your language:
Affects too many people.
Not applicable.
You can change the way of typing single quote: using ' in context.
I have typed a new code (which is working well) for you :
<?php
echo '<div onclick="select(this.innerHTML);">"Page d'accueil"</div><br/>';
?>
<script>
function select(text){
alert(text);
}
</script>
you should avoid useless quotes before you get lost inside.
"Page d'accueil" don't need to be between quotes so remove them.
Use one kind (single or double quotes) for the first level of quoting, and the other for the next levels, and keep that it mind.
<? php echo '<div onclick="select(\" page d\' accueil \");">Page d\'accueil</div><br/>'; ?>
<script type="text/javascript">
function select(text){
alert(text);
}
</script>
you can use heredoc syntax
refreence : http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
also try this:
echo json_encode("<div onclick='select(" page d ' accueil ");'>{$texttt}</div><br/>");