Home > Net >  Get Multiple name value of multiple select in PHP POST
Get Multiple name value of multiple select in PHP POST

Time:01-16

I have my HTML form code something like this.

<div >
  <label>Vendor Name <span >*</span>
  </label>
  <input type="text" name="vendor_name" placeholder="Hello World"  required>
</div>
<div >
  <label>my Domain: <span >*</span>
  </label>
  <input type="text" name="mailwizz_domain[]" value=""  placeholder="https://mailwizz.com">
</div>
<div >
  <label>Select Required Variables</label>
  <select multiple="multiple" name="redirect_url_variables[]"  data-fouc>
    <option locked="locked" value="FNAME" selected>FNAME</option>
    <option value="FROM_NAME">FROM NAME</option>
    <option value="FROM_EMAIL">FROM EMAIL</option>
    <option value="SUBJECT_LINE">SUBJECT LINE</option>
  </select>
</div>


<div >
  <label>my Domain: <span >*</span>
  </label>
  <input type="text" name="mailwizz_domain[]" value=""  placeholder="https://mailwizz.com">
</div>
<div >
  <label>Select Required Variables</label>
  <select multiple="multiple" name="redirect_url_variables[]"  data-fouc>
    <option locked="locked" value="FNAME" selected>FNAME</option>
    <option value="FROM_NAME">FROM NAME</option>
    <option value="FROM_EMAIL">FROM EMAIL</option>
    <option value="SUBJECT_LINE">SUBJECT LINE</option>
  </select>
</div>

so here mailwizz_domain and redirect_url_variables both are multiple and written with []

Now I am posting form data with POST method to PHP and ajax.

Basically I am looking for first insert vendor name in vendor table, mailwizz_domain belongs with vendor name so I am inserting in domain list table. Now redirect_url_variables depends on mailwizz_domain and so I want insert it for each mailwizz domain in domain_list_variable table. so I am trying like this

    $vendor_name = $_POST['vendor_name'];
    $api_endpoint = $_POST['mailwizz_domain'];
    
    //vendor insert code is here
    
    $list_count=count($api_endpoint);
    
    for($k=0; $k<$list_count; $k  ){ 
    $api_endpoint_sql = $_POST['mailwizz_domain'][$k];
    //domain list insert code here
    
    $redirect_url_variables = $_POST['redirect_url_variables'][$k];
    $redirect_url_variables_count = count($redirect_url_variables);
    
    for($v=0; $v<$redirect_url_variables_count; $v  ){ 
        $variable = $_POST['redirect_url_variables'][$k][$v];
         //variable insert code here          
    }
}

But I am getting the error called

PHP Warning:  count(): Parameter must be an array or an object that implements Countable in

in line $redirect_url_variables_count = count($redirect_url_variables);

and only one variable getting inserted with Just First Character called F, even I have selected three variables. My Post in Browser looks like this

enter image description here

I am not getting idea whats wrong with my code, Let me know if anyone here can help me for solve the puzzle. Thanks!

CodePudding user response:

I would look into this:

$redirect_url_variables = $_POST['redirect_url_variables'][$k];
$redirect_url_variables_count = count($redirect_url_variables);

It looks like that you are assigning '$redirect_url_variables' as a single string, not an array.

Maybe you want '$redirect_url_variables[] ='?

CodePudding user response:

It is not very clear what you are trying to obtain. My best guess is that you want to link the first mailwizz_domain text input to the first redirect_url_variables select and the second mailwizz_domain to the second redirect_url_variables.

This is not happening because redirect_url_variables collects the values from both selects in a single array (that may even be empty if no option is selected).

So, redirect_url_variables should be a bidimensional array, where the first index is linked to the relative mailwizz_domain index.

For example:

<!-- First input -->
<input type="text" name="mailwizz_domain[1]">
...
<select multiple="multiple" name="redirect_url_variables[1][]">...
...

...
<!-- Second input -->
<input type="text" name="mailwizz_domain[2]">
...
<select multiple="multiple" name="redirect_url_variables[2][]">...
...
  •  Tags:  
  • Related