0

I have a condition where there are 5 different menu items and in a row and one form.

For each of the menu item there is a input value in the form, which looks like this:

<form action='' method='post'>
    <?php 
           foreach($menu_items as $item){?>
           <div id='' class=''>
               <?php echo ('menu item description here');?>
               <input type='text' name='something' value='something'>
           </div>
       <?php 
       } 
    ?>
   <button type='submit'>send</button>
</form>

Now i need only to send only one input value with form submit that has the menu class 'active'.

Is there a simple approach to this?

4
  • You need to process your value before submit using JQuery event handler $("form").submit(function(event) { /* ... */ });. Commented Jan 19, 2016 at 17:11
  • id's must be unique. Inserting a blank id for each item is essentially setting them all to the same id. you're not giving anything the 'active' class. Commented Jan 19, 2016 at 17:14
  • Thank you for your response. I will give a shot Commented Jan 19, 2016 at 17:17
  • @JayBlanchard yes the ids are set unique, was just giving the overview of the problem. Commented Jan 19, 2016 at 17:21

2 Answers 2

1

This is a simpler way of doing it:

<form action='' method='post'>
   <select id="example" name="example">
     <option value="something">Option 1</option>
     <option value="something">Option 2</option>
     <option value="something">Option 3</option>
     <option value="something">Option 4</option>
     <option value="something">Option 5</option>
   </select>

   <button type='submit'>send</button>
</form>

now when you send the form using PHP it will only send the value of the selected option. <select> can be followed by as many <option> blocks as you want.

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

2 Comments

but i need the user to enter the input as text
You could create a single <input> where the user inputs the text and then selects for what option it is?
0

You need to listen to the event thrown by your form, and process your form before sending it.

You can follow this example to perform this task :

$(function() {
    $("form").submit(function(event) {
        event.preventDefault(); // prevent sending the form before we processed the form

        var active_input        =   $(".active")[0];        
        var active_input_value  =   active_input.val();

        $.ajax({
                url     :   "php_file_to_call.php"
            ,   method  :   "post"
            ,   data    :   { something : active_input_value }
        });
    });
});

The Ajax call (which could have been a $.post()) is necessary since you cannot submit the actual form because it does not prevent un-active element to be sent.

Comments

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.