在ajax调用响应中加载的php表单未提交

时间:2016-12-19 05:22:52

标签: php jquery ajax forms

我有一个页面,其中列出了捐赠者的链接。当用户点击捐赠者名称时,会在模式窗口内创建一个新选项卡,该窗口以预先填充的形式显示捐赠者详细信息,以便在需要时进行更新。我对标签内容和ajax调用几乎没有经验,所以我的代码可能都发生在ajax中,但在我试图简化它之前,我试图让它全部运行。拜托我。我首先创建了我的表单,通过客户端和服务器端验证。然后我创建了提供链接的页面,并尝试在该供应商的选项卡中使用相同的表单。下面是文件 - (请原谅哑名)fun10.php查询数据库并创建链接。 script10.js监听点击并抓取捐赠者的id并将其传递给update_donor.php,它预先填充并呈现带有验证fns的自引用表单。问题是update_donor.php不会提交。我没有足够的空间来包含所有的update_donor.php,但它是一个自我提交的php表单,带有jquery验证和bootstrap css。

fun.php - 创建链接列表的文件......

 <div id="main">
    <div id="doclist">
        <h2>Donors</h2>
        <?php

                try {
                    $db = new PDO("mysql:host=$servername;dbname=$mydbname", $username, $password);
                    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    // echo "Connected successfully"; 
                    }
                catch(PDOException $e)
                    {
                    echo "Connection failed: " . $e->getMessage();
                    }

                $stmt1 = $db->prepare('SELECT * FROM donors ORDER BY account_number ASC');
                $stmt1->execute();
                $data = $stmt1->fetchAll();

                echo '<ul id="documents">';
                foreach($data as $row) {
                    $html = $row['id'] . '<br>';

                    if($row['donor_fname']) {
                        $html .= $row['donor_fname'] . '<br>';
                    }
                    if($row['donor_lname']) {
                        $html .= $row['donor_lname'] . '<br>';
                    }
                    echo '<li><a href="#" rel="'.$row['id'].'" title="title_'.$row['id'].'">'.$row['donor_fname'].' '.$row['donor_lname'].'</a></li>';
                }
                echo '</ul>';
        ?>
    </div>
    <div id="wrapper">
        <div id="dialog">
            <ul id="tabs">
            </ul>

            <div id="content">
            </div>
        </div>
    </div>
</div>

`

script10.js - 使ajax调用加载update_donor表单的javascript ....

$(document).ready(function() {
$( "#dialog" ).dialog({
    autoOpen: false,
    resizable: true,
    position: { my: "left top", at: "left top", of: "#wrapper" },
    width: 700
});

$("#documents a").click(function() {
    addTab($(this));

});

$(document).on("click", "#tabs a.tab", function() {
    var contentname = $(this).prop("id") + "_content";

    // hide all other tabs
    $("#content div").hide();
    $("#tabs li").removeClass("current");
    $("#" + contentname).show();
    $("#" + contentname + " div").show();
    $(this).parent().addClass("current");
});

$(document).on("click", "#tabs a.remove", function() {
    // Get the tab name
    var tabid = $(this).parent().find(".tab").prop("id");

    // remove tab and related content
    var contentname = tabid + "_content";
    $("#" + contentname).remove();
    $(this).parent().remove();

    if ($("#tabs li.current").length == 0 && $("#tabs li").length > 0) {

        var firsttab = $("#tabs li:first-child");
        firsttab.addClass("current");

        var firsttabid = $(firsttab).find("a.tab").prop("id");
        $("#" + firsttabid + "_content").show();
    }
}); });

function addTab(link) {
if(!$("#dialog").is(":visible")) { 
    $("#dialog").dialog('open');
}


// If tab already exist in the list, return
if ($("#" + $(link).prop("rel")).length != 0)
    return;

// hide other tabs
$("#tabs li").removeClass("current");
$("#content div").hide();

$("#tabs").append("<li class='current'><a class='tab' id='" +
    $(link).prop("rel") + "' href='#'>" + $(link).html() + 
    "</a><a href='#' class='remove'>x</a></li>");

var rel = $(link).prop('rel');

 $.ajax({
    type: 'POST',
    data: { id: rel },
    success: function(response) {
        var special = "<div id='" + $(link).prop("rel") + "_content'></div>";
        $('#content').append(special);
        $('#'+rel+'_content').load('http://localhost:8888/gifts/update_donor.php?id='+rel);
    }
});
$("#" + rel + "_content div").show();}

并且update_donor.php表单......

session_start();
include('messages.php');
include('validation_functions.php');

$donor_id = '';
if(isset($_REQUEST['id'])) $donor_id = $_REQUEST['id'];

if($_SERVER['REQUEST_METHOD'] == 'POST'){
//POPULATE VARIABLES
//VALIDATION FNS
    if(!validDonorType()) {
        header('Location:'. $_SERVER['PHP_SELF']); 
    } else {
    //if ok then enter into db
        $sdtype = ''; if(isset($_SESSION['donortype'])) { $dtype = $_SESSION['donortype']; }    
    $stmt = $db->prepare("UPDATE donors SET type = ?);

        $stmt->execute([$sdtype]);
        $updated = $stmt->fetchColumn();
        if($updated) {
            if(isset($_SESSION['donortype'])) unset($_SESSION['donortype']);

            $_SESSION = array();
            session_destroy();
            // header('Location: google.com');
            exit;
        } //if updated
    }//if valid
}//if posted

    $stmt = $db->prepare("SELECT account_number, type, .... WHERE id = ?");
    $stmt->execute([$donor_id]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $acct_number = $result['account_number'];
    $dtype = $result['type'];
    if($result) {
        //  
    } else {
        echo 'there was a problem executing this request.';
    }
?>
<form class="form-horizontal" id="newDonor" method="post" action="<?php htmlentities($_SERVER['PHP_SELF']); ?>" novalidate>
    //form fields
<input type="submit" class="btn btn-primary" value="Update">
<input type="reset" value="Reset" />
</form>

0 个答案:

没有答案