Syntax problem with quotes in PHP and HTML - php

I have this php code
echo "<textarea id='textarea' cols='70' rows='5' name='code'>".$code."</textarea>";
and I need to put this onClick="SelectAll('txtarea');" after id='textarea' but the quotes are messing me up and I cant figure it out.
Any help?
Thanks!

Explaination
You will need to escape the double quotes, so they will not be read as PHP code. You can do this by typing a \ character before them. You can read more about escaping characters in PHP here.
Edit your code to this
echo "<textarea id='textarea' onClick=\"SelectAll('txtarea');\" cols='70' rows='5' name='code'>".$code."</textarea>";

Try this:
echo "<textarea id='textarea' onClick=\"SelectAll('txtarea');\" cols='70' rows='5' name='code'>".$code."</textarea>";

You can escape quotes with the backslash char "\". Try something like that:
echo "<textarea id=\"textarea\"></textarea>";

Did you try use Escape Character \" ?
So it would be
onClick=\"SelectAll('txtarea');\"

echo "<textarea id='textarea' onClick='SelectAll(\"txtarea\");' cols='70' rows='5' name='code'>".$code."</textarea>";

Use \" instead of " within your text.

Use this
echo "<textarea id='textarea' cols='70' onClick=\"SelectAll('txtarea');\" rows='5' name='code'>".$code."</textarea>";

You have to escape the quotes using backslash, so put in onClick=\"SelectAll('txtarea')\"
Same is recommended for the other attributes, e.g. cols=\"70\"

Why don't you make it easier on yourself? If you need single and double quotes within your string, you can use heredoc syntax, such as:
echo <<<EOF
<textarea id="textarea" cols="70" onClick="SelectAll('txtarea');" rows="5" name="code">$code</textarea>
EOF;

Related

Replace double quotes with single quotes in json string in php

I have a json string which contains some html and it's attrubutes. I'm trying to to escape or replace double quotes with single quotes in this string. my code works with some html attributes but not with all.
My example:
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^:,{])"([^:,}])/', "$1".'\''."$2",$json);
echo htmlspecialchars($json);
//ouput: {"en":"<b class='test' size='5" >Description</b>"}
Needed result:
{"en":"<b class='test' size='5' >Description</b>"}
I hope this works as expected ([^{,:])"(?![},:])
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^{,:])"(?![},:])/', "$1".'\''."$2",$json);
Results in
{"en":"<b class='test' size='5' >Description</b>"}
Try this one : str_replace('"', "'",$json);
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=str_replace('"', "'",$json);
echo htmlspecialchars($json);
Output will : {'en':'<b class='test' size='5' >Description</b>'}

Trouble with php using quotes, double quotes and backslash

I've been hours trying to figure out how to solve one thing, first I had this that works:
echo "<p>$valor[nombre_categoria]
<input type='button' value='modifica'
onclick='location.href=\"mod_cat.php?categ=\""
,'</p>\n";
And then I tried to send with the link a variable but I can't figure it out how to use the quotes, double quotes and backslashes.
echo "<p>$valor[nombre_categoria]<input type='button' value='modifica'
onclick='location.href=\"mod_cat.php?categ=".
$valor[nombre_categoria]."\'</p>\n";
I'm sure the solution its easy but I cant figure it out thanks for reading
This works for me:
$valor['nombre_categoria'] = "hello";
echo "<p>{$valor['nombre_categoria']}
<input type='button' value='modifica'
onclick=\"location.href='mod_cat.php?categ={$valor['nombre_categoria']}'\"></p>\n";
or this:
echo "<p>".$valor['nombre_categoria']."
<input type='button' value='modifica'
onclick=\"location.href='mod_cat.php?categ=".
$valor['nombre_categoria']."'\"></p>\n";
outputs: (line break added for readability here)
<input type="button" value="modifica"
onclick="location.href='mod_cat.php?categ=hello'">
When you use double quotes, you have to use { and } :
echo "Hello {$foo['bar']}";
You can skip { and } if your variable is "simple" :
echo "Hello $foo";
In my opinion, it's always better to use concat :
echo 'Hello '.$foo;
Regards
I highly recommend you break your variables out a quote your array elements
echo '<p>' . $valor['nombre_categoria'] . '<input type="button" value="modifica" onclick="location.href=\'mod_cat.php?categ=' . $valor['nombre_categoria'] . '\'"/></p>' . "\n";
Easier to read

Parsing an echo in PHP

I have what is, for me, quite a complex line I want to parse into php.
echo '<input class="cbox" onclick="if(this.checked){document.getElementById('school').style.display ='inline'}else{document.getElementById('school').style.display ='none'}" type="checkbox" name="type[]" value="School" checked/><br>';
Obviously, if I use speech marks to enclose the echo, it would fall apart in the onlick event. How can I avoid this?
To escape characters in php use the \ like so:
echo '<input class="cbox" onclick="if(this.checked){document.getElementById(\'school\').style.display =\'inline\'}else{document.getElementById(\'school\').style.display =\'none\'}" type="checkbox" name="type[]" value="School" checked/><br>';
You need to escape your quotes with \"
Like this:
echo "<input class=\"cbox\" onclick=\"if(this.checked){document.getElementById('school').style.display ='inline'}else{document.getElementById('school').style.display ='none'}\" type=\"checkbox\" name=\"type[]\" value=\"School\" checked/><br>";
Add slashes before '.
echo '<input class="cbox" onclick="if(this.checked){document.getElementById(\'school\').style.display =\'inline\'}else{document.getElementById(\'school\').style.display =\'none\'}" type="checkbox" name="type[]" value="School" checked/><br>';
If you use double-quotes, you can escape any other double-quotes by putting the \ character before them.
There are some more details at: http://php.net/manual/en/language.types.string.php
NEVER use echo to output HTML.
But always type it as is
?>
<input class="cbox"
onclick="if(this.checked){
document.getElementById('school').style.display='inline'
}else{
document.getElementById('school').style.display ='none'}"
type="checkbox" name="type[]" value="School" checked/><br>

PHP Echo back a form inside of a table cell

Here is how I am echoing back a 'normal' table cell in PHP:
Code:
echo "<tr>";
echo "<td>Some cell data</td>";
echo "</tr>\n";
Attempt to echo a button within a cell:
echo "<tr>";
echo "<td><form action="insertdata.php?page=add" method="post">
Username: <input type="text" name="username" >
<input type="submit" >
</form>
</td>";
echo "</tr>\n";
Thank you for any help, this is quite confusing to me. I am a beginner when it comes to PHP so please pardon any errors and feel free to kindly correct me wherever a correction is needed!
You need to either escape your double quotes " that appear inside your string surrounded by double quotes, or use a different quote style (i.e. single quotes ') to surround the string that contains the double quotes. It's actually made quite obvious what the problem is by the syntax highlighting on this site.
You could either do this:
echo "<tr>";
echo "<td><form action=\"insertdata.php?page=add\" method=\"post\">
Username: <input type=\"text\" name=\"username\" >
<input type=\"submit\" >
</form>
</td>";
echo "</tr>\n";
...or this...
echo "<tr>";
echo '<td><form action="insertdata.php?page=add" method="post">
Username: <input type="text" name="username" >
<input type="submit" >
</form>
</td>';
echo "</tr>\n";
I suggest you read this thoroughly so you no what you can and can't legally do with strings in PHP.
As a side note, it is a bad idea to put new-line literals inside quoted strings, as it can lead to cross-platform compatibility wierdness. It's not really a problem with web output like this, but it's a bad habit to get into. You should either use \r,\n and \r\n as appropriate, or if you must put literals into the source code, use heredoc syntax. Remember that escape sequences like \r\n are only interpolated in double quoted strings!!!

unable to get my window.location to work within echo tag

echo "<form><input type='button' value='$back_label' onclick='window.location="'$url'"'/></form>";
I cant figure the whole single and double quotes thing when it comes to the window.location code because it has an extra set of single quotes to wrap around the url. I have no idea what to do. I tried escaping the quotes.
Also, can you use a relative path for this method?
Thanks
Try this
echo "<form><input type='button' value='$back_label' onclick='window.location=\"$url\"'/></form>";
A working example on http://codepad.org/K7AafokT
Can you take it out of the PHP context?
<?php $url = 'http://www.yourdomain.com'; ?>
<form>
<input type='button' value='<?php echo $back_label;?>' onclick='window.location="<?php echo $url;?>"'/>
</form>
Just change the quotes at the end to onclick='window.location="$url"'/>
echo "<form><input type='button' value='$back_label'
onclick='window.location="$url"'/></form>";
I believe to meet the HTML 'standard' the almost all attributes must use double quotes and for javascript you need to encapsulate the url so:
echo '
<form>
<input type="button" value="'.$back_label.'" onclick="window.location=\''.$url.'\'" />
</form>';
EDIT
A cleaner way to code this is to use heredoc syntax as it eliminates the need for escaping:
echo <<<EOL
<form>
<input type="button" value="$back_label" onclick="window.location='$url'" />
</form>';
EOL;
UPDATE
You are able to go down a directory structure, and I just did a quick test and it appears to work going up the hierarchy as well.

Categories