1

I want to send multiple inputs(one is text and one is select multiple) via AJAX to a PHP file. Data is sended correctly with form serialize, in fact, using console.log, it shows that prod_multi is a complete array with the selected options. The problem is when I try to store the array values of prod_multi in a variable; if I echo the variable, it is empty. Could you plese help me to solve this issue?

Here's the code.

HTML

<form id="form_menu">
  <div class="row row-24">
    <div class="col-sm-12">
      <div class="form-wrap form-wrap-validation validation-with-outside-label">
        <label class="form-label-outside" for="forms-check-name">Name *</label>
          <input class="form-input round-small" id="forms-check-name" type="text" name="name_menu" placeholder="Name" required>
      </div>
     </div>
     <div class="col-sm-12">
       <div class="form-wrap form-wrap-validation">
         <label class="form-label-outside" for="involve-form-product">Products *</label>
            <select class="form-input round-small product" id="involve-form-product" name="prod_multi" multiple required>
               <option disabled="disabled" >Products</option>
               <option>Prod A</option>
               <option>Prod B</option>
               <option>Prod C</option>
               <option>Prod D</option>
             </select>
       </div>
     </div>
     <div class="col-md-6 mt-3">
        <input type="submit" class="button button-primary button-xs round-xl button-block form-el-offset-1" value="Add" id="add_menu" name="add_menu">
     </div>
   </div>
</form>

AJAX

$(document).on('click','#add_menu',function (e) {
  var form = $('#form_menu').serialize();
  e.preventDefault();
            
  $.ajax({
   type: "POST",
   url: "file.php",
   data: form,
   cache: false,
   success: function()
   {
      alert("Ok");
   },
   error: function() 
   {
      alert("Not ok");
   }
 });
});

PHP

$array=$_POST["prod_multi"];
$array_length=count($array);
$list=NULL;
$i=0;
foreach($array as $prod) 
{
    if(++$i == $array_length) 
    {
       $list=$list . $prod;
    } 
    else 
    {
       $list=$list . $prod . ', ';
    }
    $i++;
}

Thank you for your help.

10
  • Try adding value attribute to option tag. <option value="10">Prod A</option> Commented Jan 3, 2022 at 10:31
  • Try using $('#form_menu').serializeArray() instead of $('#form_menu').serialize() Commented Jan 3, 2022 at 10:33
  • @Kasun already tried and it doesn't work Commented Jan 3, 2022 at 10:42
  • @LamTranDuc nothing changed Commented Jan 3, 2022 at 10:43
  • If you're trying to get the array as a comma separated list, there's no need to build it yourself with foreach. All you need to do is: $list = implode(', ', $_POST['prod_multi']);. Side note: I hope you're not planning on storing it like that in a database though. If you do, then you should read about database normalization to learn how to properly store it. Commented Jan 3, 2022 at 10:52

1 Answer 1

3

Adding the brackets will help PHP process the data correctly

name="prod_multi[]"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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