Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX表单通信,显示结果消息-如何?_Javascript_Php_Ajax - Fatal编程技术网

Javascript AJAX表单通信,显示结果消息-如何?

Javascript AJAX表单通信,显示结果消息-如何?,javascript,php,ajax,Javascript,Php,Ajax,我是ajax领域的新手,慢慢掌握了它的窍门。我已经构建了一个相当实用的表单,除了一些我正在整理的细节。其中一个原因就是这个问题 我有一个表单,它通过html->ajax->php->mysql的过程,通过ajax成功地更新到mysql数据库。然而,目前绝对没有从php返回到html的通信消息或指示器,以指示成功或错误。由于对使用ajax非常陌生,我们非常感谢您的帮助 使用中的技术:jQuery、JavaScript、html、php HTML表单(它的一部分,而不是完整表单,因为这是不必要的):

我是ajax领域的新手,慢慢掌握了它的窍门。我已经构建了一个相当实用的表单,除了一些我正在整理的细节。其中一个原因就是这个问题

我有一个表单,它通过html->ajax->php->mysql的过程,通过ajax成功地更新到mysql数据库。然而,目前绝对没有从php返回到html的通信消息或指示器,以指示成功或错误。由于对使用ajax非常陌生,我们非常感谢您的帮助

使用中的技术:jQuery、JavaScript、html、php

HTML表单(它的一部分,而不是完整表单,因为这是不必要的):

最后,接收php部分处理此处显示的表单部分,如下所示:

<?php
// include the funcky stuff
include './conf/Funcs.php';
include './conf/DBconfig.php';

// GET the constants
$token = $_GET['token'];
$appID = $_GET['appID'];
$ident = $_GET['ident'];

// =================== TAB 1 ===================
// organisation
if(($ident) == "input_1_1") {
    $userInput = $_POST['input_1_1'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `organisation` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

// contactPerson
if(($ident) == "input_1_2") {
    $userInput = $_POST['input_1_2'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `contactPerson` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

// phoneLandline
if(($ident) == "input_1_3") {
    $userInput = $_POST['input_1_3'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `phoneLandline` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_INT, 22);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

// phoneMobile
if(($ident) == "input_1_4") {
    $userInput = $_POST['input_1_4'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `phoneMobile` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_INT, 22);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

// email
if(($ident) == "input_1_5") {
    $userInput = $_POST['input_1_5'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `email` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

// fax
if(($ident) == "input_1_6") {
    $userInput = $_POST['input_1_6'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `fax` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_INT, 22);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

// address
if(($ident) == "input_1_7") {
    $userInput = $_POST['input_1_7'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `address` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 128);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

实际上,它并不像看上去那么复杂。当您在php中处理ajax请求时,您在php中输出的任何内容都将被返回,您可以在javascript中使用它

所以如果你有
echo'hello'
在php脚本中,hello将被发送回javascript。你几乎可以输出任何东西

JSON是一种方便的输出格式,因为它很容易用两种语言处理。如果您的php中有一个关联数组,其中包含要发回的数据,则只需执行以下操作:

$return = json_encode($my_data);
header('Content-Type: application/json');
echo $return;
然后,您可以在javascript中获取该数据:

$.post( 'handler.php', function( data ) {
  // data now has the data structure from your php in it, ready to use.
});
下面是一个从php发送一些数据的示例(我相信您在实践中会做一些不同的事情,但只是一个适合现有代码的示例):

然后,您可以在javascript中使用它来显示消息:

function doSend_1_1() {
    $.post('./post.4.ConSupAp.php?appID=' + (appID) + '&ident=input_1_1', $('#input_1_1').serialize()).done(function(data) {
        $('#errorText_1_1').html(data.errorText);
    });
}
在这样一个简单的例子中,没有必要使用JSON——我们可以将文本发送回。但希望它能显示出如何根据需要发送更多数据。您在
$return\u data
中输入的任何内容都将发送到javascript.done函数

尝试此操作以指定响应将采用json格式,以便您可以直接使用返回的数据:

function doSend_1_1() {
        $.post('./post.4.ConSupAp.php?appID=' + (appID) + '&ident=input_1_1', 
                $('#input_1_1').serialize(),
                function(data) {
                    $('#errorText_1_1').html(data.errorText);
                },
                'json' );
}

您是否尝试过使用firebug/chrome开发者集成网络选项卡检查通信?我认为这个小东西非常方便,因为您还可以在服务器上看到失败的请求(由ajax php文件的错误位置字符串引起)。根据您从服务器获取数据所使用的jquery函数,如果在格式方面存在任何错误(例如,当您使用$.getJson加载数据时,结果将不是有效的json(例如,由于mysql错误))@serjoscha Firebug不会报告任何问题,因为没有问题,马上我不知道如何或在哪里设置我想要的东西的沟通因素,这是一个问题。@Cassandra这是一个社区网站,你对编辑的评论不适合你的问题。如果您对编辑有顾虑,那么。把它放在问题里面是不合适的。@James谢谢James,你能用我上面的代码给我一点演示代码吗,这样我就可以感觉到如何使它工作了?谢谢。@Cassandra,我用一个粗略的例子更新了我的答案,根据$stmt->execute返回,这个例子只会返回一条成功或失败的消息。不是你在现实中可能如何处理它,但希望能展示返回和处理数据的机制。@James非常感谢James,非常感谢。:)尽管出于某种原因,它不太管用。Firebug没有告诉我为什么。。。响应如预期的那样,但在中根本没有显示任何内容。我不明白为什么/正如预期和期望的那样,新的替代版本可以完美地工作。詹姆斯,那里的帮助太棒了,非常感谢。:)
$error_message = '';
if(($ident) == "input_1_1") {
    $userInput = $_POST['input_1_1'];
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `organisation` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        if( $stmt->execute() ) {
            $error_message = 'Success';
        } else {
            $error_message = 'Error';
        }
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}

$return_data = array( 'errorText' => $error_message );
echo json_encode($return_data);
function doSend_1_1() {
    $.post('./post.4.ConSupAp.php?appID=' + (appID) + '&ident=input_1_1', $('#input_1_1').serialize()).done(function(data) {
        $('#errorText_1_1').html(data.errorText);
    });
}
function doSend_1_1() {
        $.post('./post.4.ConSupAp.php?appID=' + (appID) + '&ident=input_1_1', 
                $('#input_1_1').serialize(),
                function(data) {
                    $('#errorText_1_1').html(data.errorText);
                },
                'json' );
}