473,382 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Saving selection in drop-down lists in PHP after page reload?

Hi all,

I can't find an answer to my quesiton anywhere.

Given a drop-down list with USA states and other text fields I pass
this information for further processing via PHP. If any of the required

fields is empty, the PHP script would return an error and reload the
form page asking to fill out those fields. All entered information is
saved... except for the drop-down list selection. I tried to write a
Javascript function which is echoed by PHP, but interpolation of PHP
variables in the Javascript section gives a syntax error message in
Javascript.

The PHP script returns, e.g., an "AL" string for Alabama. Of course, I
don't want to write PHP code for each option to make it selected.

Would appreciate any reply to my question.

Thanks!

Dshan

Jun 19 '06 #1
3 4246
Message-ID: <11**********************@u72g2000cwu.googlegroups .com> from
ds***@web.de contained the following:
The PHP script returns, e.g., an "AL" string for Alabama. Of course, I
don't want to write PHP code for each option to make it selected.

The way I do it requires some setup, but you only do it once,
First you create an array of states and codes

e.g.

$states= array("AL"=>"Alabama","TX"=>"Texas","VA", "Virginia",...);

then use PHP to create your select box

Untested.

<select name='state'>
<?php
foreach($states as $key=>$value){
if(isset($_POST[$key]) &&$_POST[$key]==$value){
$selected="selected";
}
else{
$selected="";
}
echo"<option value='$key' $selected >$value</option>\n";

}
?>
</select>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jun 19 '06 #2
Thank you, Geoff,

You did it! I made some changes to your code, it works fine!

Geoff Berrow wrote:
Message-ID: <11**********************@u72g2000cwu.googlegroups .com>


Jun 20 '06 #3
Message-ID: <11**********************@i40g2000cwc.googlegroups .com> from
dshan contained the following:
Thank you, Geoff,

You did it! I made some changes to your code, it works fine!


Did I make a mistake?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jun 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Karlsson | last post by:
Hi guys, Does anyone know how I can restore a selection created with createRange()? The problem is that I have a function that opens a new window with window.open() which of course makes the...
16
by: Bernd Hohmann | last post by:
Hi there, we're currently convert an old ISAM application to Java and DB2. Among the load of problems we found the reports which have user selections like "select all data between mm/yyyy and...
0
by: Robin Tucker | last post by:
Hi there, I have a list box (just happens to be owner draw). When I select multiple items using the CTRL key, the items I have selected become highlighted (obviously). Now, when I click on one...
18
by: TORQUE | last post by:
Hi, Im wondering if anyone can help me with a problem. I have a form with more than 50 unbound fields. Some of the fields will be blank from time to time. This seems to be where im having...
2
by: Kannan | last post by:
Hi, I am trying to save Inbox selected message in C: I have used following code string SavedMessage = "C:\\AIA\\Message\\"; string strSaveName = "Test.msg"; Outlook._Application olApp = new...
3
by: NoDozing | last post by:
Trying load an embedded object to the clipboard and then save it to a file. When I try and save to the file, I'm getting a "Object reference not set to an instance of an object". I've marked the line...
3
by: Hartmut Dippon | last post by:
Hi all, I hope somebody can help me with following problem: I have an application where I can drag&drop files/dirs from within explorer onto my form. If multiple files/dirs are selected I...
1
by: darrel | last post by:
Good day! i just want to ask if their is someone who can teach on how i can save records, I'am using ADODB connection... it like this, my system is a time, subject and room scheduling system, all the...
3
omerbutt
by: omerbutt | last post by:
hi there i have a tbl in db which has following fields table name:stock id_no(primary key...autono) part_name(text) price(currency) now i have an asp and htmlbased form in which i have made a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.