c.k
c.k

Reputation: 1105

Data or value retention of textarea and select input

When submitting a form via $_POST, the input file value remains. Like this:

    if($_POST){
    $error = false;
            if(!$post['price']){    
                $error = true;
                $error_note['price'] = "*Should not be empty";
            }    
    if($error == false){
    //submit/save the data
    }
}



 <form name="post" id="post" method="post"  action="">
             <input name="price" value="<?=$_POST['price']?>" class="text-input" type="text" maxlength="15">
            <select name="price-type"  class="text-input">
                                <option value="" selected>Select price type</option>
                                <option value="item">Item</option>
                                <option value="kilo">Kilo</option>
                                <option value="rate">Rate</option>
            </select>

           <textarea class="description" name="description" cols="55%" rows="6"></textarea>

        <button class="button" type="submit" name="submit-btn" >SUBMIT</button>
    </form>

But I have textarea and select input on my form.

How to retain the content of textarea and selected item on select input? value="<?=$_POST['price']?>" doesn't work on it..

Upvotes: 0

Views: 165

Answers (2)

Md Hasibur Rahaman
Md Hasibur Rahaman

Reputation: 1061

Check this it may help you

 <form name="post" id="post" method="post"  action="">
     <input name="price" value="<?=isset($_POST['price']) ? $_POST['price'] : ''?>" class="text-input" type="text" maxlength="15">
     <select name="price-type"  class="text-input">
         <option <?= (isset($_POST['price-type']) && $_POST['price-type']=='') ? 'selected' : '' ?>  value="" >Select price type</option>
         <option <?= (isset($_POST['price-type']) && $_POST['price-type']=='item') ? 'selected' : '' ?> value="item">Item</option>
         <option <?= (isset($_POST['price-type']) && $_POST['price-type']=='kilo') ? 'selected' : '' ?> value="kilo">Kilo</option>
         <option <?= (isset($_POST['price-type']) && $_POST['price-type']=='rate') ? 'selected' : '' ?> value="rate">Rate</option>
     </select>
     <textarea class="description" name="description" cols="55%" rows="6"><?=isset($_POST['description']) ? $_POST['description'] : '' ?></textarea>
    <button class="button" type="submit" name="submit-btn" >SUBMIT</button>
 </form>

Upvotes: 1

Passionate Coder
Passionate Coder

Reputation: 7294

You have used short tag but note that your short tag should also open for this

I suggest always use php tag

value="<?php echo $_POST['price']; ?>"

To set Short tag open do this

short_open_tag=On  //in php.ini

And restart your Apache server.

Upvotes: 0

Related Questions