1

提交我的 html 表单后,我需要添加一条成功消息。我尝试了一些我在网上找到的示例,甚至尝试编写自己的示例,但它们要么不起作用,要么阻止表单提交。我对 PHP 还是很陌生,所以感谢任何帮助。我对消息的显示方式不太挑剔,只要它在那里。我怎样才能做到这一点?谢谢。

<?php
try {
    $dbh = new pdo('sqlsrv:Server=xxxx,1433;database=xxxx', 'xxxx', 'xxxx');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $tsql = "INSERT INTO testtable (date, position, referral) VALUES (?, ?, ?)";
    $stmt = $dbh->prepare($tsql);
    $stmt->execute(array(
        $_REQUEST['date'],
        $_REQUEST['position'],
        $_REQUEST['referral']
    ));

    /* The following call to closeCursor() may be required by some drivers */
    $stmt->closeCursor();

    // and now we're done; close it
    $dbh = null;
}
catch (PDOException $e) {
    die("PDO Exception: " . $e->getMessage());
}
catch (Exception $e) {
    die("Exception: " . $e->getMessage());
}
?>
4

4 回答 4

0

您可以通过编写这样的一段代码来简单地检查您的 HTML 表单是否已提交。

<?php
    if(isset($_POST['submit'])) {
        echo "Form Submitted";
    } 
?>

和这样的 HTML 表单:

<form >
    <input class="button" type="submit" name="submit" value="submit"/>    
</form>

这只是您根据需要设置的示例。

于 2013-06-07T19:31:13.693 回答
0

您可以设置会话变量,然后在视图文件中检索并清除它。

于 2013-06-07T19:19:33.993 回答
0

HTML:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="ajax-example.js"></script>
</head>
<body>
<h1>ajax form simple example</h1>
<form id="foo">
    <div class="message"></div>
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />
    <input type="submit" value="Send" />
</form>
</body>

Javascript(ajax-example.js):

// bind to the submit event of our form
$("#foo").submit(function(event){

    // fire off the request to /form.php
    $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData,
            error: function(){
               alert('error');
            },
            beforeSend: function() {
               //here you can put do thing cool like a $('element').html('loading-image.gif')
            },
            success : function(data){
                $('.message').html('success!! '+data);
            }
    });
});

PHP (form.php)

<?php
try {
    $dbh = new pdo('sqlsrv:Server=xxxx,1433;database=xxxx', 'xxxx', 'xxxx');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $tsql = "INSERT INTO testtable (date, position, referral) VALUES (?, ?, ?)";
    $stmt = $dbh->prepare($tsql);
    $stmt->execute(array(
        $_REQUEST['date'],
        $_REQUEST['position'],
        $_REQUEST['referral']
    ));

    /* The following call to closeCursor() may be required by some drivers */
    $stmt->closeCursor();

    // and now we're done; close it
    $dbh = null;

   return 'PUT SOMETHING HERE IF YOU WANT';
}
catch (PDOException $e) {
    die("PDO Exception: " . $e->getMessage());
}
catch (Exception $e) {
    die("Exception: " . $e->getMessage());
}
?>

欲了解更多信息,请搜索jquery ajax submit

于 2013-06-07T19:53:48.430 回答
0

你可以简单地echo "Success!"后行$dbh = null;

如果在 try {} 和 catch() 之间发生错误(异常)。它将被 catch() 行捕获,这将导致die()'s显示其中一个。如果您到达该$dbh = null行,则您的查询成功。

于 2013-06-07T19:21:19.233 回答