c.k c.k - 24 days ago 8
HTML Question

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..

Answer

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>
Comments