Using jquery with php to [Add More..] Pin

I have a problem with this code. I am not sure if it’s jquery’s or php’s problem.

I am using jquery and php to implement [Add More…]

first I’ll list the code:

here is my jquery function:

<script type="text/javascript">
    $(function() {
        var counter = 0;
        $('#add_industry').click(function() {
            counter++;
            var newField = $('#cboIndustry').clone().
                attr({id: 'cboIndustry_' + counter,
                    // name: 'cboIndustry_' + counter});
                    name: 'cboIndustry[]'});
            $('#container').append(newField);
        });
    });
</script>

then here:

<tr>
    <td valign="middle">Industry</td>
    <td valign="middle">:</td>
    <td valign="middle">
        <div id="container">
        <select id="cboIndustry" name="cboIndustry" style="width: 100%" required>
            <option value="" selected>[Industry..]</option>
            <?php
                $mysql_command = "CALL sp_populate_industry()";				
                $mysql_query = $mysql_connection->prepare($mysql_command);

                $mysql_query->execute();

                while($mysql_row = $mysql_query->fetch())
                {
            ?>
            <option value="<?php echo $mysql_row['industry_id']; ?>" <?php if ($mysql_row['industry_id'] == $company_industry) { echo 'selected'; } ?>><?php echo $mysql_row['industry_name']; ?></option>
            <?php } ?>
        </select>
        </div>
    </td>
</tr>
<tr>
    <td valign="middle"></td>
    <td valign="middle"></td>
    <td valign="middle"><p id="add_industry"><a href="javascript:void(0)"><span>Add Industry</span></a></p></td>
</tr>

and in my php code I am inserting into mysql using:

if ($_POST['cboIndustry'])
{
    foreach ( $_POST['cboIndustry'] as $key => $value )
    {
        $mysql_query = $mysql_connection->prepare('CALL sp_add_new_company_industry(:param_member_guid, :param_company_guid, :param_company_industry, :param_created_ip_address)');
        $mysql_query->bindParam(':param_member_guid', $_SESSION["xoompage_member_guid"], PDO::PARAM_STR);
        $mysql_query->bindParam(':param_company_guid', $company_guid, PDO::PARAM_STR);
        $mysql_query->bindParam(':param_company_industry', $value, PDO::PARAM_STR);
        $mysql_query->bindParam(':param_created_ip_address', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
        $mysql_query->execute();
    }
}

now the problem is… if I am adding two industries then only one will be inserted in the database and if I add 10 only 9 will be added… why?

Thanks,
Jassim

Hi there,

Dynamically added elements should be submitted with your form.
The first place I would check is your PHP to see what $_POST['cboIndustry'] contains.

my var_dump is:

array(4) { [0]=> string(2) "29" [1]=> string(1) "5" [2]=> string(2) "50" [3]=> string(2) "13" }

from the attached input [see the attached image]

which means the Advertising Technology was not submitted!

why?

Could you post a link to your page so I can take a look at the form?

you can go to www.xoompage.com

login with <snip />

and password is Pullo

Hi,

I logged in, but can’t see the form in question.

sorry it was not uploaded.

Please go to http://www.xoompage.com/biz, add a new company and you’ll see the var_dump of it.

Thanks
<snip/>

I’m wondering if it might be a PHP error and not a javascript error?

Warning: Invalid argument supplied for foreach() in /home/jassimuser/xoompage.com/biz/insert_company.php on line 47
string(2) “62”

Though the form is a bit unclear.
You need to click the “add” link under the select for it to get into an array?
Even after doing so, the page reloaded showing the previously added industry but gave an “add an industry” message so it very well could be a problem with

<script type="text/javascript">
        $(function() {
            var counter = 0;
            $('#add_industry').click(function() {
                counter++;
                var newField = $('#cboIndustry').clone().
                    attr({id: 'cboIndustry_' + counter,
                        // name: 'cboIndustry_' + counter});
                        name: 'cboIndustry[]'});
                $('#container').append(newField);
            });
        });
</script>

appended but not selected?
appended outside of the select?

Yeah, I get the same.
That looks like a PHP error as Mittineague says.

Off Topic:

I found it confusing that my URL was flagged as invalid until I entered the protocol. Maybe you want to add a note about that?

Warning: Invalid argument supplied for foreach() in /home/jassimuser/xoompage.com/biz/insert_company.php on line 47
string(2) “62”

I am not getting this error anywhere! where did you see this error?

Moreover,

I don’t rember how I saw it before, and when I tried to replicate it I got

array(2) {
  [0]=>
  string(3) "226"
  [1]=>
  string(1) "1"
}

I added the first 3 Industries, but only the last 2 “took”

<div id="container">
   <select id="cboIndustry" name="cboIndustry" style="width: 100%" required>
      <option value="" selected>[Industry..]</option>
      <option value="3" >Accounting</option>
      <option value="226" >Administration</option>
      <option value="1" >Advertising</option>
      <option value="62" >Aerospace</option>
      <option value="228" >Agriculture</option>
      <option value="12" >Airline</option>
      <!-- etc. etc. -->
   </select>
</div>

i.e. I think it should have been “3”, “226” and “1”. But for some reason “3” didn’t take.

and that’s exactly my problem which I am trying to find a solution for it…

When I submitted the form at the link you posted.
It seems that this has been cleared up. I’ll have another look today.

Hi there,

I’ve just gone to this page: http://www.xoompage.com/biz/home
I refreshed the page to make sure I didn’t have anything cached locally.
I clicked “Add your company”
This takes me to: http://www.xoompage.com/biz/companydetails
I refreshed the page again to make sure I didn’t have anything cached locally.
I then entered:

Company Name: Test
Country: Afghanistan
Industry: Accounting
Email Address: test@test.test

I clicked “Save”
I am redirected to: http://www.xoompage.com/biz/insert_company
I see this error message:

Warning: Invalid argument supplied for foreach() in /home/jassimuser/xoompage.com/biz/insert_company.php on line 47
string(1) "3"

Now I am getting the same error but it’s because of the same issue which I am seeking a solution for it.

If You have selected two industries only one will be passed, if you selected three only two will be passed so it’s always -1 (minus 1) that’s why when you selected only one (1-1 =0). The line 47 is this:

foreach ( $_POST['cboIndustry'] as $key => $value )

from my above code in my first post

OK, with you now.

If you look at your code in your browser’s dev tools (once you have added a couple of industries), you see:

<select id="cboIndustry" name="cboIndustry" style="width: 100%" required=""></select>
<select id="cboIndustry_1" name="cboIndustry[]" style="width: 100%" required=""></select>
<select id="cboIndustry_2" name="cboIndustry[]" style="width: 100%" required=""></select>

As you can see, the has been left off of the name attribute of the first industry.
Try adding it and see if that helps.

Yup! :slight_smile: Yahoooo! the problem is solved by adding the brackets in the select’s name.

Thank you sooooooo much…

You’re welcome.

Thanks for taking the time to let us know that this worked for you.