forms value and php echo stops after first space

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    forms value and php echo stops after first space

    The title is maby a bit strage, I had problem coming up with a title
    that explain my problem.

    I have a variable $myvar="TEST 123"

    I would like to use this variable as a default value in a form. I have
    tryed this code:

    <input name="testbox1" type="text" value=<?php echo "$myvar"; ?>>
    the result is a form with a textbox with the default value "TEST"

    I have tryed

    <input name="testbox2t " type="text" value=<?php echo "TEST 123"; ?>>
    Same as before the default value is only "TEST"

    But if I try

    <input name="testbox 3t" type="text" value= "TEST 123t">
    the default value is "TEST 123"

    all my test code is inside the <form> </form> statement
  • jan.hancic@gmail.com

    #2
    Re: forms value and php echo stops after first space

    All text values must be inside " "
    If the parser comes and sees this:

    value=TEST 123

    he will take TEST but he will think 123 is another attribute of the
    tag.

    The best way would be this:

    <input name="textbox2" type="text" value="<?php echo $myvar ?>" />

    Comment

    • John Dunlop

      #3
      Re: forms value and php echo stops after first space

      Somebody:

      [$myvar='TEST 123';]
      [color=blue]
      > <input name="testbox1" type="text" value=<?php echo "$myvar"; ?>>
      > the result is a form with a textbox with the default value "TEST"[/color]

      You are obviously aware that the value must be within quotes in the
      HTML, but if you look at the HTML source you'll see the value is not
      quoted, which causes the value to be cut off after 'TEST'. PHP
      interprets what's after echo here as a string delimited by double quote
      marks, that string being the variable $myvar, and only the value of
      that string, not the quote marks as well, is echo-ed.

      Two options. Either include the quotes in the echo-ed string (there
      are various ways) or include them directly in the markup. Given the
      advice to always quote attribute values, the latter seems more
      appropriate.

      --
      Jock

      Comment

      • Guest's Avatar

        #4
        Re: forms value and php echo stops after first space

        Thanks to both of you. The solution was so obvious that I feel almost
        like an idiot not seeing it myself.

        Comment

        • william.clarke@gmail.com

          #5
          Re: forms value and php echo stops after first space

          If you had viewed the source from the browser you may have spotted what
          it was doing, I find the old "View Source" option great for debugging
          variable display code and other similar areas in my scripts.

          Comment

          Working...