rtoken
rtoken

Reputation: 313

PHP POST a variable value

I have a form that gives select box options for Any, All, or At Least. If you choose 'At Least', JavaScript (shown below) triggers a second select box and lets you pick from the number of checkboxes checked. How can I make my 'atleast' option value equal to atleast:number picked? So when the form is posted it posts atleast:1, atleast:3, etc.

Here's my HTML:

<select name="anyall" id="anyall" onchange="anyall_change()">
    <option value = "any"> Any </option>
    <option value = "all"> All </option>
    <option value = "atleast"> At Least... </option>
</select>
<span id="atleast_div" style="display:none"></span>

Here's the JavaScript that triggers the second select box that gives a list of numbers to choose from based on how many checkboxes are checked:

 function anyall_change() {
     if ($('#anyall').val() == 'atleast') {
         var current = $('#atleast_qty').val();
         var count = 0;
         $('.possible_checked').each(function() {
             if ($(this).is(':checked')) {
                 count++;
             }
         });
         if (current > count) {
             current = count;
         }
         var opts = '<select name="atleast_qty" id="atleast_qty">';
         for (i = 0; i < count; i++) {
             opts += '<option ';
             if (current == (i + 1)) {
                 opts += 'selected ';
             }
             opts += 'value="' + (i + 1) + '">' + (i + 1) + '</option>';
         }
         opts += '</select> Option(s) from list above';
         $('#atleast_div').html(opts);
         $('#atleast_div').show();
     } else {
         $('#atleast_div').hide();
     }
 }

I've spent a good deal of time trying to figure this out with no luck. I know you can't set the option value equal to a variable, so you must be able to append it somehow with PHP. Would love some help, thanks!

Upvotes: 1

Views: 45

Answers (1)

You can change the select's option value to prefix with 'atleast:' string and get the value using PHP.

example:

opts += 'value="atleast:'+(i+1)+'">'+(i+1)+'</option>';

And on the PHP side, get the value:

<?php
echo $_POST['atleast_qty'];

Hope this helps.

Upvotes: 1

Related Questions