2013-10-31 204 views
4
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(空白页)。 我想在index.html页面上显示alert页或至少将重定向到index.html。 怎么办?使用php和javascript显示警告框

回答

3

尝试window.location.href = '/index.htmlscript

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

这样来做:

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

这将导致重定向发生在警报解除后,这可能会让用户感到困惑(这可能是他期望的效果,但是,谁知道)。我会做重定向服务器端。 – ElGavilan

+2

也许是这样,但他们仍然需要学习。 Noobs不能永远留下菜鸟 – ElGavilan

+1

是的,仍然在学习。但如果你做出建设性的批评,那会很好。 – HungryDB

0

做1两件事..如果条件在同一个页面将这个。并在同一页面上提交你的表格。 这是个好主意。如果你因为不同的原因不需要register.php文件。 它可以通过

windows.location("index.php"). 

但它现在是好方法。

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

当您使用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> 
+1

客户端验证总是很好的形式,但完全合理的应用程序必须始终在服务器端进行验证。 – ElGavilan

+0

非常好的一点! +1的冗余安全性,但仅仅希望打开OP对JQuery表单验证的眼光。 –

+0

是的,我从某处听说我不应该用JS做任何验证,用户可以控制JS开启/关闭。真的吗?? (总共是JS和jquery的新版本) – HungryDB

0

你要完全做到这一点的客户端 - 服务器一趟甚至没有必要的,以便确定FullName域是否被填写。

尝试像你的注册页面上的以下内容:

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

0

我认为你是在现场的验证这样做。 最好在index.html页面中进行简单验证。 使用 在你的HTML代码中的index.html PGE添加在你提交按钮

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

的folloimg并使用JavaScript验证方法

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

    } 
</script> 
0

更好的方法,你可以给指数的javascript .html本身。所以当用户不会把全名,那么它不会重定向到register.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> 

如果用户把全名,然后它会重定向到register.php

0

我做到这一点。在register.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; 
    } 

在那你会在错误的情况下重定向文件的第一行(在这种情况下,contact.php)

<?php session_start(); 
if(isset($_SESSION['mensaje'])){ 
    echo $_SESSION['mensaje']; 
} 
session_destroy();?> 
<!DOCTYPE html> 

您也可以使用相同的展现成功讯息的方式。希望能帮助到你。