Forum Moderators: coopster

Message Too Old, No Replies

php form

need to retain user input after submit

         

marra1

1:12 am on Dec 21, 2004 (gmt 0)

10+ Year Member



I have a php form that uses PHP_SELF. When the submit button is clicked, all the information is processed no problem. But I would like to have the data that the user typed to still be there so they can edit thier search easily without having to redo all 6 input fields. Is their a way that I can do this?

Thanks in advance.

orion_rus

8:53 am on Dec 21, 2004 (gmt 0)

10+ Year Member



It seems not Javascript question, but i suggest you to see the following:
Then u submits form, in this php make something like this:
<input type="text" name="data" value="<? echo $_POST['data'];?>"> and if u sumbit a form data would be filled, if u there in a first time there would be nothing)
good luck to you

marra1

12:13 am on Dec 22, 2004 (gmt 0)

10+ Year Member



Thanks, that works great! Do you know how I can get it to work in a drop down? I tried it the same way and it didn't work.

Thanks,

dreamcatcher

3:37 am on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi marra1,

With a drop down menu, you need to determine which option was selected and echo it as the 'selected' value. A ternary operator [php.net] is useful here.

For example, say your drop down menu is:

<select name="options">
<option>Option One</option>
<option>Option Two</option>
<option>Option Three</option>
</select>

You need to change your code to this:


<select name="options">
<option<?php echo (($_POST['options']=="Option One")? " selected " : "");?>>Option One</option>
<option<?php echo (($_POST['options']=="Option Two")? " selected " : "");?>>Option Two</option>
<option<?php echo (($_POST['options']=="Option Three")? " selected " : "");?>>Option Three</option>
</select>

Hope that helps.

dc :)

Warboss Alex

9:22 am on Dec 22, 2004 (gmt 0)

10+ Year Member



Or do what I do:

<select name="option_button">
<?php
if (isset($_POST['option_button'])) echo '<option selected>'.$_POST['option_button'])).'</option>';
?>
<option>1</option>
...
<option>n</option>

That way, you don't have to check every option.

dreamcatcher

12:24 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Alex, a good alternative. :)

Warboss Alex

1:03 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



:)

's what I always use.

marra1

6:32 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



I can not get it to work. I tried it both of the ways you suggested and neither way retains the info.

here is what I used.

<select name="searchBy">
<option value="city" <?php echo (($_POST['searchBy']=="City")? " selected " : "");?>>City</option>
<option value="lp" <?php echo (($_POST['searchBy']=="Price")? " selected " : "");?>>Price</option>
<option value="br" <?php echo (($_POST['searchBy']=="Bedrooms")? " selected " : "");?>>Bedrooms</option>
<option value="dom" <?php echo (($_POST['searchBy']=="Days on Market")? " selected " : "");?>>Days on Market</option>
<option value="sf" <?php echo (($_POST['searchBy']=="Square Feet")? " selected " : "");?>>Square Feet</option>
<option value="lpsf" <?php echo (($_POST['searchBy']=="Price per Square Foot")? " selected " : "");?>>Price per Square Foot</option>
</select>

Please tell me what I am doing wrong?

Thanks.

marra1

9:37 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



Ok. tinkered with it and got it to work!

Thanks for the great information.

scrubbie1

3:14 am on Jan 11, 2005 (gmt 0)

10+ Year Member



Hi, I found this thread, and it relates to a problem I'm having. I set up my drop down to be "sticky" using the method described by Alex. Once the user submits the form, they are directed to another page called "submitted.php" where I want to echo the submitted information. Everything works except the menus that were dynamically populated from mysql.

Here is the code for the menu:
<td> <?

$sql="SELECT user_id,last_name,first_name FROM personneldemo WHERE position='sales'ORDER BY last_name";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

$user_id=$row["user_id"];
$last_name=$row["last_name"];
$first_name=$row["first_name"];
$name = $last_name.", ".$first_name;
$options.="<OPTION VALUE=\"$id\">".$name;
$options .= (isset($_POST['menu1']))? "selected=\"selected\"" : "";
}
?>

<SELECT NAME="menu1">
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT>
</td>

Here is the part of the code for the submit page that is supposed to echo this value:

<?
echo <p> Name: $menu1</p>;
?>
menu1 is the <select name=menu> from the drop down. But, I think I actually need to echo the option that is chosen, and I don't know how to do that.

Any help appreciated.

Warboss Alex

11:01 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



$options.="<OPTION VALUE=\"$id\">".$name;

Shouldn't $id be $user_id?

And if you're not getting any output, are you sure that the query is returning information?

scrubbie1

11:47 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



Alex,
THANK YOU! It should be user_id, and I bet that's why its not working, since I just copied the script and all the menus are the same. I'm gonna go see if that fixes it. I know the query is returning info cuz all the other fields populate, just the ones from those menus weren't working. That's what happens when you're trying to do code at 2 am, I guess. =D

Cori