2

我有一个困扰我好几天的问题——当复选框处于打开或关闭状态时,我试图将一个变量附加到一个选项值中。变量正在显示,但不是我想要的方式,我希望在复选框关闭时将“无订阅”附加到选项值中,并且在复选框打开时将“订阅”附加到选项值中。这是我的脚本:

<?php

if(isset($_POST['subscribe']) == '1')   {
    $sub = ", Subscribe";
    } else {
    $sub = ", No Subscription";}
?>

--------------------------------------------------------------------------------------------


            <form class="cart" action="http://serber.com" method="post">
            <section class="clear"></section>
             <label>Tampons (Non-Applicator) Regular :&nbsp;</label> 
              <select name="productpr">
                <option value="NT12<?php printf($sub); ?>:17.95">1 Month - $17.95 <span></span> </option>
                <option value="NT12 3 Months<?php printf($sub); ?>:33.85">3 Months - $33.85</option>
                <option value="NT12 6 Months<?php printf($sub); ?>:57.70">6 Months - $57.70</option>
              </select>
              <input type="hidden" name="submitted" value="true" />
               <br /><input type="submit" value="Add to Cart">&nbsp;
               <input type="hidden" name="subscribe[]" value="0" />
               <input type="checkbox" name="subscribe[]" value="1">Check Box if you wish to have a recurring subscription</input> 
          </form>

I am at my wit's end with this because it seems I have the isset correct but the checkbox value

没有被设置。

4

4 回答 4

1

if (isset($_POST['subscribe']) AND $_POST['subscribe'] == '1') {

于 2012-05-02T07:23:14.900 回答
1

hidden field和 和的名称相同checkbox。请更改隐藏字段名称。并在 PHP 代码中放置代码

if(isset($_POST['subscribe']) && ($_POST['subscribe'][0] == '1'))   

代替

if(isset($_POST['subscribe']) == '1')   

如果您无法更改隐藏字段的名称,则在 if 条件中使用代码

if(isset($_POST['subscribe']) && ($_POST['subscribe'][1] == '1'))   

谢谢

于 2012-05-02T07:55:19.233 回答
0

isset()返回一个布尔值,请将其评估为布尔值或将其与布尔值使用===. 虽然类型强制仍然会根据您所做的事情给出正确的结果,但它效率低下且令人困惑。做就是了:

if (isset($_POST['subscribe'])) {

...但是这本身并不能解决问题。因为您已经命名了两个输入subscribe[]$_POST['subscribe']所以将始终设置它,并且它始终是一个包含 1 个或 2 个元素的数组。为此目的创建数组是没有意义的,请尝试以下代码:

<?php

  if (isset($_POST['subscribe'])) {
    $sub = ", Subscribe";
  } else {
    $sub = ", No Subscription";
  }

?>

<!--------------------------------------------->

<form class="cart" action="http://serber.com" method="post">
  <section class="clear"></section>
  <label>Tampons (Non-Applicator) Regular :&nbsp;</label> 
  <select name="productpr">
    <option value="NT12<?php printf($sub); ?>:17.95">1 Month - $17.95</option>
    <option value="NT12 3 Months<?php printf($sub); ?>:33.85">3 Months - $33.85</option>
    <option value="NT12 6 Months<?php printf($sub); ?>:57.70">6 Months - $57.70</option>
  </select>
  <input type="hidden" name="submitted" value="true" /><br />
  <input type="submit" value="Add to Cart" />&nbsp;
  <input type="checkbox" name="subscribe" value="1"<?php if (isset($_POST['subscribe'])) echo ' checked="checked"'; ?>>Check Box if you wish to have a recurring subscription</input> 
</form>
于 2012-05-02T07:32:02.783 回答
0

试试这个。它使用您的代码为我工作。你已经放了一个<input type="hidden" name="subscribe[]" value="0">. 没必要。

<?php
echo"hi";
if(isset($_POST['subscribe']) == '1')   {
    $sub = ", Subscribe";
    } else {
    $sub = ", No Subscription";}
    echo $sub;
?>

<html>
<body>
            <form class="cart" method="post">
            <section class="clear"></section>
             <label>Tampons (Non-Applicator) Regular :&nbsp;</label> 
              <select name="productpr">
                <option value="NT12<?php echo($sub); ?>:17.95">1 Month - $17.95 <span></span> </option>
                <option value="NT12 3 Months<?php echo($sub); ?>:33.85">3 Months - $33.85</option>
                <option value="NT12 6 Months<?php echo($sub); ?>:57.70">6 Months - $57.70</option>
              </select>
              <input type="hidden" name="submitted" value="true" />
               <br /><input type="submit" value="Add to Cart">&nbsp;
               <input type="checkbox" name="subscribe[]" value="1">Check Box if you wish to have a recurring subscription</input> 
          </form>
          </body>
</html>
于 2012-05-02T07:40:04.750 回答