我有两个单选按钮,我动态填充它们的值,我选择其中的任何一个,我得到第一个值。
HTML: $haber_modulleri[0] = 'Duyuru';
$haber_modulleri[1] = 'Manşet';
<?php foreach($haber_modulleri as $key => $modul):?>
<div style="margin-bottom:10px;">
<input type="radio" name="haber_tur" value="<?= $key;?>" <?php if(isset($haber->haber_tur) AND $haber->haber_tur == $key) echo "checked"?> />
<label for="<?php echo $key;?>" style="cursor:pointer; font-size:13px;"><?php echo $modul;?></label>
</div>
<?php endforeach;?>
渲染的HTML边:
<div style="margin-bottom:10px;">
<input type="radio" name="haber_tur" value="0" />
<label for="0" style="cursor:pointer; font-size:13px;">Duyuru</label>
</div>
<div style="margin-bottom:10px;">
<input type="radio" name="haber_tur" value="1" />
<label for="1" style="cursor:pointer; font-size:13px;">Manşet</label>
</div>
PHP POST Side:
$haber_tur = $db->escape($_POST['haber_tur']);
echo $_POST['haber_tur'] . "'r'n";
echo $haber_tur . "TUR" . PHP_EOL ;
变量call $ haber_turr始终为零,无论我选择哪个。Echos的结果为"0 0TUR"
这是因为你传递的是$key
而不是值,做:
<input type="radio" name="haber_tur" value="<?= $modul;?>"
value
属性的内容是在调用$_POST['haber_tur']
时传递给PHP的内容。