tags:

views:

134

answers:

4

Hello all,

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

A: 

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

Daman
I will consider that on a multi-step form. ;) Thanks for making the connection between the use of $_SESSION and the multi-step forms.
MEM
+1  A: 

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?

pleasedontbelong
Not sure if it's a proper answer. Really. I do agree. But my agree isn't enough. However, it's only what we have.
MEM
+1  A: 

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.

Garis Suero
Do you believe that those kind of verifications should stay outside our html form elements?
MEM
Is <?= some sort of shortcut ? Is like an echo? SOrry, can't google that. I don't know the name and google "php <?=" is useless. :/
MEM
yeah, is more legible that way. And yes, to your question.
Garis Suero
I see. I have hear here and there, that it's not a good practice to use shortcut echos. We should use <?php echo instead. I will considerer make the separation however.
MEM
If fieldA isn't defined, on top, will not the value attribute give undefined or something? No because we are telling, to actually fill it with an empty string... hmm... yes? :D
MEM
nah, will be blank anyway... but is a good practice to set it blank manually when the value is null...
Garis Suero
I need to read more about those diferences between null and '' etc... :/ Still so much to learn... oh well. Thanks a lot.
MEM
+2  A: 

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

Col. Shrapnel
Col. Shrapnel: Right now, I do have a form with two submit buttons. The first it's called: getDomainInfo and the other: saveDomainInfo. 1) The user will input a domain name; 2) the getDomainInfo will grab the hostAddr and Ns of that domain, and put them on another input fields; 3) Once they are on the input field, we can change them. 4) After we change those values, ALL will be used, and we will submit all this values. Do you still believe that GET and two forms are the proper way for doing so? If so, why?Thanks a lot. MEM
MEM
@MEM sure that's exact solution in this case. usual and natural.I did it many times this way. What's wrong you see with it?
Col. Shrapnel
To answer why is simple. I's going from GET and POST methods purpose. GET is used for *retrieving* data and POST - for saving. Thus, GET for retrieve and POST for save. I have just one question. Why do you let to edit a domain name? Shouldn't it be a read-only value? What's the purpose of editing domain name?
Col. Shrapnel
@MEM domain name is a record identifier. It is very usual to use such identifier in the address bar.
Col. Shrapnel
I will not edit the domain name. I will edit the ns address associated to that domain name and the corresponded IP.So, we should use POST each time,for example, we want to save data into a database Ok. The GET we can use to retrieveok.For both submission, since on both I want to retrieve some data, GET should be used.RestFul(has something to do with this?).The only reason I'm scratching my nose, it's because, I'm thinking that, by doing it, I will need more lines of code to accomplish the same.Any example, so that I can look at? Need to better understand what changes will that imply here...
MEM
Weird English grammar to say the least. Sorry. O.o
MEM
@MEM you will understand it better if make it with 2 files first (later you may combine them in one).
Col. Shrapnel
@MEM ...or even 3 as shown above
Col. Shrapnel
Ok... I will study it. But since both work, the reason why we use GET instead of POST, relies on the http protocol considerations that we should be aware of and respect - is it? Just wondering...Actually, the first submission we need to GET information. True. But the second one, once we get that information, must: a) save the data into a database; b) send a XML command to a EPP server. So, on the second case, I'm saving information, hence, POST should be used there. No? So, GET and then POST perhaps?I'm also using value objects. (what's a newbie doing with that? I ask myself that question)
MEM
@MEM look that's easy. When we need to know, what page to show, we use GET method. Like this very page. it has this question identifier in it's address. It's very handy. you can bookmark it, reload it, whatever. If we use post and try to reload, it will as us thousand stupid questions. That's why its GET.
Col. Shrapnel
yes, GET and then POST, you got it right. POST has no limit on content length and just designed to save data.
Col. Shrapnel
@Col. Shrapnel - that simple relation between GET, and the fact that we should decide if we want the page to available later as a direct link seems quite interesting to think about. Thanks. :)I would just love to have some literature about this subject to learn more about it. :D:D:D
MEM
Col. Shrapnel
@MEM as for this decision, there is another issue - an authorization. You can see your user info here on SO, and it uses GET method as usual. But if you log out from SO, you wont see your info. While it's still GET! Same for deleted question. It's still on the site but you have no access to it.
Col. Shrapnel
I will properly read. And see if I can get some semantic way of find it, on what circumstances should we use one, or another. Thanks a lot for the reference. And for your patience. :)
MEM