R9102
R9102

Reputation: 727

How to Keep the data that user entered in textbox even after submit button is clicked and even if page is reloaded

I want to keep the data in a text box after submit and inserted into db. even if i refresh the page data should not change or empty

here is my code

<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_admin"><?php _e('Collaboration Admin', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_admin" name="approved_admin" type="email"  value="<?php echo esc_attr($item['approved_admin'])?>"size="50" class="code" placeholder="<?php _e('Admin Email', 'custom_table_example')?>"  required>
</td>
</tr>

if i do this data will be there if i reload empty text box will be shown.I need to make this readonly and onload same text should be there.

Form

<form id="form" method="POST">
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
<?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
<input type="hidden" name="sid" value="<?php echo $item['sid'] ?>"/>
<div class="metabox-holder" id="poststuff">
<div id="post-body">
<div id="post-body-content">
<?php /* And here we call our custom meta box */ ?>
<?php do_meta_boxes('Settings', 'normal', $item); ?>
<input   type="submit" value="<?php _e('Save', 'custom_table_example')?>" id="submit" class="button-primary" name="submit"  / >
<input   type="submit" value="<?php _e('Edit', '')?>" id="edit" class="button-primary" name="edit"  / >
</div>
</div>
</div>
</form>

I tried doing

<input type="email" name="approved_admin" value="<?php if (isset($_POST['approved_admin'])) { echo $_POST['approved_admin']; }  ?>">

and also

<input type="text" name="country" value="Norway" <?php isset($approved_admin) ? echo 'readonly' : ''; ?> >

Upvotes: 1

Views: 1465

Answers (2)

Kumar Rakesh
Kumar Rakesh

Reputation: 2708

You can save data after page reload a lot of ways like... Session , cookie, get , post. After this value get by isset and echo .

In your case Post method used..

Try this working fine all thing

<input type="text" name="country" value=" <?php echo isset($_POST['country']) ?  $_POST['country'] : ''; ?>" <?php echo isset($_POST['country']) ?  'readonly' : ''; ?> >

You just put field name in isset();

Upvotes: 1

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

You can use this syntax. Have a try for this.

<input type="text" id="to_date" name="to_date" class="form-control" placeholder="Enter End Date" value="<?php echo isset($_POST['to_date']) ? $_POST['to_date'] : '' ?>" />

The name of the input type has to be mentioned in the isset() condition.

Upvotes: 0

Related Questions