我没有从“谢谢”页面上的单选按钮获得正确的值。
我希望在我的用户结束付款并重定向到感谢页面后,将填写的表单中的一些值发布到那里。我已经用这个脚本将其存档在 form.php 文件中:
<script type="text/javascript">
function CookieTheFormValues() {
var cookievalue = new Array();
var fid = document.getElementById(FormID);
for (i = 0; i < fid.length; i++)
{
var n = escape(fid[i].name);
if( ! n.length ) { continue; }
var v = escape(fid[i].value);
cookievalue.push( n + '=' + v );
}
var exp = "";
if(CookieDays > 0)
{
var now = new Date();
now.setTime( now.getTime() + parseInt(CookieDays * 24 * 60 * 60 * 1000) );
exp = '; expires=' + now.toGMTString();
}
document.cookie = CookieName + '=' + cookievalue.join("&") + '; path=/' + exp;
return true;
}
</script>
而不是将这个脚本放在感谢页面上:
<?php
$CookieName = "PersonalizationCookie";
$Personal = array();
foreach( explode("&",@$_COOKIE[$CookieName]) as $chunk )
{
list($name,$value) = explode("=",$chunk,2);
$Personal[$name] = htmlspecialchars($value);
}
?>
到目前为止,我从其他输入中得到了所有正确的值,但从 radio 中我总是得到类名称值中的最后一个?这意味着例如如果我有以下代码:
<input type="radio" name="emotion" id="basi" value="Basic Pack" />
<input type="radio" name="emotion" id="deli" value="Deluxe Pack" />
<input type="radio" name="emotion" id="premi" value="Premium Pack"/>
在“谢谢”页面中,我将此代码放入例如
Thank you for chosing <?php echo(@$Personal["emotion"]); ?>
我总是得到这个感谢您选择高级包
即使当我检查基本或豪华 radio 时为什么会这样?
最佳答案
你的循环:
for (i = 0; i < fid.length; i++)
{
var n = escape(fid[i].name);
if( ! n.length ) { continue; }
var v = escape(fid[i].value);
cookievalue.push( n + '=' + v );
}
会将所有三个 radio 推送到您的 cookie 值中。每个都会覆盖前一个,因为它们具有相同的名称。因此,最终您留下的值是映射到“情感”名称的“Premium Pack”。在推送 val 之前,您需要检查 radio 是否已选择,可能类似于:
for (i = 0; i < fid.length; i++)
{
var n = escape(fid[i].name);
if( ! n.length ) { continue; }
var v = escape(fid[i].value);
// Only push in the selected emotion radio button
if (n == "emotion") {
if (fid[i].checked == true) cookievalue.push( n + '=' + v );
}
else cookievalue.push( n + '=' + v );
}
关于javascript - 如何从PHP获取特定的radio值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26324797/