user3258603
user3258603

Reputation: 169

How to Preserve the input Value in HTML <input>

How can i possibly preserve the value of the <input> field if submitted wrong? I have a form that will be submitted to a php page. The PHP page will then check if that is correct or not.

Ex. if the user input google the PHP page says it is wrong. I want google sent back to the input field.

Upvotes: 0

Views: 1264

Answers (4)

krishna
krishna

Reputation: 4099

use $_POST[fieldname] like this

<input type="text" name="name" value="<?php if(isset($_POST['name'])) echo strip_tags($_POST['name']);  ?>"/>

Upvotes: 0

AndrewB
AndrewB

Reputation: 836

If you have your input element:

<input id="myInput" name="myInput" value="<?php if(isset($_POST['myInput'])){ print $_POST['myInput']; } ?>" />

I believe this should work but if it doesn't let me know and I'll edit it.

Upvotes: 1

Snehal S
Snehal S

Reputation: 875

You can return the result and the value entered by user.

for ex if user input "google"

Then in your case return an array as

array('result' => 'error', 'val' => );

or it its right

array('result' => 'success', 'val' => );

with this array you can check whether the 'result' variable is success or error. And if it is an error then take the "val" value and put it inside your input

Upvotes: 0

BenM
BenM

Reputation: 53198

You can access the value using the $_POST or $_GET super-global. Depending upon the form submit method, you can do:

<input type="text" value="<?= (isset($_POST['field'])) ? strip_tags($_POST['field']) : '' ?>" name="field" />

If your method="get", you'll need:

<input type="text" value="<?= (isset($_GET['field'])) ? strip_tags($_GET['field']) : '' ?>" name="field" />

Upvotes: 2

Related Questions