以下是問題:我有一個國家和國家的JS下拉菜單運行在PHP表單中供用戶更新他們的配置文件。幫助使用JS下拉列表爲國家/國家與PHP
用戶選擇國家爲前'美國',然後州'科羅拉多',並提交。會發生什麼情況是這些值在我的數據庫中保存成功,但是當頁面刷新時,只有用戶選擇的國家/地區下拉菜單纔會被選中。狀態顯示爲「選擇狀態」,儘管值「科羅拉多」在數據庫中。
我只是不能設法讓PHP和JS互相對話,這樣如果用戶選擇科羅拉多州,它應該從數據庫中拉出來,並在刷新或返回頁面時顯示爲選定的。
任何想法如何做到這一點?我試着在JS代碼頂部的建議,但沒有成功。
這裏是JS(一些代碼修整爲簡潔起見):
// If you have PHP you can set the post values like this
//var postState = '<?= $_POST["state"] ?>';
//var postCountry = '<?= $_POST["country"] ?>';
var postState = '';
var postCountry = '';
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var state = '\
US:Alaska:Alaska|\
US:Alabama:Alabama|\
';
var country = '\
US:United States|\
CA:Canada|\
';
function TrimString(sInString) {
if (sInString) {
sInString = sInString.replace(/^\s+/g, "");// strip leading
return sInString.replace(/\s+$/g, "");// strip trailing
}
}
// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry) {
if (postCountry != '') {
defaultCountry = postCountry;
}
var countryLineArray = country.split('|'); // Split into lines
var selObj = document.getElementById('countrySelect');
selObj.options[0] = new Option('Select Country','');
selObj.selectedIndex = 0;
for (var loop = 0; loop < countryLineArray.length; loop++) {
lineArray = countryLineArray[loop].split(':');
countryCode = TrimString(lineArray[0]);
countryName = TrimString(lineArray[1]);
if (countryCode != '') {
selObj.options[loop + 1] = new Option(countryName, countryCode);
}
if (defaultCountry == countryCode) {
selObj.selectedIndex = loop + 1;
}
}
}
function populateState() {
var selObj = document.getElementById('stateSelect');
var foundState = false;
// Empty options just in case new drop down is shorter
if (selObj.type == 'select-one') {
for (var i = 0; i < selObj.options.length; i++) {
selObj.options[i] = null;
}
selObj.options.length=null;
selObj.options[0] = new Option('Select State','');
selObj.selectedIndex = 0;
}
// Populate the drop down with states from the selected country
var stateLineArray = state.split("|"); // Split into lines
var optionCntr = 1;
for (var loop = 0; loop < stateLineArray.length; loop++) {
lineArray = stateLineArray[loop].split(":");
countryCode = TrimString(lineArray[0]);
stateCode = TrimString(lineArray[1]);
stateName = TrimString(lineArray[2]);
if (document.getElementById('countrySelect').value == countryCode && countryCode != '') {
// If it's a input element, change it to a select
if (selObj.type == 'text') {
parentObj = document.getElementById('stateSelect').parentNode;
parentObj.removeChild(selObj);
var inputSel = document.createElement("SELECT");
inputSel.setAttribute("name","state");
inputSel.setAttribute("id","stateSelect");
parentObj.appendChild(inputSel) ;
selObj = document.getElementById('stateSelect');
selObj.options[0] = new Option('Select State','');
selObj.selectedIndex = 0;
}
if (stateCode != '') {
selObj.options[optionCntr] = new Option(stateName, stateCode);
}
// See if it's selected from a previous post
if (stateCode == postState && countryCode == postCountry) {
selObj.selectedIndex = optionCntr;
}
foundState = true;
optionCntr++
}
}
// If the country has no states, change the select to a text box
if (! foundState) {
parentObj = document.getElementById('stateSelect').parentNode;
parentObj.removeChild(selObj);
// Create the Input Field
var inputEl = document.createElement("INPUT");
inputEl.setAttribute("id", "stateSelect");
inputEl.setAttribute("type", "text");
inputEl.setAttribute("name", "state");
inputEl.setAttribute("size", 30);
inputEl.setAttribute("value", postState);
parentObj.appendChild(inputEl) ;
}
}
function initCountry(country) {
populateCountry(country);
populateState();
}
這裏是PHP/HTML(修剪了一下):
<?php
include 'dbc.php';
page_protect();
$err = array();
$msg = array();
if ($_POST['doUpdate'] == 'Update') {
foreach ($_POST as $key => $value) {
$data[$key] = filter($value);
}
$country = $data['country'];
$state = $data['state'];
mysql_query("UPDATE users SET
`country` = '$data[country]',
`state` = '$data[state]'
WHERE id='$_SESSION[user_id]'
") or die(mysql_error());
$msg[] = "Your Profile has been updated";
//header("Location: mysettings.php?msg=Your new password is updated");
}
$rs_settings = mysql_query("select * from users where id='$_SESSION[user_id]'");
?>
<html>
<body>
<form name="profile_form" id="profile_form" method="post" action="">
<?php while ($row_settings = mysql_fetch_array($rs_settings)) {?>
<input name="doUpdate" type="submit" id="doUpdate" value="Update">
<table>
<tr>
<th>Country:</th>
<td><select id='countrySelect' name='country' onchange='populateState()'>
</select></td>
</tr>
<tr>
<th>State:</th>
<td>
<select id='stateSelect' name='state'>
</select>
<script type="text/javascript">
initCountry('<?php echo $row_settings['country']; ?>');
</script>
</td>
</tr>
</table>
</form>
</body>
</html>
你的意思是移動populateState()?對不起,但我不明白你認爲應該去哪裏 – pepe 2011-02-02 05:20:41
是的,對不起。天色已晚。我用正確的函數名稱編輯了我的回覆。在完成populateCountry函數之後,將populateState放置在右括號之前,以便在給定代碼的情況下按順序運行。 – jerebear 2011-02-02 15:43:43