自动检查单选按钮和php表单处理 - 如何避免空白字段? [英] Auto checked radio buttons and php form processing - How to avoid blank field?

查看:100
本文介绍了自动检查单选按钮和php表单处理 - 如何避免空白字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决:只需要将autocomplete =off添加到单选按钮中即可。

编辑:我在原始评估中导致错误的原因不正确。它不取决于用户是否做出选择。这是当我在表单的其余部分使用Chrome的自动填充功能时。如果我使用自动填充功能,则不会传递任何值: - /



编辑2:看起来这是一个已知的错误。 Chrome自动填充未填充单选按钮 http://productforums.google.com/forum/#!主题/ chrome / WNBd8p6q7YQ



我有一个表格,推荐的邮资选项是使用checked预先选择的:

 < fieldset class =outgoingpostage> 

<图例>外向邮资< /图例>

< label for =departurepostageeconomy>经济< span>< span class =greenprice>£3.95< / span>< / label>
< input type =radioname =outgoingpostagevalue =3.95id =departurepostageeconomychecked>

< label for =outgoingpostagestandard>标准< br>< span class =greenprice>£7.95< / span>< / label>
< input name =outgoingpostagetype =radioid =outwardpostagestandardvalue =7.95>

< label for =outgoingpostagepremium> Premium< br>< span class =greenprice>£11.95< / span>< / label>
< input type =radioname =outgoingpostagevalue =11.95id =outgoingpostagepremium>

< label for =outgoingpostagepostyourself>我会使用自己的首选送货服务发布自己< / label>
< input type =radioname =outgoingpostagevalue =0id =outgoingpostagepostyourself>

< / fieldset>

问题是这个值没有通过php表单处理器传递,除非用户点击了单选按钮。如果他们刚刚离开推荐的选项,那么该字段留空。



我该如何解决这个问题?

  $ outgoingpostage = $ _POST ['outgoingpostage']; 


解决方案

你不能这样做吗?


if(!isset($ _ POST ['outgoingpostage'])){
$ _POST ['outgoingpostage'] = 3.99;
}


为了避免违规行为?


SOLVED: Just need to add autocomplete="off" to the radio buttons.

EDIT: I was incorrect in my original assessment of what's causing the error. It is not dependent on whether or not the user makes a selection. It's when I use Chrome's autofill feature for the rest of the form. If I use the autofill then no value gets passed through :-/

EDIT2: Looks like it's a known bug. Chrome autofill unfills radio buttons http://productforums.google.com/forum/#!topic/chrome/WNBd8p6q7YQ

I have a form where the recommended postage options are pre-selected using "checked":

<fieldset class="outwardpostage">

<legend>Outward postage</legend>

<label for="outwardpostageeconomy">Economy<br><span class="greenprice">£3.95</span></label>
<input type="radio" name="outwardpostage" value="3.95" id="outwardpostageeconomy" checked>

<label for="outwardpostagestandard">Standard<br><span class="greenprice">£7.95</span></label>
<input name="outwardpostage" type="radio" id="outwardpostagestandard" value="7.95">

<label for="outwardpostagepremium">Premium<br><span class="greenprice">£11.95</span></label>
<input type="radio" name="outwardpostage" value="11.95" id="outwardpostagepremium">

<label for="outwardpostagepostyourself">I will post myself using my own preferred delivery service</label>
<input type="radio" name="outwardpostage" value="0" id="outwardpostagepostyourself">

</fieldset>

The problem is the value doesn't get passed through the php form processor unless the user has clicked on the radio button. If they have just left the recommended option then the field is left as blank.

How can I fix this?

$outwardpostage = $_POST['outwardpostage'];

解决方案

Can't you do something like:

if( !isset($_POST['outwardpostage']) ) { $_POST['outwardpostage'] = 3.99; }

To get round it not picking up the default?

这篇关于自动检查单选按钮和php表单处理 - 如何避免空白字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆