Putting the value of a PHP variable into a form control

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sheridan
Forum Newbie
Posts: 18
Joined: Sat May 31, 2008 1:50 pm

Putting the value of a PHP variable into a form control

Post by Sheridan »

Hi:
I am a newcomer to PHP. I am trying to put the value in a PHP variable into the value of a form control. The code I have tried is:

***** PLEASE USE THE CODE AND PHP TAGS *****

Code: Select all

 $row = mysql_fetch_row($result);
    $str = $row[1];
    $str .= ", ";
    $str .= $row[2];
    echo '<form action="testSendQueryData.php" method="get">';
    echo '<br>';
    echo '<input type="text" name="iName"  value = $str>';
    echo '<input type="submit">';
The code:

Code: Select all

  "value = $str" 
does not work. The form sends the string "$str". I have also tried:
  echo '<input type="text" name="iName"  value = value = <?php echo "$str;" ?>>
Anyone know a solution? :cry:
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Putting the value of a PHP variable into a form control

Post by nowaydown1 »

Code: Select all

 
printf("<input type=\"text\" name=\"iName\" value=\"%s\" />", $str);
 
or

Code: Select all

 
<input type="text" name="iName" value="<?php print($str); ?>" />
 
Sheridan
Forum Newbie
Posts: 18
Joined: Sat May 31, 2008 1:50 pm

Re: Putting the value of a PHP variable into a form control

Post by Sheridan »

Hi:
The printf() function did the trick. Thanks a million. :D
LSJason
Forum Commoner
Posts: 45
Joined: Mon May 12, 2008 4:43 pm

Re: Putting the value of a PHP variable into a form control

Post by LSJason »

For future reference, you can do this:

Code: Select all

 
  $row = mysql_fetch_row($result);
    $str = $row[1];
    $str .= ", ";
    $str .= $row[2];
    echo '<form action="testSendQueryData.php" method="get">';
    echo '<br>';
    echo '<input type="text" name="iName"  value = '.$str.'>';
    echo '<input type="submit">';
 
Post Reply