0

我正在为我在学校的项目做一些添加、编辑和删除操作。添加模块中的代码运行良好,实际上我添加了一些记录。然后,来了Edit模块,一开始还不错,add模块也用了类似的代码。但是当我尝试和尝试时,编辑模块中的帖子是空的。

这是我的编辑代码:

$(".careersEdit").click(function () {
    var careersTableSelect = encodeURIComponent($("input:radio[name=careersTableSelect]:checked").val());
    if (careersTableSelect > 0) {
        $(".careersEditForm_load").show();
        $(".careersEditForm_error").hide();
        $(".careersEditForm").hide();
        var dataStringCareersEdit = 'careersTableSelect=' + careersTableSelect;
        $.ajax({
            type: "POST",
            url: "admin/careers/process/careersEditGet.php",
            data: dataStringCareersEdit,
            beforeSend: function(){
                alert(dataStringCareersEdit);
            },
            success: function () {
                setTimeout("", 5000);
                fetchResult();
            },
            error: function () {
                alert("Post Error");
            }
        });
        function fetchResult() {
            $.ajax({
                url: "admin/careers/process/careersEditGet.php",
                type: "POST",
                dataType: "json",
                success: function (result) {
                    if (result) {
                        $("input#careersEditPosition").val(result['position']);
                        $("input#careersEditCompany").val(result['company']);
                        $("input#careersEditLocation").val(result['location']);
                        $(".careersEditForm_load").hide();
                        $(".careersEditForm").show();
                    }
                },
                error: function () {
                    alert("Fetch Error");
                }
            });
        }
    } else {
        $(".careersEditForm").hide();
        $(".careersEditForm_load").hide();
        $(".careersEditForm_error").show();

    }
});

这是 careersEditGet.php:

<?php
include('connect.php');
error_reporting(0);
$careersTableSelect = $_POST['careersTableSelect'];
//$careersTableSelect = $careersTableSelect + 1;
//echo $careersTableSelect;
$query = "SELECT * FROM atsdatabase.admincareers WHERE refNum ='" . $careersTableSelect . "' LIMIT 0 , 30";
$runQuery = mysql_query($query);
if (!$runQuery) {
    die('Could not enter data: ' . mysql_error());
}
$result = mysql_fetch_row($runQuery);
$array = array(
    'position' => "" . $result[1] . "",
    'company' => "" . $result[2] . "",
    'location' => "" . $result[3] . "",
);
echo json_encode($array);
mysql_close($connection);

?>

是的,代码很丑/错误/废话,我对 jquery 的东西很陌生,大约 3-4 天。对于那些会有所帮助的人,请纠正我。我想学习这个 jquery ajax 的东西。格拉西亚斯

4

4 回答 4

1

也许尝试以更常见的方式传递数据:

改变

data: dataStringCareersEdit,

data: { "careersTableSelect" : careersTableSelect },
于 2013-10-01T10:00:00.030 回答
0

像这样调用你的ajax函数,

$.ajax({
    url: "admin/careers/process/careersEditGet.php",
    type: "POST",
    dataType: "json",
    data: {careersTableSelect: careersTableSelect},
    success: function (result) {
       if (result) {
          $("input#careersEditPosition").val(result.position);// json not array
          $("input#careersEditCompany").val(result.company);// json not array
          $("input#careersEditLocation").val(result.location);// json not array
          $(".careersEditForm_load").hide();
          $(".careersEditForm").show();
       }
    },
    error: function () {
       alert("Fetch Error");
    }
});
于 2013-10-01T09:46:06.337 回答
0

感谢大家为回答这个问题付出的所有努力,我咨询了一位网络开发人员的朋友,教我如何在 jquery 中正确使用 ajax。;)

于 2013-10-04T11:07:07.067 回答
-1

当您从 jQuery.Ajax 发布数据时,您在做一些根本错误的事情。数据应该是一个对象,键应该是服务器端 POST 变量的名称,稍后将在 PHP 中使用......示例:

data : {"server_side_vriable" : "Your_data_to_Post" }

……

var dataStringCareersEdit = 'careersTableSelect=' + careersTableSelect + "&careersTableSelect=" + careersTableSelect;
            $.ajax({
                type: "POST",
                url: "admin/careers/process/careersEditGet.php",
                data: {"careersTableSelect" : dataStringCareersEdit},
                beforeSend: alert(dataStringCareersEdit),
                success: function () {
                    alert("Fetching Result");
                    setTimeout("", 3000);
                    $.ajax({
                        url: "admin/careers/process/careersEditGet.php",
                        type: "GET",
                        dataType: "json",
                        success: function (result) {
                            if (result) {
                                $("input#careersEditPosition").val(result['position']);
                                $("input#careersEditCompany").val(result['company']);
                                $("input#careersEditLocation").val(result['location']);
                                $(".careersEditForm_load").hide();
                                $(".careersEditForm").show();
                            }
                        },
                        error: function () {
                            alert("Fetch Error");
                        }
                    });
                },
                error: function () {
                    alert("Post Error");
                }
            });
于 2013-10-01T09:48:16.077 回答