Putting a PHP variable as a text box value [closed

2019-06-06 00:19发布

问题:

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.

回答1:

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>";


回答2:

You can try this-

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


回答3:

you have to echo once, for example:

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


回答4:

Use this:

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


回答5:

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.