显示使用 PHP 和 JavaScript 的警告框
showing alert box using php and javascript
if (!(isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo
"<script type='"text/javascript'">".
"window.alert('You must enter your full name.');".
"</script>";
exit;
}
上面的代码在register.php
文件中。我在index.html
中有 html 表单.当我在没有full name
的情况下发布表格时.它显示alert
但页面卡在register.php
(空白页)。我想在页面上显示alert
index.html
或者至少重定向到index.html
。怎么做???
在 script
中尝试window.location.href = '/index.html
if (! (isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo "<script type='"text/javascript'">window.alert('You must enter your full name.');window.location.href = '/index.html';</script>";
exit;
}
这样做:
if (!(isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo
"<script type='"text/javascript'">".
"window.alert('You must enter your full name.');".
"top.location = 'index.html';".
"</script>";
exit;
}
您的exit
命令正在停止执行,这就是为什么它卡在 register.php。
如果要重定向到其他页面:http://www.cyberciti.biz/faq/php-redirect/
做 1 件事。将此 if 条件放在同一页面中。并在同一页面上提交您的表格。这是更好的主意。如果您不需要注册.php请出于其他原因提交文件。它可以通过以下方式完成
windows.location("index.php").
但现在这是好方法。
<?php
if (!(isset($_POST['fullname']) && strlen($_POST['fullname']))) {
echo
"<script type='"text/javascript'">".
"window.alert('You must enter your full name.');".
'window.location.href="index.html";'.
"</script>";
exit;
}
您需要完全在客户端执行此操作 - 甚至不需要访问服务器来确定是否填写了全名字段。
在注册页面上尝试类似以下内容的操作:
<form id="myform" action="register.php" method="post">
Full Name: <input type="text" name="fullname" />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript">
document.getElementById('myform').onsubmit=function(){
if(!this.fullname.value) {
alert("You must enter your full name")
//stop form submission
return false
}
//continue on to register.php
return true
}
</script>
工作 JS 小提琴:
http://jsfiddle.net/GAk8C/
当您使用 PHP 发布表单时,它会将浏览器重定向到该页面。为什么不在提交表单之前提醒错误消息?下面是一个简短的示例:
<form name="contactForm" action="register.php">
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" />
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</form>
<script>
$(function() {
$('#contactForm').on('submit', function() {
// validate and process form here
if( !$("#name").val() ) {
alert('You must enter a valid name!');
return false;
}
});
});
</script>
我认为您这样做是为了验证该字段。最好在 index.html 页面本身中进行简单的验证。用在索引的 HTML 代码中.html PGE 在提交按钮中添加 folloimg
<input type="submit" name="submit" value="yourvalue" onclick="return validateName()">
并使用 javascript 验证方法作为
<script language="javascript" >
function validateName(){
if(document.getElementById("idOfFirstNameField").value==""){
alert("Please enter your name");
document.getElementById("idOfFirstNameField").focus();
return false;
}
}
</script>
更好的方法,你可以在索引上给出javascript.html本身。 因此,当用户不输入全名时,它不会重定向到注册.php。 它将保持在同一页面上,并在索引上显示警报错误消息.html
在 HTML 的头部,你这样放
<head>
function validate(){
fname=document.getElementByID('fname').value;
if (fname==""){
alert("Please provide full name.");
return false;
}else{
window.location.href = 'register.php';
}
}
</head>
<body>
<form name="adminform" src="register.php" onsubmit="return validate();">
<input type="text" id="fname" name="fname" value=""/>
</form>
</body>
如果用户输入全名,那么它将重定向到注册.php
我这样做的方式。在注册中.php文件:
<?php session_start();
if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['subject'])) || (empty($_POST['message']))) {
$_SESSION['mensaje']="<script language='"javascript'">alert('All fields are mandatory')</script>";
header("Location: ../contact.php");
exit;
}
在文件的第一行中,您将在出现错误时重定向(在本例中为联系人.php)
<?php session_start();
if(isset($_SESSION['mensaje'])){
echo $_SESSION['mensaje'];
}
session_destroy();?>
<!DOCTYPE html>
您也可以使用相同的方式显示成功消息。希望对您有所帮助。
- 在Resharper中禁用javascript类型强制警告
- 为什么JavaScript对话框(警告、确认)会停止页面重绘
- 如何重写此 Javascript 代码以避免 JSHint 警告
- Java 警告 (?) 在 Eclipse (Juno) 中显示为 JavaScript 代码
- PHP/Javascript会话超时并发出警告
- 如何让我的html5功能不兼容警告使用javascript工作
- 当用户在Javascript中触摸空白屏幕时,警告框关闭
- Javascript 增量 (++) 警告
- 使用 JavaScript 显示错误消息,不带 OUT 警告框
- 在 javascript 中动态显示一个带有换行符的警告框
- 检测 JavaScript 警告
- 显示使用 PHP 和 JavaScript 的警告框
- 离开站点警告 Javascript
- 在表单html上编程php和警告javascript
- 警告Javascript数组对象显示为空
- Bootstrap 3.2.0警告javascript混淆
- Android windows已经集中警告javascript界面
- Firefox扩展警告:JavaScript命名空间污染
- 包括谷歌地图API多次警告javascript/mvc
- 警告javascript不工作