473,656 Members | 2,983 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 1415
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="sele cted"' 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

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

Similar topics

3
16177
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 like '876543', when the user clicks on the check box, otherwise the fields should be blank, the value of the select box dosnt matter much, only that it defaults to USA, when the user checks it. Can anyone help? i have tried a google with no luck,...
2
8219
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 answer for this. I have a subform which the user selects an item description from a combo box from a table. I want to obtain the cost for the item (another field in the table) also. I've been able to produce the second data field by rewriting the...
11
12472
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 with a query but don't know what the criteria needs to be. Any help would be appreciated, Thanks
2
2115
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 value in a field called 'Grade' that is also in the subform. The name of the main form is 'frmSelectByTest'. The 'SourceObject' of the subform and the subform name are the same. 'Grade' in the subform can be different for each student record...
4
2256
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 field, using the form, he ONLY can enter a value with that structure (0.000). eg. 1.234, 1.345, 6.543, 1.040, 0.003, 0.434. Can somebody tell me how to do this please??? I know there is a default value option under the properties of the
1
2511
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 from C:/ to D:/ so the next time the form is opened D:/ is showing. Any ideas? Thanks Charlie
4
3375
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 example: on my form, the first control is bound to a date/ time field and has a default value of now(). When users need to change this control's data, they'll most commonly need to alter the time part leaving the date as is. But, on a new record, when...
1
1820
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, changes the property of the form to dataentry. On this form, there are two fields that have a default value set. The one default value set using the property sheet is getting the value from a numeric field on a different form and is working just...
8
12596
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 table's default values, I want to create a user-friendly form that allows them to change the default values in the linked table. Let's say I have a form field "office" on my form that is linked to the table field in my table. The field has a...
6
5078
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" onchange="alert(this.name + '=' + this.value);"></label></div> <label>Field 2: <input type="text" name="field2" onchange="alert(this.name + '=' + this.value);"></label></div> </form>
0
8296
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.