-3

trying to get the html code working by using echo,

this works as html:

<input type="text" name="ordernum" value="<?= isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '' ?>" />

but when I escape the value with backslash ( i have tried dozens of combinations and read a lot on stackoverflow,but still cant fix it) I get unexpected T_STRING errors .

echo ' <input type="text" name="ordernum" value=\'= isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '' \' />';
botch
  • 15
  • 5

5 Answers5

0

As Quentin has commented:

You can't just write arbitrary PHP code in the middle of a string literal

It's because of the $_POST['ordernum'] part.

Re-write it like this:

$val = isset($_POST["ordernumW]) ? htmlspecialchars($_POST["ordernum"]) : "";

echo "<input type='text' name='ordernum' value='$val' />";
Community
  • 1
  • 1
Albzi
  • 15,431
  • 6
  • 46
  • 63
0

You cant use arbitrary code in a string literal. First assign the value in one variable and use that variable in echo statement.

$orderNum = isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '';
echo ' <input type="text" name="ordernum" value="'.$orderNum.'" />';
Manibharathi
  • 945
  • 6
  • 18
  • 1
    A ternary condition is perfectly valid in a string definition. You should just remove the `=` and `?>` tags and properly concatenate ;-) – svvac May 19 '14 at 13:30
  • 1
    @user3556674 ok and accept this answer and close this question. – Manibharathi May 20 '14 at 02:26
0

You need to escape with \ the characters that may end your string (i.e. specifically string delimiters " and ').

Those two lines work:

echo "<input type=\"text\" name=\"ordernum\" value=\"" . (isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '') . "\" />";
echo '<input type="text" name="ordernum" value="' . (isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '') . '" />';
svvac
  • 5,814
  • 3
  • 17
  • 22
  • as already said [here](http://stackoverflow.com/questions/23738996/echo-a-form-input-with-backslashes#comment36492633_23738996) you cannot use arbitrary condition inside a string – krishna May 19 '14 at 13:33
  • This code is perfectly valid. The ternary condition evaluates to either the HTML-escaped version of `$_POST['ordernum']` or the empty string, which is concatenated to the first part. You can't use a `if` statement in a string definition, but there's nothing preventing you from concatenating literals, function results or ternary conditions. – svvac May 19 '14 at 13:37
0

This is what it should be:

echo('<input type="text" name="ordernum" value="'.(isset($_POST['ordernum'])?htmlspecialchars($_POST['ordernum']):'').'">');

You use a full-stop to concatenate strings, so you should be ending the first portion of the string, adding a full-stop, then the dynamic value, another full stop, then the remainder of the string.

So in you're string, what you're doing wrong is here:

value=\'= isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '' \'

Like this:

"first part of string".$myvariable."last part of string";

You only need to escape the quote type which the string is contained by also:

"I need to escape this \" but not this ' "
'I need to escape this \' but not this " '
Luke
  • 3,985
  • 1
  • 20
  • 35
0
$val = isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '';

echo '<input type="text" name="ordernum" value="'. $val. '" />';
Kanishk Dudeja
  • 1,201
  • 3
  • 17
  • 33