收音机输入在页面重新加载时消失(枚举,foreach,默认选中,记住页面重新加载时的答案)

时间:2013-08-06 20:34:10

标签: php javascript html

我可能目前正走错路(疲倦......),但我已经过了4个小时或类似的事情来调试我的代码。当我点击一个单选按钮时,我有一个自动提交的表单,一旦点击下一个表单出现,让我输入客户信息。但是当页面重新加载以显示另一个表单时,我的变量$ CustomerType被设置并且正确,当我完成输入表单(第二个)时,php检查它中的所有内容是否正确并且确实如此,但它表示我的变量$ CustomerType丢失然后重新加载页面并再次要求我设置类型。

我无法将所有代码粘贴到这里(约300行),但这是核心:

<?php $_POST['CustomerType']="SOMEONE"; ?> // Ok so this was the trick, it solved the main bug but it now fix my choice to SOMEONE only. Can't change to another type
<form method="post" action="<?php echo escaped_html($_SERVER['PHP_SELF'],'URL');?>">
<?php
        $array=show_enum("customer", "CustomerType");
        $checked="";
        foreach($array as $CustomerType) 
        {
            $checked="";                            
            if(isset($_POST['CustomerType'])) 
            {
               if($_POST['CustomerType']==$CustomerType) $checked="checked";
            }
            echo "<input type='radio' name='CustomerType' value=".$CustomerType." ".$checked." onClick='this.form.submit()'>".$CustomerType."</input>";
        }
?> </form>

编辑确定有一些新闻:通过修改名为<?php $_POST['CustomerType']="SOMEONE"; ?>

的热门人物

if(!isset($_POST['CustomerType'])) $_POST['CustomerType']="SOMEONE";

它似乎解决了表单的第二个问题,它无法让我更改类型(自动回滚到SOMEONE)。但现在,在表单提交上,我的选择总是回滚到[CustomerType] =&gt;有人而不是SOMEBODY(我检查了SOMEBODY)。

这意味着我无法在页面重新加载时保留值$ _POST ['CustomerType']以进行提交。

例如:这个看起来相同但除了用“保存”按钮而不是onsubmit提交之外工作正常。

$array=show_enum("customer", "Language");
foreach($array as $Language) 
{
    $checked="";                            
        if(isset($_POST['Language'])) 
    {
         if($_POST['Language']==$Language) $checked="checked";                      
    }
    else if($Language=="FR") $checked="checked";

    echo "<input type='radio' name='Language' value=".$Language." ".$checked." />";
    string2languagepic($Language);
}

Picture of the problem * OnSubmit = onClick ='this.form.submit()

1 个答案:

答案 0 :(得分:1)

在仔细查看您的代码之后,我想我已经发现了您的问题,请尝试以下操作,看看它是否有效。

echo "<input type='radio' name='CustomerType' id='CustomerType' value='$CustomerType' $checked onClick='this.form.submit();' >" 

如果失败,你总是可以添加一个隐藏字段,当点击单选按钮时,它会为它添加一个值,然后提交表单。

相关问题