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: -
<form method="post" action="<?php echo $PHP_SELF;?>">
-
<span style='color: #008b8b; font-size: 10pt; font-family: arial'>
-
<b>code </b><input type="text" size="12" maxlength="3" name="cd" value =
-
i am not sure what goes afte rthe "value=" part.
thanks in advance,
geebee
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. -
value="<?php if ( isset ( $_POST['cd'] ) ) echo $_POST['cd']; ?>"
-
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: -
<select name="TEST">
-
<option value="select one" selected = "selected">Select One</option>
-
<option value="Charity">one</option>
-
<option value="Charity">two</option>
-
...
-
thanks in advance,
geebee
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: -
$selected = 3;
-
-
$values =
-
array
-
(
-
'0' => 'Sunday'
-
, '3' => 'Wednesday'
-
, '5' => 'Friday'
-
);
-
-
foreach( $values as $val => $text )
-
{
-
echo '<option value="', htmlentities($val), '"';
-
-
if( $val == $selected )
-
{
-
echo ' selected="selected"';
-
}
-
-
echo '>', htmlentities($text), '</option>';
-
}
-
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: -
<select>
-
$selected = 3;
-
$values =
-
array
-
(
-
'0' => 'Sunday'
-
, '3' => 'Wednesday'
-
, '5' => 'Friday'
-
);
-
-
foreach( $values as $val => $text )
-
{
-
echo '<option value="', htmlentities($val), '"';
-
-
if( $val == $selected )
-
{
-
echo ' selected="selected"';
-
}
-
echo '>', htmlentities($text), '</option>';
-
}
-
</select>
-
but its not working... am i missing something?
thanks in advance,
geebee
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.
ok now i have.. -
<?php
-
########################################################
-
#this dropdown remembers the values selected after form is posted
-
#by processing each element as i dynamically output it
-
########################################################
-
<select>
-
$values =
-
array
-
(
-
'0' => 'Sunday'
-
, '3' => 'Wednesday'
-
, '5' => 'Friday'
-
$selected = 3;
-
);
-
-
foreach( $values as $val => $text )
-
{
-
echo '<option value="', htmlentities($val), '"';
-
-
if( $val == $selected )
-
{
-
echo ' selected="selected"';
-
}
-
echo '>', htmlentities($text), '</option>';
-
}
-
</select>
-
########################################################
-
#end of this dropdown remembers the values selected after form is posted
-
#by processing each element as i dynamically output it
-
########################################################
-
?>
-
still not working. im sure its just a small syntax or order i am missing somewhere...
Dormilich 8,658
Recognized Expert Moderator Expert
swap line 13/14 so $selected is outside the array.
i now have... -
-
<?php
-
########################################################
-
#this dropdown remembers the values selected after form is posted
-
#by processing each element as i dynamically output it
-
########################################################
-
<select>
-
$values =
-
array
-
(
-
'0' => 'Sunday'
-
, '3' => 'Wednesday'
-
, '5' => 'Friday'
-
);
-
$selected = 3;
-
-
foreach( $values as $val => $text )
-
{
-
echo '<option value="', htmlentities($val), '"';
-
-
if( $val == $selected )
-
{
-
echo ' selected="selected"';
-
}
-
echo '>', htmlentities($text), '</option>';
-
}
-
</select>
-
########################################################
-
#end of this dropdown remembers the values selected after form is posted
-
#by processing each element as i dynamically output it
-
########################################################
-
?>
-
but now i am getting a parse error pointing to the <select> line
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,...
|
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...
|
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
|
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...
|
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
| |
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
|
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...
|
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...
|
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...
|
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>
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |