使用 php 和 javascript 显示警告框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19698526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:52:49  来源:igfitidea点击:

showing alert box using php and javascript

javascriptphpjqueryhtml

提问by HungryDB

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

   exit;             
}

The above code is in the register.phpfile. I have html form in the index.html. when i post the form without full name. it displays alertbut page gets stuck at register.php(blank page.) i want to display the alerton index.htmlpage or atleast get redirected to index.html. how to do it???

上面的代码在register.php文件中。我在 .html 中有 html 表单index.html。当我发布没有full name. 它显示alert却页面卡在register.php(空白页。)我想显示alertindex.html页面或至少重定向到index.html。怎么做???

采纳答案by Tushar Gupta - curioustushar

Try window.location.href = '/index.htmlinside script

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.');window.location.href = '/index.html';</script>"; 
   exit;
}

回答by jondinham

Do it this way:

这样做:

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

回答by ElGavilan

Your exitcommand is stopping execution, that is why it gets stuck at register.php.

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

If you want to redirect to another page: http://www.cyberciti.biz/faq/php-redirect/

如果您想重定向到另一个页面:http: //www.cyberciti.biz/faq/php-redirect/

回答by Ronak Patel

Do 1 thing.. Put this if condition in the same page. and submit your form on the same page. it's better idea. if you don't need register.php file for different reason. It can be done by

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

windows.location("index.php").

but it's now good way.

但现在是个好方法。

回答by srain

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

回答by Pastor Bones

When you are posting a form using PHP, it redirects the browser to that page. Why not alert the error message before submitting the form? Here is a short example:

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

回答by Sean Johnson

You'll want to do this completely client side - a trip to the server isn't even necessary in order to determine whether or not the fullname field was filled out.

您需要完全在客户端执行此操作 - 甚至不需要访问服务器即可确定是否填写了全名字段。

Try something like the following on your registration page:

在您的注册页面尝试类似以下内容:

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

Working JS Fiddle:

工作JS小提琴:

http://jsfiddle.net/GAk8C/

http://jsfiddle.net/GAk8C/

回答by Vishal

I Think You are doing this for validation of the field. It is better to do the simple validation in index.html page itself. use in your html code in the index.html pge add the folloimg in your submit button

我认为您这样做是为了验证该领域。最好在 index.html 页面本身做简单的验证。在 index.html pge 的 html 代码中使用在提交按钮中添加 folloimg

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

And use the javascript validation method as

并使用 javascript 验证方法作为

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

    }
</script>

回答by Ashish Jain

Better way, you can give javascript on index.html itself. so when user will not put fullname then it will not redirect to register.php. it will stay on same page with alert error message means on index.html

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

in head of HTML you put like that

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

if user put full name then it will redirect to register.php

如果用户输入全名,那么它将重定向到 register.php

回答by Javi Ps

The way i do it. In register.php file:

我这样做的方式。在 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;
  }

And in the first lines of the file that you will redirect in case of error (in this case contact.php)

并且在文件的第一行中,您将在出现错误时重定向(在本例中为 contact.php)

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

Also you can use the same way to show a success message. Hope it helps.

您也可以使用相同的方式显示成功消息。希望能帮助到你。