我需要在提交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());
}
?>
答案 0 :(得分:0)
您可以设置会话变量,然后在视图文件中检索并清除它。
答案 1 :(得分:0)
您可以在echo "Success!"
行之后$dbh = null;
。{/ p>
如果try {}和catch()之间发生错误(异常)。它将被catch()行捕获,这将导致显示die()'s
之一。如果您到达$dbh = null
行,则表示您的查询已成功。
答案 2 :(得分:0)
您可以通过编写像这样的代码来检查您的HTML表单是否已提交。
<?php
if(isset($_POST['submit'])) {
echo "Form Submitted";
}
?>
HTML形式如下:
<form >
<input class="button" type="submit" name="submit" value="submit"/>
</form>
这只是你想要设置的一个例子。
答案 3 :(得分: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