Update a Select Box on Parent Page from FancyBox2

2019-03-05 14:16发布

问题:

I'm trying to submit a form in a fancybox where users can add a company to a select box that exists on the modals parent page. Im doing this by submitting the modal information to a script that adds the company to my database. Then I run a query to to get all the updated companies as a group of tags. Then I am trying to pass that group of tags to the parent page as a jquery update. Im not sure if this is the best approach or where I'm going wrong.

I am attempting to use this post as a guide:

Find element on site from a fancybox iframe

But I have two problems with my code. One: The fancybox is not closing Two: The select box on the parent page is not updating

I am not sure where I am going wrong with my success call. The code from the Modal page is:

$("#send-message").click(function(){
        $(this).closest('form').submit(function(){
            return false;
        });
        var frm = $(this).closest('form');        
         if($(frm).valid()){
            $("#ajax-loading").show();
            var data = $(frm).serialize();
            $(frm).find('textarea,select,input').attr('disabled', 'disabled');            
            $.post( 
                    "../forms/company_add.php", 
                    data,                   
                    function(data) {
                      if (data.success) {
                      // data.redirect contains the string URL to redirect to
                      $('#companyselect', $(parent.document)).html(data.success);
                      parent.$.fancybox.close();
                      }
                      else {
                        $("#ajax-loading").hide();
                        $(frm).find('textarea,select,input').removeAttr('disabled');
                        $("#send_message_frm").append(data.error);
                      }
                    },
                    "json"                  

           );
        }
    });

The Code from company_add.php returns all the options tags like such:

if ($_POST) {
        // Collect POST data from form
        $name     = filter($_POST['name']);
        $conmail  = filter($_POST['conmail']);
        $addy     = filter($_POST['addy']);
        $confax   = filter($_POST['confax']);
        $city     = filter($_POST['city']);
        $state    = filter($_POST['state']);
        $con      = filter($_POST['con']);
        $conphone = filter($_POST['phone']);
        $zip      = filter($_POST['zip']);
    }
    $search1   = mysql_query("SELECT man_name FROM manufacturers WHERE man_name = '$name'");
    $outcome1  = mysql_fetch_row($search1);
    $num_rows1 = mysql_num_rows($search1);
    $imageid1  = $outcome1[0];
    $imageid1  = filter($imageid1);
    if ($num_rows1 > 0) {
        echo json_encode(array(
            "error" => '<div class="msg-error">A company by that name already exists.</div>'
        ));
    } else {
        $stmnt = mysql_query("INSERT INTO manufacturers (manufacturer_id, man_name, man_address, man_city, man_state,man_zip, man_contact, man_phone, man_fax, man_mail) VALUES ('NULL', '" . $name . "', '" . $addy . "' ,'" . $city . "', '" . $state . "' , '" . $zip . "' , '" . $con . "' , '" . $conphone . "' , '" . $confax . "', '" . $conmail . "'  )");
        //echo "Duplicate WAS found:" . $answer1; 
        mysql_query($answer1);
        //}
        $resp['status'] = 'success';
        if (empty($error)) {
            $nada     = "SELECT man_name FROM manufacturers ORDER BY man_name ASC";
            $resulter = mysql_query($nada);
            $comp1    = '0';
            //Spit out array of companys as select boxes
            $select   = '<option value="">--Select one--</option>';
            while ($result59 = mysql_fetch_array($resulter))
                $select .= '<option value="' . $result59['man_name'] . '">' . $result59['man_name'] . '</option>';
            echo json_encode(array(
                "success" =>$select
            ));
        } else {
            echo json_encode(array(
                "error" => '<div class="msg-error">Error: Unable to add your company at this time</div>'
            ));
        }
    }

I am new to programming and very new to Jquery so I'm hoping someone can see where I'm going wrong. I am using fancybox 2 and php.

回答1:

Have you checked error console for any JavaScript errors?

You can try this anyway:

parent.$('#companyselect').html(data.success);