473,503 Members | 4,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

default form field value

60 New Member
hi,

i have a form and i want to be sure that when the form is submitted the value entered stays visible in the form. here is what i have:

Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="<?php echo $PHP_SELF;?>">
  2. <span style='color: #008b8b; font-size: 10pt; font-family: arial'>
  3. <b>code </b><input type="text" size="12" maxlength="3" name="cd" value =
  4.  
i am not sure what goes afte rthe "value=" part.

thanks in advance,
geebee
Jan 13 '09 #1
14 1401
Markus
6,050 Recognized Expert Expert
Just check if the post data is present and, if it is, echo it into the value.

Expand|Select|Wrap|Line Numbers
  1. value="<?php if ( isset ( $_POST['cd'] ) ) echo $_POST['cd']; ?>"
  2.  
Jan 13 '09 #2
geraldjr30
60 New Member
thanks. that works. now how can i do the same thing with a dropdown box? i want it to default to the value already selected from the dropdown when the form is posted. i currently have:

Expand|Select|Wrap|Line Numbers
  1. <select name="TEST">
  2. <option value="select one" selected = "selected">Select One</option> 
  3. <option value="Charity">one</option>
  4. <option value="Charity">two</option>
  5. ...
  6.  

thanks in advance,
geebee
Jan 13 '09 #3
pbmods
5,821 Recognized Expert Expert
Heya, geebee.

To set the value of a <select> element, you need to add 'selected="selected"' to the proper <option> element.

The easiest way to do this is to process each element as you dynamically output it:

Expand|Select|Wrap|Line Numbers
  1. $selected = 3;
  2.  
  3. $values =
  4.   array
  5.   (
  6.       '0' => 'Sunday'
  7.     , '3' => 'Wednesday'
  8.     , '5' => 'Friday'
  9.   );
  10.  
  11. foreach( $values as $val => $text )
  12. {
  13.   echo '<option value="', htmlentities($val), '"';
  14.  
  15.   if( $val == $selected )
  16.   {
  17.     echo ' selected="selected"';
  18.   }
  19.  
  20.   echo '>', htmlentities($text), '</option>';
  21. }
  22.  
Jan 14 '09 #4
geraldjr30
60 New Member
thanks... i tried your suggestion.. but since ive never worked with combining arrays and dropdowns before, your code looks a little confusing to me. i have:

Expand|Select|Wrap|Line Numbers
  1. <select>
  2. $selected = 3; 
  3. $values = 
  4.   array 
  5.   ( 
  6.       '0' => 'Sunday' 
  7.     , '3' => 'Wednesday' 
  8.     , '5' => 'Friday' 
  9.   ); 
  10.  
  11. foreach( $values as $val => $text ) 
  12.   echo '<option value="', htmlentities($val), '"'; 
  13.  
  14.   if( $val == $selected ) 
  15.   { 
  16.     echo ' selected="selected"'; 
  17.   }   
  18.   echo '>', htmlentities($text), '</option>'; 
  19. }
  20. </select>
  21.  
but its not working... am i missing something?

thanks in advance,
geebee
Jan 14 '09 #5
Markus
6,050 Recognized Expert Expert
Well, of course, you need to wrap the php code in <?php and ?>. Otherwise, the code won't be parsed.

Also, $selected will have to point to the selected index of the dropdown.
Jan 14 '09 #6
geraldjr30
60 New Member
ok now i have..

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ########################################################
  3. #this dropdown remembers the values selected after form is posted
  4. #by processing each element as i dynamically output it
  5. ########################################################
  6. <select>
  7. $values = 
  8.   array 
  9.   ( 
  10.       '0' => 'Sunday' 
  11.     , '3' => 'Wednesday' 
  12.     , '5' => 'Friday' 
  13. $selected = 3;
  14.   ); 
  15.  
  16. foreach( $values as $val => $text ) 
  17.   echo '<option value="', htmlentities($val), '"'; 
  18.  
  19.   if( $val == $selected ) 
  20.   { 
  21.     echo ' selected="selected"'; 
  22.   }   
  23.   echo '>', htmlentities($text), '</option>'; 
  24. }
  25. </select>
  26. ########################################################
  27. #end of this dropdown remembers the values selected after form is posted
  28. #by processing each element as i dynamically output it
  29. ########################################################
  30. ?>
  31.  
still not working. im sure its just a small syntax or order i am missing somewhere...
Jan 14 '09 #7
Dormilich
8,658 Recognized Expert Moderator Expert
swap line 13/14 so $selected is outside the array.
Jan 14 '09 #8
geraldjr30
60 New Member
i now have...

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. ########################################################
  4. #this dropdown remembers the values selected after form is posted
  5. #by processing each element as i dynamically output it
  6. ########################################################
  7. <select>
  8.  $values = 
  9.   array 
  10.   ( 
  11.       '0' => 'Sunday' 
  12.     , '3' => 'Wednesday' 
  13.     , '5' => 'Friday' 
  14.   ); 
  15. $selected = 3;
  16.  
  17. foreach( $values as $val => $text ) 
  18.   echo '<option value="', htmlentities($val), '"'; 
  19.  
  20.   if( $val == $selected ) 
  21.   { 
  22.     echo ' selected="selected"'; 
  23.   }   
  24.   echo '>', htmlentities($text), '</option>'; 
  25. }
  26. </select>
  27. ########################################################
  28. #end of this dropdown remembers the values selected after form is posted
  29. #by processing each element as i dynamically output it
  30. ########################################################
  31. ?>
  32.  
but now i am getting a parse error pointing to the <select> line
Jan 14 '09 #9
Dormilich
8,658 Recognized Expert Moderator Expert
@geraldjr30
that's correct, <select> doesn't belong in the PHP code but in the HTML code (or put in simpler words, echo it out like you did with <option>).

regards
Jan 14 '09 #10
geraldjr30
60 New Member
well i am not getting an error now, but the selected option is not defaulted to after the form posts either. here is what i have:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ########################################################
  3. #this dropdown remembers the values selected after form is posted
  4. #by processing each element as i dynamically output it
  5. ########################################################
  6. echo "<select>";
  7.  $values = 
  8.   array 
  9.   ( 
  10.       '0' => 'test1' 
  11.     , '1' => 'test2' 
  12.     , '2' => 'test2' 
  13.   ); 
  14. $selected = $_POST['TEST_GROUP'];
  15.  
  16. foreach( $values as $val => $text ) 
  17.   echo '<option value="', htmlentities($val), '"'; 
  18.  
  19.   if( $val == $selected ) 
  20.   { 
  21.     echo ' selected="selected"'; 
  22.   }   
  23.   echo '>', htmlentities($text), '</option>'; 
  24. }
  25. echo "</select>";
  26. ########################################################
  27. #end of this dropdown remembers the values selected after form is posted
  28. #by processing each element as i dynamically output it
  29. ########################################################
  30. ?>
  31.  
  32.  
Jan 14 '09 #11
Dormilich
8,658 Recognized Expert Moderator Expert
what does the output look like?
Jan 14 '09 #12
Markus
6,050 Recognized Expert Expert
Your select element needs a name attribute (TEST_GROUP). Also, you should check that $_POST['TEST_GROUP'] exists, first, because you'll get an undefined index, otherwise.
Jan 14 '09 #13
geraldjr30
60 New Member
thanks... this worked.

is there a good way to have he values of the dropdown be based on a SELECT statement?

thanks in advance
Jan 14 '09 #14
Markus
6,050 Recognized Expert Expert
@geraldjr30
Yes, but that is a new question. So start a new thread asking that question (be a little more detailed, too.)

Kind regards,
Markus.
Jan 14 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
16169
by: Sarah West | last post by:
Hi, I have the following problem; This is my form, i would like to default the select box to USA, and default the text field to a name such as 'Sarah' and the hidden field to another number...
2
8194
by: Terry Bickle | last post by:
Please forgive me for using the wrong term here or there. I'm an old Excel 4 macro guy who didn't convert to VB and I'm tinkering with an Access 2000 DB. I'm sure there is a simple Access 101...
11
12460
by: David Messner | last post by:
Ok I know this is simple but the statement eludes me... I have a date field where I want the default value on the data entry form's date field to be the last date entered. I figure I can do this...
2
2096
by: CSDunn | last post by:
Hello, I have a field called 'TestGrade'in a subform called 'frmSelectByTestSub'that I need to assign a default value to, and the value needs to be an integer value that is exactly the same as the...
4
2246
by: erick-flores | last post by:
Hello I am trying to setup a default value for a field in a form. The field is a text box. I need to setup the default value to: 0.000. And whenever the user is trying to enter a value for this...
1
2507
by: tnriverfish | last post by:
I've got a text box on a form where the default value shows - C:/ The user can change the default to whatever they want - D:/ I'd like to put a button next to the field that will change the default...
4
3366
by: helenwheelss | last post by:
Access 2003, using a bound form. I'm seeing rather annoying behaviour when editing data in a control with a default value. It only happens when the form is on a new record. A specific...
1
1805
by: EJO | last post by:
I've looked around the groups and don't have tried some of the various suggestions to no avail. So I'm hoping someone can help me out here. I have a form that, when a cmd button is clicked,...
8
12562
by: peterkennett | last post by:
I am designing a database that will be used by many different offices, and each office may want to change the defaults I have in a table. Rather than teach each user how to go in and change a...
6
5063
by: Stanimir Stamenkov | last post by:
I have a form without a submit button like: <form name="form1" action="" onsubmit="alert('submit ' + this.name);"> <div> <label>Field 1: <input type="text" name="field1"...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.