Putting a PHP variable as a text box value [closed

2019-06-06 00:46发布

I am trying to write code that passes a php variable as a textboxes value. Here is the following code I tried:

echo "<td>"."<input type='text' value='<?php echo $start_date; ?>'/>"."</td>"

This brings up an error : Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';'

I have tried various methods of re-wording:

 echo "<input type='text' value='<?php echo $start_date?>'/>"; 

(this was purely to test as I would like the result in a table row)

but this shows:

<?php echo ?> 

in the textbox and I also get this error: Notice: Undefined variable: start_date...

Thanks in advance for any help.

5条回答
何必那么认真
2楼-- · 2019-06-06 00:54

You can try this-

echo "<td>"."<input type='text' value='$start_date;'/>"."</td>";
查看更多
仙女界的扛把子
3楼-- · 2019-06-06 01:06

You don't need to start one echo within another. The variable $start_date can be within double quotes and hence interpolated.

echo "<td><input type='text' value='$start_date'/></td>"; //no unnecessary concatentation

EDIT:

In case of an associative array, for example, to echo $row['start_date']

echo "<td><input type='text' value='".$row['start_date']."/></td>";
查看更多
贼婆χ
4楼-- · 2019-06-06 01:10

Use this:

echo '<td><input type="text" value="'.$start_date.'"/></td>';
查看更多
乱世女痞
5楼-- · 2019-06-06 01:12

you have to echo once, for example:

echo "<td>"."<input type='text' value='$start_date'/>"."</td>";
查看更多
Juvenile、少年°
6楼-- · 2019-06-06 01:14

You should always took thin in your mind that if you enclose a variable inside "" double quotes than it can be echoed directly. You don't need use concatenation for this only. Eg

echo "<td>$rohit</td>"; //here you dont have to echo this like "<td>".$rohit."</td>".

In this as @Cthulhu said use it like that.

查看更多
登录 后发表回答