0

I have my HTML form code something like this.

<div class="form-group">
  <label>Vendor Name <span class="text-danger">*</span>
  </label>
  <input type="text" name="vendor_name" placeholder="Hello World" class="form-control" required>
</div>
<div class="form-group">
  <label>my Domain: <span class="text-danger">*</span>
  </label>
  <input type="text" name="mailwizz_domain[]" value="" class="form-control" placeholder="https://mailwizz.com">
</div>
<div class="form-group">
  <label>Select Required Variables</label>
  <select multiple="multiple" name="redirect_url_variables[]" class="form-control redirect_url_variables select" 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 class="form-group">
  <label>my Domain: <span class="text-danger">*</span>
  </label>
  <input type="text" name="mailwizz_domain[]" value="" class="form-control" placeholder="https://mailwizz.com">
</div>
<div class="form-group">
  <label>Select Required Variables</label>
  <select multiple="multiple" name="redirect_url_variables[]" class="form-control redirect_url_variables select" 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 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 won't insert it for each mailwizz domain in domain_list_variable table. Hence, 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 below error

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 is 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 an idea of what's wrong with my code. Let me know if anyone here can help me solve the puzzle. Thanks!

2 Answers 2

1

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][]">...
...
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Yes You are correct, but I am dynamically creating and deleting fields, so I am not able to assign numbers inside brackets, I have tried redirect_url_variables[][] but its not working
How are you creating it dynamically? With javascript? You may keep a counter variable in javascript and use it to create the two indexes. redirect_url_variables[][] can't work because the PHP engine can't reliably index a two dimensional array at once. There is also no PHP language construct that allow this. Think about it: by what rules would you create such array?
I have got hint from answer and have solved issue. Thanks!
0

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[] ='?

UPDATE:

In that case you may want to rethink your logic. In other words, don't use arrays. You have no way to tie them together. I would suggest you change the input names to 'mailwizz_domain1' and 'mailwizz_domain2'. Then change the select names to 'redirect_url_variables1' and 'redirect_url_variables2'. This is how you will tie them together. When submitted both 'redirect_url_variables*' will be an array, because of the 'multiple' attribute. This way you can process them as a group.

I just saw your comment to 'Dym'. You don't have control over the HTML?

Your HTML must be changed so you can link the input with the select. If you generate multiple groups then you must already use a counter. Place the counter between the [], as suggested by 'Dym', but I would start at 0 not 1.

7 Comments

Hi! with $redirect_url_variables[] there no any more error or warning but even I post 3 variables counts getting only 1 and for $variable = $_POST['redirect_url_variables'][$k][$v]; its coming only F, There something still wrong with the code but does not getting idea. Let me know if you have idea about it. Thanks!
@Vidhi Sharma, That line '$variable = $_POST['redirect_url_variables'][$k][$v]' only extracts the 1st character. I don't understand the purpose of that 2nd for loop, inside the 1st loop.
@Vidhi Sharma, If you want to tie the each 'mailwizz_domain' to each 'redirect_url_variables' then your code already does that with '$redirect_url_variables = $_POST['redirect_url_variables'][$k]'. You don't need the following line with the count command and you don't need a 2nd for loop.
Charles, Thanks a lot, Yes I want do same as you have told in last comment, but problem is that array of redirect_url_variables getting in one array so if I have selected two things in first select, one are getting with first mailwizz and second one is getting in second mailwizz. I hope I am able to clear more
Based on your HTML it looks like each mailwizz entry can have multiple redirect_url_variables options selected. Is this correct?
|

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.