开发者

Is this a nice way to preserve a field value when an HTML form submitted?

I have a form with two submit buttons.

The user fills field-A and hits submit.

Done that, some input fields will be filled with data.

After that first submission, the value on the field-A should not disappear.

How can we preserve this value after the first submission?

Should we, on the field-A value attribute, place:

value开发者_高级运维="<?php echo isset($_POST['fieldA'])) ? $_POST['fieldA'] : ''; ?>" ?

The form submits to self.

Update - Additional details: This is a form that will have two submit buttons on the same page (sort of speak). Submit Button A - Will grab some data based on a input field, and fill the other input fields on that form. Submit Button B - Once the form is filled, it will use all that data to do another submission.

This is a very simple case, no frameworks are in place here. I do have, however, some sort of MVP structure here.

Thanks in advance, MEM


In general, such things being done using 2 forms, no one.
And GET method, not POST. At least for the first form.
But as you cannot ask a question, it's impossible to give you an answer.

Here you go:

index.php

<form action=edit.php>Enter name: <input name="name"><input type=submit></form>

edit.php

<? $row = dbget("row","SELECT * FROM domains WHERE name = %s",$_GET['name']); ?>
<form method="POST" action="save.php">
Edit data for <?=htmlspecialchars($row['name'])?>:</br>
NS: <input name="ns" value="<?=htmlspecialchars($row['ns'])?>"><br>
Another: <input name="another" value="<?=htmlspecialchars($row['another'])?>"><br>
<input type="hidden" name="name" value="<?=htmlspecialchars($row['name'])?>"><br>
<input type=submit>
</form>

save.php

do whatever you do usually to save info


I would store these values into $_SESSION, as user fabrik said. This way they can be stored across the entire form submission process(assuming it is multiple pages) and posted all at once at the end.

Assuming you're having some kind of submission system with a "next" button to go to the next set of forms, using session_start() and $_SESSION is certainly the best method. More information could be found here, or various tutorial sites--

http://php.net/manual/en/reserved.variables.session.php


It's ok to do that with $_POST, some people dont like the ternary operator but for me it works just fine. Although, there are better ways to deal with forms using O.O.P. You could create a class that manages your form, and pass an array to the constructor of that class (eventually you could pass the $_POST) and the class will create your form according to the info submited. You could even use the same class to valdidate your form

I don't see the need of using $_SESSIONS, cause this is not information that you need to preserve during the whole session.. or not?


Try this:

<?php
   $fieldA = (isset($_POST['fieldA']) ? $_POST['fieldA'] : '')
?>
// and in your form
<INPUT type="text" name="fieldA" id="fieldA" value="<?=fieldA?>" />

as you mentioned, this should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜