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 1401 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="selected"' 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
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: -
<?php
-
########################################################
-
#this dropdown remembers the values selected after form is posted
-
#by processing each element as i dynamically output it
-
########################################################
-
echo "<select>";
-
$values =
-
array
-
(
-
'0' => 'test1'
-
, '1' => 'test2'
-
, '2' => 'test2'
-
);
-
$selected = $_POST['TEST_GROUP'];
-
-
foreach( $values as $val => $text )
-
{
-
echo '<option value="', htmlentities($val), '"';
-
-
if( $val == $selected )
-
{
-
echo ' selected="selected"';
-
}
-
echo '>', htmlentities($text), '</option>';
-
}
-
echo "</select>";
-
########################################################
-
#end of this dropdown remembers the values selected after form is posted
-
#by processing each element as i dynamically output it
-
########################################################
-
?>
-
-
Dormilich 8,658
Recognized Expert Moderator Expert
what does the output look like?
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.
thanks... this worked.
is there a good way to have he values of the dropdown be based on a SELECT statement?
thanks in advance
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.
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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"...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |