How do I set the value of a form object from PHP

Discussion in 'PHP' started by Imozeb, Feb 9, 2010.

  1. #1
    How do I set the value of a form text field using PHP? I have created a PHP script to validate my form and when it is finished it refreshes the page to output error messages on user mistakes but when it refreshes the user loses all her imput. How do I prevent this? Mabye by setting the form text fields to their old values through variables...? but I don't know how...

    Thanks to whoever can help!

    ~imozeb :)
     
    Imozeb, Feb 9, 2010 IP
  2. DEWebDev

    DEWebDev Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The easiest way to do it would be to just echo the appropriate variable in the HTML code of each text field. For example:

    <input type="text" name="email" value="<? echo $_POST['email']; ?>">
    PHP:
    What happens is this: If the form hasn't been submitted, the value will be blank, so the text field will be empty. If it has been submitted but there are errors and the form can't send, the value of the text field will be what the user had entered so he/she doesn't have to enter the information again.

    Hope this helps.
     
    DEWebDev, Feb 9, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks DEWebDev. It worked! Is there any way I can do the same with textareas and/or dropdown select menus?

    ~Imozeb :)
     
    Imozeb, Feb 9, 2010 IP
  4. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    you can also do it like this : <input type="text" name="email" value="<?= $_POST['email'] ?>">

    Just do it the same way to other fields.
     
    blacksheep666, Feb 9, 2010 IP
  5. DEWebDev

    DEWebDev Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    For textareas, it should be easy. Just put at php echo statement between the <textarea> and </textarea> tags. For example:

    <textarea name="message"><? echo $_POST['message']; ?></textarea>
    PHP:
    For dropdown menu's, it gets a little more complicated. You need to use an IF statement for each option to determine which option the user has selected, and then echo "selected" so the user doesn't have to choose it again. For example:

    <select name="groceries">
      <option <? if ($_POST['groceries'] == "Milk") echo "selected"; ?> value="Milk">Milk</option>
      <option <? if ($_POST['groceries'] == "Bread") echo "selected"; ?> value="Bread">Bread</option>
      <option <? if ($_POST['groceries'] == "Eggs") echo "selected"; ?> value="Eggs">Eggs</option>
    </select>
    PHP:
    Hope this helps.
     
    DEWebDev, Feb 9, 2010 IP
  6. ajith107

    ajith107 Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,
    for Dropdown try like this.
    <?php
    //if form submitted
    $item=$_POST['groceries'];
    //else not submited
    $item="Milk";//set the default selected item for your case
    ?>
    write a JS function
    function set()
    {
    var item="<?php echo $item;?>";
    document.frm.groceries.value=item;
    }
    <body onLoad="set()">
    ......
    </body>
    Try this
     
    ajith107, Feb 9, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thread resloved!

    Thanks DEWebDev your code worked!

    ~Imozeb
     
    Imozeb, Feb 10, 2010 IP