Populating an html form with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mlewis
    New Member
    • Sep 2008
    • 4

    Populating an html form with php

    Hello I wanted to follow on from http://bytes.com/forum/thread3085.html but the forum wouldn't let me post ... so ...

    I dont see the complete answer. If this is how to get variables from php into html text fields

    Code:
    $foo = "something from db"
    $bar = "something very long from db"
    
    ...
    <input type="text" name="short" value="$foo">
    <textarea name="long">$bar</textarea>
    How do you get the data into the variables?

    Do I start my html with say, this:
    Code:
    <FORM METHOD=POST ACTION="annuaire_populate.php">
    then in the php file:
    something like

    * connect to the database and table and then

    Code:
    $foo= 'SELECT * FROM $table';
    Thanks in advance
    Last edited by Markus; Oct 28 '08, 10:54 AM. Reason: added # tags
  • zabsmarty
    New Member
    • Feb 2007
    • 25

    #2
    hello,
    please change these
    Code:
    <input type="text" name="short" value="$foo">
    <textarea name="long">$bar</textarea>
    into

    Code:
    <input type="text" name="short" value="<?php echo $foo;?>">
    <textarea name="long"><?php echo $bar;?></textarea>
    Last edited by Markus; Oct 28 '08, 10:55 AM. Reason: added # tags

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by zabsmarty
      hello,
      please change these
      Code:
      <input type="text" name="short" value="$foo">
      <textarea name="long">$bar</textarea>
      into

      Code:
      <input type="text" name="short" value="<?php echo $foo;?>">
      <textarea name="long"><?php echo $bar;?></textarea>
      Zab, start using code tags please.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        I'm not sure I fully understand your problem.

        Are you trying to get data from your database into your form?
        If that is the case, then simply printing them into the HTML would do the trick.
        For example:
        [code=php]
        $db = mysqli_connect( "localhost" , "usr", "pwd", "test");
        $result = mysqli_query($d b, "SELECT data FROM table");

        echo '<form action="process Page.php" method="post">
        while($row = mysqli_fetch_as soc($result)) {
        echo '<input type='text' name='box[]' value='". $row['data'] .'" /><br />';
        }
        echo '<input type="submit" />
        echo '</form>';
        [/code]
        Which would send all the boxes into the "processPage.ph p" as an array.

        Or are you trying to get data from your HTML form into your PHP script?
        If that is the case, then check out this article.

        And please use [code] tags when posting your code examples!
        Thank you.

        Comment

        Working...