10 Replies - 7644 Views - Last Post: 26 March 2012 - 03:30 PM Rate Topic: -----

#1 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Need to retain drop-down value after form submission.

Posted 25 March 2012 - 07:21 PM

I am trying to make this drop down retain its value after submission, each time I
select a value, it goes back to the initial value after posting.

  <!DROP DOWN #2>
			<select name="item2" style="width: 250px">
				<option value="60">DAILY MAX TEMPS &ge; 60</option>
				<option value="70">DAILY MAX TEMPS &ge; 70</option>
				<option value="80">DAILY MAX TEMPS &ge; 80</option>
				<option value="90">DAILY MAX TEMPS &ge; 90</option>
				<option value="32">DAILY MAX TEMPS &le; 32</option>
				<option value="monthlyhighs">MONTHLY AVG MAX TEMPS</option>
				<option value="monthlylows">MONTHLY AVG MIN TEMPS</option>
				<option value="raindays">DAY OF RAINFALL PER MONTH</option>
				<option value="spring_occ">FIRST SPRING OCCURENCES</option>
				<option value="rainfall">MONTHLY/YEARLY RAINFALL</option>
				
			
				<!BUTTON #2>
            <input name="Submit2" type="submit" value="submit" style="height: 28px" />
		
		<?php  If( $i==($_POST['item2'])){?>
                  <option value="<?php echo $i?>" <?php if($i==$_POST['item2'])
				  echo 'selected="selected"'?>><?php echo $i?></option>
				  
                <?php }?>
            </select>  



Is This A Good Question/Topic? 0
  • +

Replies To: Need to retain drop-down value after form submission.

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,151
  • Joined: 08-August 08

Re: Need to retain drop-down value after form submission.

Posted 25 March 2012 - 07:38 PM

Do you remember this thread, and how you went to another site where they gave you the solution? That's the reason you're having trouble with this simple task: you learned nothing by being handed the solution. You didn't learn that HTML is static and PHP is dynamic, nor did you learn that PHP can be used to build HTML dynamically, so you're still stuck at square one.

Go through that thread and use the advice you were given.
Was This Post Helpful? 1
  • +
  • -

#3 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: Need to retain drop-down value after form submission.

Posted 25 March 2012 - 08:35 PM

Whats wrong with going to another forum? I was getting nowhere here. You were just telling me how stupid I was for "not getting it" ..PHP community is free and open source and supposed to be friendly. Obviously you have to talk down to me to make yourself feel better about yourself rather than help me. Thank goodness there are other people in other forums that are much more willing to share their knowledge. If you had any idea how much time I spent reading/watching videos etc you wouldn't be saying this. Do me a favor and don't reply to my posts anymore if you are just gonna bash me for trying to learn this stuff. I admit it was a mistake now posting what I did in the other forum but I was frustrated. Still doesn't give you the right to try to black ball me from this forum. I can easily mask my IP and just create a new account if it comes down to it.
Was This Post Helpful? -2
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,151
  • Joined: 08-August 08

Re: Need to retain drop-down value after form submission.

Posted 25 March 2012 - 08:55 PM

Nothing's wrong with going to another forum. What's wrong is looking for some one to give you the solution instead of taking the advice you're given and learning something. If I thought you were trying to "learn this stuff" you'd get a completely different response. Even now, if you were to read my post for what it is you would know that I'm giving you good advice which you are again not following.

Masking your IP will not change anything because logging in by a different name will not change the fact that you're looking to be spoon fed answers and not looking to "learn this stuff".
Was This Post Helpful? 1
  • +
  • -

#5 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: Need to retain drop-down value after form submission.

Posted 25 March 2012 - 09:03 PM

FYI, I do understand how php code works. I am not a "C" programmer and the syntax gives me fits. I will go back now and look at all the links you provided and then post again. The last thing I wanted was people on here hating on me.
Was This Post Helpful? 0
  • +
  • -

#6 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Need to retain drop-down value after form submission.

Posted 25 March 2012 - 10:38 PM

If should be if in:
<?php  If( $i==($_POST['item2'])){?>


I hope I'm not being condescending by posting these links.
How to learn programming.
http://norvig.com/21-days.html
http://www.cprogramm...to_program.html

On Lifehacker Programmer 101 they suggest Patience, Elbow Grease, Trial and Error.

Think like a computer scientist.
http://www.openbookp...thon/english2e/

Play Tower of Hanoi. This thing will mess with your mind, but it can be solved and will give you something very powerful to use in programming.

Play Plumber and that will help too.
Warning: there is some loud music playing when you start that game, but you can turn it off.

Mostly these games are just fun.
But playing them will get you into a similar kinda thinking that can be used to solve problems in programs.

Remember there are two big problems in programming.
The reason why you're making a program, and the program itself.

What kind of visual basic do you know?
Are you using the integrated development environment mostly or do you code directly.

This post has been edited by hiddenghost: 25 March 2012 - 10:48 PM

Was This Post Helpful? 1
  • +
  • -

#7 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: Need to retain drop-down value after form submission.

Posted 26 March 2012 - 01:42 PM

here is an online version of my php project:


Portland Climate Data

Take a look at it.. You can see how the box repopulates with the field value instead of the selected name.

The reason this doesn't work correctly is because I did the coding myself :online2long:
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,147
  • Joined: 12-June 08

Re: Need to retain drop-down value after form submission.

Posted 26 March 2012 - 02:25 PM

Please do not create duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#9 Schmit38  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 78
  • Joined: 21-December 07

Re: Need to retain drop-down value after form submission.

Posted 26 March 2012 - 02:48 PM

Well one of your members was giving me a hard time on the other thread so I started a new one.

My apologies.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,151
  • Joined: 08-August 08

Re: Need to retain drop-down value after form submission.

Posted 26 March 2012 - 03:00 PM

Sigh. Ok, here's a little spoon feeding for you. Read these:
http://www.dreaminco...ode-separation/
http://www.dreaminco...-use-functions/
http://www.dreaminco...box-from-mysql/
When you're done, read them again. Then again.
After you've read each three times you might have questions. You might even find mistakes in them! Try writing some code to see if you can answer them yourself, then post back here with a specific problem and error messages.
Was This Post Helpful? 0
  • +
  • -

#11 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: Need to retain drop-down value after form submission.

Posted 26 March 2012 - 03:30 PM

Logically:

- Initialise variable "item" with a value. E.g. "0"
- If POST data has been submitted:
--- Set variable "item" to the value submitted.
- For each <option> element in your <select>:
--- Check if the option's "value" is equal to your "item" variable's value. If it is:
------ Set "selected='selected' on that <option>

Some things consider:

- You can't have your <input> submit button within your <select></select> tags
- Where are you actually setting the variable $i ? I cannot see it, only the comparison with it
- You are not setting the "selected='selected'" value on any of your original <option> elements, you appear to be creating a new one at the bottom after they submit something.


A very quick example:
<?php
$item = 0; # Initialise variable

// If post data submitted, set variable to submitted value
if(isset($_POST['item'])){
$item = $_POST['item'];
}
?>

<form action='' method='post'>
<select name='item'>
<option value="1" <?php if($item == 1): echo "selected='selected'"; endif; ?> >One</option>
<option value="2" <?php if($item == 2): echo "selected='selected'"; endif; ?> >Two</option>
<option value="3" <?php if($item == 3): echo "selected='selected'"; endif; ?> >Three</option>
</select>
</form>



That is of course not a particularly great way to do it, as you have to manually check each option. The better idea would be to loop through a given set of values and create the <option> elements automatically, checking as you go. E.g:

$values = array(1, 2, 3);

foreach($values as $value)
{

$selected = ($value == $item) ? "selected='selected'" : "" ;
echo "<option value='{$value}' {$selected}>{$value}</option>";

}




Or something along those lines.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1