SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    <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:

    HTML Code:
    <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:


    PHP Code:
    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_guidPDO::PARAM_STR); 
            
    $mysql_query->bindParam(':param_company_industry'$valuePDO::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

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my var_dump is:

    Code:
    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?
    Attached Images Attached Images

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Could you post a link to your page so I can take a look at the form?

  5. #5
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can go to www.xoompage.com

    login with <snip />

    and password is Pullo
    Last edited by Pullo; Dec 25, 2013 at 15:05. Reason: Removed private details

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

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

  7. #7
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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/>
    Last edited by Mittineague; Dec 25, 2013 at 15:31. Reason: Please wait until you get your signature for your links.

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,156
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    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
    Code JavaScript:
    <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?

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    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?

  10. #10
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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,

  11. #11
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,156
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    I don't rember how I saw it before, and when I tried to replicate it I got
    Code:
    array(2) {
      [0]=>
      string(3) "226"
      [1]=>
      string(1) "1"
    }
    I added the first 3 Industries, but only the last 2 "took"
    HTML Code:
    <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.

  12. #12
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and that's exactly my problem which I am trying to find a solution for it...

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by jrahma View Post
    I am not getting this error anywhere! where did you see this error?
    When I submitted the form at the link you posted.
    It seems that this has been cleared up. I'll have another look today.

  14. #14
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    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:
    Code:
    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:
    Code:
    Warning: Invalid argument supplied for foreach() in /home/jassimuser/xoompage.com/biz/insert_company.php on line 47
    string(1) "3"

  15. #15
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    foreach ( $_POST['cboIndustry'] as $key => $value )
    from my above code in my first post

  16. #16
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    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:

    Code:
    <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.

  17. #17
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup! Yahoooo! the problem is solved by adding the brackets in the select's name.

    Thank you sooooooo much...

  18. #18
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    You're welcome.

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •