Page 1 of 1

Putting the value of a PHP variable into a form control

Posted: Sat May 31, 2008 2:13 pm
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:

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

Posted: Sat May 31, 2008 2:43 pm
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); ?>" />
 

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

Posted: Sat May 31, 2008 3:14 pm
by Sheridan
Hi:
The printf() function did the trick. Thanks a million. :D

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

Posted: Sat May 31, 2008 9:26 pm
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">';