2

I have a problem that has been bothering me for days - I am trying to get a variable appended into a an option value when a checkbox is either left on or off. The variable is showing up, but not the way I intent, I want "No Subscription" to be appended into the option value when the checkbox is off, ans "subscribe" to be appended into the option value when the checkbox is on. Here is my script:

<?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

is not being set.

1
  • why you are use checkbox name as an array Commented May 2, 2012 at 7:30

4 Answers 4

2

You have same name of both hidden field and checkbox. Please change hidden field name. and In PHP code put code

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

In place of

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

If you can't change name of hidden field then use code in if condition

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

thanks

Sign up to request clarification or add additional context in comments.

2 Comments

I'd probably suggest removing the hidden field altogether and remove the array from the subscribe field.
Thanks guys, this seems to work, I had put the array and hidden fields in out of desperation not realizing all I needed to do was check a simple Boolean value since my variable only needed to be set as 1 of 2 things. I will be studying all these answers too, because it seems my error derived from a lack of knowing how my condition would/should work!
1

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

2 Comments

isset returns TRUE when successful, so string "1" is never true.
The check for the string is a value check - a separate condition from the isset(). So this answer is valid, although I would use && instead of AND.
0

isset() returns a boolean, please either evaluate it as a boolean or compare it with a boolean using ===. While type coercion will still give the correct result with what you have done, it is inefficient and confusing. Just do:

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

...however this in itself will not fix the problem. Because you have named both your inputs subscribe[], $_POST['subscribe'] will always be set and it will always be an array with either 1 or 2 elements. There is no sense in creating an array for this purpose, try the below code instead:

<?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>

Comments

0

Try This. Its working for me with your code. You have put an <input type="hidden" name="subscribe[]" value="0">. No need for that.

<?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>

1 Comment

You haven't corrected the invalid if condition. Either (isset($_POST['subscribe']) or (isset($_POST['subscribe']) && $_POST['subscribe'] == '1')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.