Trouble with php using quotes, double quotes and backslash - php

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

Related

Too many quote marks - How would I echo this without closing them up

Basically I want to have my spoiler thingy inside an echo, but can't get it to work due to the quotes marks confusing me, hah.
echo "<input class='spoilerbutton' type='button' value='Register' onclick='this.value=this.value=='Register'?'Cancel':'Register';'><div class='spoiler'><div>woooohoo hide this text</div></div>";
As you can see by the 'register'?'cancel' part, there are quotes that gets closed by one another.
How could I fix this the most simple way? I'm getting too confused, lol.
Don't put HTML inside PHP strings if you can help it. Turn it inside out.
if (somecondition) {
?>
<input
class='spoilerbutton'
type='button'
value='Register'
onclick="this.value=this.value=='Register'?'Cancel':'Register';">
<div class='spoiler'>
<div>woooohoo hide this text</div>
</div>
<?php
} else {
}
For that matter, don't put JavaScript inside HTML attributes if you can help it.
<input
class='spoilerbutton'
type='button'
value='Register'>
<script>
var input = document.querySelector('input.spoilerbutton')l
input.addEventListener('click', toggleValue);
function toggleValue(evt) {
this.value=this.value=='Register'?'Cancel':'Register';
}
</script>
You can escape the quotation marks like so
echo "<input class='spoilerbutton' type='button' value='Register' onclick='this.value=this.value==\"Register\"?\"Cancel\":\"Register\";'><div class='spoiler'><div>woooohoo hide this text</div></div>";

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!!!

Escaping a complicated string with PHP

I have a string like this:
<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
<?php
echo $var;
?>
</form>
I want to echo it out with PHP, and have it look exactly as above.
I started with this:
echo '<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
<?php
echo $var;
?>
</form>';
But the additional single quotes inside are causing me problems. How can I have the exact verbatim output of my string print (so that no variables are parsed and no code is run)?
Normal Escaping would look like this:
echo '<form action=\'php/zoneNotifUnsub.php\' id=\'zoneNotifUnsub\' method=\'POST\'>
<?php
echo $var;
?>
</form>';
Escaping every one of the ' with a backslash. To recap how strings work look in the manual page
You could also go with the HEREDOC Syntax that would look a little bit nicer:
echo <<<OUT
<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
<?php
echo $var;
?>
</form>
OUT;
You can also use double quotes for the things you want to echo.
So: '<form action="php/zoneNotifUnsub.php" id="zoneNotifUnsub" method="POST">'; and so on.
Try escaping them
echo '<form action=\'php/zoneNotifUnsub.php\' id=\'zoneNotifUnsub\' method=\'POST\'>
<?php
echo $var;
?>
</form>';
Using double quotes could be an option in this case:
echo "<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
<?php
echo $var;
?>
</form>";

Syntax problem with quotes in PHP and HTML

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;

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