如何使用 PHP 将错误消息添加到页面而不刷新页面?
我正在使用 PHP 来处理表单并处理错误。这些错误工作正常,但问题是我想将其添加到带有我的表单的页面(如果发现错误)而不刷新页面。这是因为用户可以动态地将行添加到我的表单中,并且我不希望这些额外的行丢失(刷新页面时会发生这种情况)。
目前,我的表单 (PHP) 页面上仅包含以下行:
<?=$errorString?>
然后我在 booking-engine.php 中定义 $errorString
,并添加 include 'workshops.php' ;
刷新页面以添加错误。
是否可以在不刷新页面的情况下添加错误,如果可以,我将如何处理?
谢谢,
尼克
完整的 PHP 脚本:
$row_count = count($_POST['name']);
if ($row_count > 0) {
mysql_select_db($database, $connection);
$name = array();
$workshop = array();
$not_found = array();
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name[$i] = mysql_real_escape_string(ucwords($_POST['name'][$i]));
$workshop[$i] = mysql_real_escape_string($_POST['workshop'][$i]);
}
$names = "('".implode("','",$name)."')";
$not_in = Array();
// lets say all names doesn't exist in `conference`
foreach($name as $value) {
// names in array are keys, not values
$not_in[$value] = true;
}
$query = mysql_query("SELECT Name FROM conference WHERE Name IN $names");
while(list($dbname) = @mysql_fetch_row($query)) {
// delete those name from $not_in who exists
unset($not_in[$dbname]);
}
// names in $not_in array are keys, not values
$not_in = array_keys($not_in);
if(empty($not_in)) {
// its ok, all names have been found. do the magic.
for($i = 0; $i < $row_count; $i++) {
$sql = "UPDATE conference SET Workshop = '$workshop[$i]' WHERE Name LIKE '$name[$i]'";
mysql_query($sql);
$body .= "Name: " . $name[$i] . " Workshop: " . $workshop[$i] . "\n\n";
}
// send email
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks-workshop.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}else{
$errorString = "<div id=\"error\">".'<strong>The following name(s) have not been found on our database of bookings</strong>:<div id=\"names\">'.join(', ',$not_in)."</div><div id=\"error-sub\">Please check the name(s) and try submitting your booking again. Each name needs to be identical to the name you first booked on to the conference, as described above.</div></div>";
include 'workshops.php';
}
}
I am using PHP to process a form and handle errors. The errors work fine, but the issue is that I am wanting to add it to the page with my form on (if an error is found) without refreshing the page. This is because the user can add rows dynamically to my form, and I don't want these extra rows to be lost (which happens when the page is refreshed).
At the moment then, I simply have the following line on my form (PHP) page:
<?=$errorString?>
And then I define $errorString
in booking-engine.php, and add include 'workshops.php';
which refreshes the page to add the error.
Is it possible to add the error without refreshing the page, and if so, how would I go about this?
Thanks,
Nick
FULL PHP SCRIPT:
$row_count = count($_POST['name']);
if ($row_count > 0) {
mysql_select_db($database, $connection);
$name = array();
$workshop = array();
$not_found = array();
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name[$i] = mysql_real_escape_string(ucwords($_POST['name'][$i]));
$workshop[$i] = mysql_real_escape_string($_POST['workshop'][$i]);
}
$names = "('".implode("','",$name)."')";
$not_in = Array();
// lets say all names doesn't exist in `conference`
foreach($name as $value) {
// names in array are keys, not values
$not_in[$value] = true;
}
$query = mysql_query("SELECT Name FROM conference WHERE Name IN $names");
while(list($dbname) = @mysql_fetch_row($query)) {
// delete those name from $not_in who exists
unset($not_in[$dbname]);
}
// names in $not_in array are keys, not values
$not_in = array_keys($not_in);
if(empty($not_in)) {
// its ok, all names have been found. do the magic.
for($i = 0; $i < $row_count; $i++) {
$sql = "UPDATE conference SET Workshop = '$workshop[$i]' WHERE Name LIKE '$name[$i]'";
mysql_query($sql);
$body .= "Name: " . $name[$i] . " Workshop: " . $workshop[$i] . "\n\n";
}
// send email
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks-workshop.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}else{
$errorString = "<div id=\"error\">".'<strong>The following name(s) have not been found on our database of bookings</strong>:<div id=\"names\">'.join(', ',$not_in)."</div><div id=\"error-sub\">Please check the name(s) and try submitting your booking again. Each name needs to be identical to the name you first booked on to the conference, as described above.</div></div>";
include 'workshops.php';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 是服务器端的,而你想要做的事情是在客户端。因此,您需要使用(客户端)Javascript 将数据提交到服务器端 PHP 验证,然后使用结果更新页面。请参阅 AJAX 了解背景信息和 jQuery.post() 了解一些实际示例。
PHP is server-side and what you want to do is on the client. So you'll need to use (client-side) Javascript to submit the data to your server-side PHP validation and then update the page with the results. See AJAX for background and jQuery.post() for some practical examples.
您可以使用 javascript 在客户端验证您的表单,可以直接在浏览器中验证,也可以使用 ajax 在服务器端验证。
但是,您需要更改服务器端处理脚本以自动添加访问者添加的额外行,因为可以禁用 JavaScript。
我假设您无论如何都会处理额外的行,因此您不妨在构建表单时将它们添加回表单中......
You can use javascript to validate your form on the client side, either directly in the browser or using ajax to check on the server-side.
However, you will need to change your server-side processing script to automatically add the extra rows that the visitor has added as javascript can be disabled.
I assume you are going to process the extra rows anyway, so you might as well add them back to the form when you're building it...