未捕获的ReferenceError:未定义数据

时间:2014-10-22 18:02:37

标签: javascript php jquery mysql ajax

我有这个脚本它应该只检查是否为空,然后将数据发送到response.php,然后将其插入到mysql中,我遇到第一个ajax请求有问题,它确实插入到mysql中但是它永远不会继续使用成功函数,并且我在两个请求上都有完全相同的代码,除了第一个是第一个由4个变量而不是1组成。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        //##### send add record Ajax request to response.php #########
        $("#FormSubmit").click(function (e) {
            var cuenta_sap = $("#cuenta_sap").val();
            var ubicacion = $("#ubicacion").val();
            var codigo_centro_beneficio = $("#codigo_centro_beneficio").val();
            var nombre = $("#nombre").val();
            var datastring = 'cuenta_sap=' + cuenta_sap + '$ubicacion=' + ubicacion +
                    '&codigo_centro_beneficio=' + codigo_centro_beneficio + '&nombre=' + nombre;
            e.preventDefault();
            if (cuenta_sap == '' || ubicacion == '' || codigo_centro_beneficio == '' || nombre == '')
            {
                alert("Please enter some text!");
                return false;
            }

            $("#responds").fadeIn().delay(10000).fadeOut();
            $("#FormSubmit").hide(); //hide submit button
            $("#LoadingImage").show(); //show loading image

            $.ajax({
                type: "POST", // HTTP method POST or GET
                url: "response.php", //Where to make Ajax calls
                dataType: "text", // Data type, HTML, json etc.
                data: datastring, //
                success: function (result) {
                    $("#responds").append(result);
                    $("#cuenta_sap").val(''); //empty text field on successful
                    $("#ubicacion").val(''); //empty text field on successful
                    $("#nombre").val(''); //empty text field on successful
                    $("#codigo_centro_beneficio").val(''); //empty text field on successful
                    $("#FormSubmit").show(); //show submit button
                    $("#LoadingImage").hide(); //hide loading image

                },
                error: function (xhr, ajaxOptions, thrownError) {
                    $("#FormSubmit").show(); //show submit button
                    $("#LoadingImage").hide(); //hide loading image
                    alert(thrownError);
                }
            });
        });

        //##### Send delete Ajax request to response.php #########
        $("body").on("click", "#responds .del_button", function (e) {
            e.preventDefault();
            var clickedID = this.id.split('-'); //Split ID string (Split works as PHP explode)
            var DbNumberID = clickedID[1]; //and get number from array
            var myData = 'recordToDelete=' + DbNumberID; //build a post data structure

            $('#item_' + DbNumberID).addClass("sel"); //change background of this element by adding class
            $(this).hide(); //hide currently clicked delete button

            jQuery.ajax({
                type: "POST", // HTTP method POST or GET
                url: "response.php", //Where to make Ajax calls
                dataType: "text", // Data type, HTML, json etc.
                data: myData, //Form variables
                success: function (response) {
                    //on success, hide  element user wants to delete.
                    $('#item_' + DbNumberID).fadeOut();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    //On error, we alert user
                    alert(thrownError);
                }
            });
        });

    });
</script>

response.php

    <?php
//include db configuration file
include_once("dbtest.php");
if (isset($_POST["nombre"]) && strlen($_POST["nombre"]) > 0) { //check $_POST["content_txt"] is not empty
    //sanitize post value, PHP filter FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH Strip tags,        
    encode special characters .
            $cuenta_sap = filter_var($_POST["cuenta_sap"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $ubicacion = filter_var($_POST["ubicacion"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $codigo_centro_beneficio = filter_var($_POST["codigo_centro_beneficio"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $nombre = filter_var($_POST["nombre"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $insert_row = $db->query("INSERT INTO test(content, model, name, sap_account)     
     VALUES('" . $cuenta_sap . "','" . $ubicacion . "','" . $codigo_centro_beneficio . "', '" . $nombre . "' )");

    if ($insert_row) {
        //Record was successfully inserted, respond result back to index page
        $my_id = $db->insert_id; //Get ID of last inserted row from MySQL
        echo '<li id="item_' . $my_id . '">';
        echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-' . $my_id . '">';
        echo '<img src="images/icon_del.gif" border="0" />';
        echo '</a></div>';
        echo $cuenta_sap . '</li>';
        $db->close(); //clse db connection
    } else {

        //header('HTTP/1.1 500 '.mysql_error()); //display sql errors.. must not output sql errors in 
        live mode .
                header('HTTP/1.1 500 Looks like mysql error, could not insert record!');
        exit();
    }
} elseif (isset($_POST["recordToDelete"]) && strlen($_POST["recordToDelete"]) > 0 &&
        is_numeric($_POST["recordToDelete"])) { //do we have a delete request? $_POST["recordToDelete"]
    //sanitize post value, PHP filter FILTER_SANITIZE_NUMBER_INT removes all characters except   
    digits, plus and minus sign .
            $idToDelete = filter_var($_POST["recordToDelete"], FILTER_SANITIZE_NUMBER_INT);

    //try deleting record using the record ID we received from POST
    $delete_row = $db->query("DELETE FROM test WHERE id=" . $idToDelete);

    if (!$delete_row) {
        //If mysql delete query was unsuccessful, output error 
        header('HTTP/1.1 500 Could not delete record!');
        exit();
    }
    $test->close(); //close db connection
} else {
    //Output error
    header('HTTP/1.1 500 Error occurred, Could not process request!');
    exit();
}
?>

0 个答案:

没有答案