从FancyBox2更新父页面上的选择框

时间:2012-11-02 03:40:29

标签: php javascript jquery ajax fancybox-2

我正在尝试在fancybox中提交表单,用户可以将公司添加到模态父页面上的选择框中。我这样做是通过将模态信息提交到将公司添加到我的数据库的脚本。然后我运行查询以将所有更新的公司作为一组标签。然后我试图将该组标签作为jquery更新传递给父页面。我不确定这是最好的方法还是我出错的地方。

我正在尝试使用这篇文章作为指南:

Find element on site from a fancybox iframe

但我的代码有两个问题。 一:fancybox没有关闭 二:父页面上的选择框未更新

我不确定我的成功电话出了什么问题。 Modal页面的代码是:

$("#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"                  

           );
        }
    });

company_add.php中的代码返回所有选项标签,如:

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>'
            ));
        }
    }

我是编程的新手,也是Jquery的新手,所以我希望有人能看到我出错的地方。我正在使用fancybox 2和php。

1 个答案:

答案 0 :(得分:1)

您是否检查过任何JavaScript错误的错误控制台?

无论如何你可以试试这个:

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