所以基本上我有一个表单,用于向数据库提交一篇新文章。
提交后,页面将验证输入并显示错误,并使用$_POST重新填充字段以允许用户修复错误。这可以很好地工作。代码如下:
<form name="form1" method="post" action="newArticle.php?action=add">
<p> Title:<br />
<input name="title" type="text" id="title" size="30"
value="<?=$HTTP_POST_VARS['title']?>">
</p>
<p> Tagline:<br />
<input name="tagline" type="text" id="tagline" size="30"
value="<?=$HTTP_POST_VARS['tagline']?>">
</p>我现在已经添加了3个下拉菜单来选择该部分,每个下拉菜单都是在从以前的列表中进行选择后通过JS重新加载从数据库中填充的。这些也可以很好地工作。如下所示:
<? ////////// Starting of first drop downlist /////////
echo "<select name='country' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['id']==@$country){
echo "<option selected value='$noticia2[id]'>$noticia2[name]</option>"."<BR>";}
else{
echo "<option value='$noticia2[id]'>$noticia2[name]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////
////////// Starting of second drop downlist /////////
echo "<select name='region' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
if($noticia['id']==@$region){
echo "<option selected value='$noticia[id]'>$noticia[name]</option>"."<BR>";}
else{
echo "<option value='$noticia[id]'>$noticia[name]</option>";}
}
echo "</select>";
////////////////// This will end the second drop down list ///////////
////////// Starting of third drop downlist /////////
echo "<select name='resort' ><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer3)) {
echo "<option value='$noticia[id]'>$noticia[name]</option>";
}
echo "</select>";
////////////////// This will end the third drop down list ///////////?>JavaScript为:
<script language=JavaScript>
function reload(form)
{
var val=form.country.options[form.country.options.selectedIndex].value;
self.location='newArticle.php?country=' + val ;
}
function reload3(form)
{
var val=form.country.options[form.country.options.selectedIndex].value;
var val2=form.region.options[form.region.options.selectedIndex].value;
self.location='newArticle.php?country=' + val + '®ion=' + val2 ;
}
</script>我的问题是,在使用下拉菜单重新加载后,表单域是空的,这实际上意味着用户必须重新启动。
有没有一种合理的方法来实现这一点,或者在post/reload时填充字段的替代方法?这似乎是一种复杂的做事方式。
我更喜欢使用PHP来使用post更新下拉列表,但在选择其他下拉列表之后,我遇到了更改拳头下拉列表的问题。
发布于 2011-02-03 02:13:00
正如评论中所说,当您使用self.location =更改页面的位置时,表单中的所有数据都会丢失。
为什么不直接使用form.submit()发布表单,而不是self.location='newArticle.php?country=' + val + '®ion=' + val2 ;
https://stackoverflow.com/questions/4876735
复制相似问题