使用php和javascript显示警报框 if(!(isset($\u POST['fullname'])和&strlen($\u POST['fullname'])){ 回音 "". “window.alert('您必须输入您的全名');”。 ""; 出口 }

使用php和javascript显示警报框 if(!(isset($\u POST['fullname'])和&strlen($\u POST['fullname'])){ 回音 "". “window.alert('您必须输入您的全名');”。 ""; 出口 },javascript,php,jquery,html,Javascript,Php,Jquery,Html,上述代码位于register.php文件中。我在index.html中有html表单。当我在没有全名的情况下发布表单时。它显示警报,但页面卡在register.php(空白页面) 我想在index.html页面上显示警报,或者至少重定向到index.html。 如何做???尝试window.location.href='/index.html内部script if (!(isset($_POST['fullname']) && strlen($_POST['fullname'])

上述代码位于
register.php
文件中。我在
index.html
中有html表单。当我在没有全名的情况下发布表单时。它显示
警报
,但页面卡在
register.php(空白页面)
我想在
index.html
页面上显示
警报
,或者至少重定向到
index.html

如何做???

尝试
window.location.href='/index.html
内部
script

if (!(isset($_POST['fullname']) && strlen($_POST['fullname']))) {
  echo 
  "<script type=\"text/javascript\">".
  "window.alert('You must enter your full name.');".
  "</script>"; 

   exit;             
}
if(!(isset($\u POST['fullname'])和&strlen($\u POST['fullname'])){
echo“window.alert('您必须输入您的全名');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($\u POST['fullname'])和&strlen($\u POST['fullname'])){
回音
"".
“window.alert('您必须输入您的全名');”。
“top.location='index.html';”。
""; 
出口
}

您的
exit
命令正在停止执行,这就是它在register.php被卡住的原因


如果你想重定向到另一个页面:

做一件事。。将此if条件放在同一页中。并在同一页上提交您的表格。 这是个更好的主意。如果出于不同的原因不需要register.php文件。 这可以通过

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;             
}

但现在这是一个好方法。

当您使用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;
}

名称
$(函数(){
$('#contactForm')。在('submit',function(){
//在此验证并处理表单
如果(!$(“#名称”).val(){
警报('您必须输入有效的名称!');
返回false;
}
});  
});

您将希望完全在客户端完成此操作-为了确定是否填写了fullname字段,甚至不需要访问服务器

在注册页面上尝试以下操作:

<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>

全名:
document.getElementById('myform')。onsubmit=function(){
如果(!this.fullname.value){
警报(“您必须输入您的全名”)
//递交表格
返回错误
}
//继续注册register.php
返回真值
}
工作小提琴:


我认为您这样做是为了验证字段。 最好在index.html页面本身中进行简单的验证。 使用 在index.html pge的html代码中,在提交按钮中添加以下内容

<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>

并使用javascript验证方法作为

<input type="submit" name="submit" value="yourvalue" onclick="return validateName()">

函数validateName(){
if(document.getElementById(“idOfFirstNameField”).value==“”){
警告(“请输入您的姓名”);
document.getElementById(“idOfFirstNameField”).focus();
返回false;
}   
}

更好的方法是,您可以在index.html本身上提供javascript。所以,当用户不输入全名时,它就不会重定向到register.php。它将与index.html上的警报错误消息保持在同一页面上

在HTML的头部,您这样放置

<script language="javascript" >
function validateName(){
if(document.getElementById("idOfFirstNameField").value==""){
alert("Please enter your name");
document.getElementById("idOfFirstNameField").focus();
return false;   
    }   

    }
</script>

函数验证(){
fname=document.getElementByID('fname')。值;
如果(fname==“”){
警告(“请提供全名”);
返回false;
}否则{
window.location.href='register.php';
}
}

如果用户输入全名,那么它将按照我的方式重定向到register.php。在register.php文件中:

<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>

这将导致警报解除后发生重定向,这可能会混淆用户(但这可能是他想要的效果,谁知道呢)。我会在服务器端重定向。也许是这样,但他们仍然需要学习。Noobs不能永远留在Noobs是的,还在学习。但是如果你做建设性的批评,那就太好了。客户端验证总是很好的形式,但是一个完全可靠的应用程序也必须在事后在服务器端进行验证。非常好的一点+1用于冗余安全性,但只是想让OP了解JQuery表单验证。是的,我从某个地方听说我不应该用JS进行任何验证,用户可以控制JS的打开/关闭。这是真的吗??(对JS和jquery来说是全新的)@HungryDB-这是真的,但是客户端验证可以带来更干净的用户体验,正如ElGavilan在上面指出的,您也应该始终在服务器端进行验证,以防止任何黑客攻击。
<?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 session_start();
if(isset($_SESSION['mensaje'])){
  echo $_SESSION['mensaje'];
}
session_destroy();?>
<!DOCTYPE html>