Insert value into a text box which is inside an echo statement

19,301

Solution 1

Concatenation is a pretty easy way to go about this:

echo '<input required  type = "text" name = "subject1" value="' . $foo . '" />'

edit: Those spaces around the concatenation .'s (. $foo .) aren't required - I just add them fore readability.

Solution 2

I think the problem is you are using single quotes, so variables are not rendered automatically... you can concat with . like this:

echo 'some text' . $variable . 'some more text';

Solution 3

echo '<input required type="text" name = "subject1 value="<?php echo $value; ?>" />';

acutally you don't need the php echo .... stuff.... /* brain not engaged */

Share:
19,301
H Bellamy
Author by

H Bellamy

Merge keep

Updated on June 14, 2022

Comments

  • H Bellamy
    H Bellamy almost 2 years

    I have this code basis:

    echo '<input required  type = "text" name = "subject1" value="XXX" />';
    

    However, I would like XXX to be a variable, I have searched this online, but all the things that one of the discussions says is to do this:

    value=\"$firstName\" 
    

    or

    value='$firstName'
    

    I have tried both of these, but they don't work, and I was hoping that someone could help me with this problem, basically, what I want is to be able to asign the value of a text edit to a variable in php, but when the text edit itself, is embedded in an echo, nothing seems to work.

    Thanks

  • H Bellamy
    H Bellamy over 12 years
    so, should I generally use the double quotes?
  • Steve Adams
    Steve Adams over 12 years
    @JBellamy It's both a matter of taste and dependent on what you're doing. If you have anything relatively complex to do, mixing strings with variables, you should really look into sprintf(). Otherwise you'll rarely find a case where concatenation or double quotes are significantly superior to each other. Double quotes are nice in cases such as echo "My name is $name, I'm $age years old!";, but when strings contain a lot of double quotes already, I find it gets ugly.