PHP中的警告框和导航到另一个页面

时间:2014-04-17 06:44:51

标签: javascript php

我正在使用javascript检查用户名和密码是否为空。如果其中一个为空,则显示javascript警报并且PHP脚本不起作用,即不应发生用户名和密码验证,并且应再次显示登录页面。有没有简单的代码可以做到这一点?

没有人需要建立整体形式。我已经为它的验证构建了登录表单和PHP脚本,我只想知道PHP中是否有任何方法或函数可以在输入空用户名/密码并提交时停止脚本

2 个答案:

答案 0 :(得分:0)

试试这个

Php代码:

 <p>
    <label for="first">User Name:</label>
    <input type="text" name="First Name" id="first" />
 </p>
 <p>
     <label for="last">Password:</label>
     <input type="text" name="Last Name" id="last" />
 </p>

 <input type="submit" name="Submit" id="button2" value="Submit" onClick="javascript:verify()"/>

JavaScript代码:

function verify() {

   if (document.getElementById('first').value=="") {
      alert("Please Enter Your Name");
      return false;
   }
   else if (document.getElementById('last').value=="") {
      alert("Please Enter Your Password"); 
      return false;
   }
   else {
       document.form.submit();
   }

}

工作模式:http://jsfiddle.net/NWWL4/24/

答案 1 :(得分:-1)

      It took me awhile to get this right. I have my form in my html index 
    page. On a closed question here, I read this couldn't be done.
    This php works VERY well with my form. 
    It generates a Javascript alert for good and bad results and 
    links back to the index page in either instance.
    If you look at the 2nd echo, you'll see where you can redirect to 
    another page. I just loaded same page to clear form.
    If you want to see the ccs, just ask.

    PHP:
                <?php
                if(isset($_POST['submit'])) {
                    $to = "admin@blahblah.com";
                    $name_field = $_POST['name'];
                    $email_field = $_POST['email'];
                    $subject_field = $_POST['subject'];
                    $message = $_POST['message'];
                    $body = "From: $name_field\n E-Mail: $email_field:\n Subject: $subject_field\n Message: $message\n";
                    }
                if(empty($name_field) || empty($email_field) || empty($subject_field) || empty($message)) {
                    echo '<script type="text/javascript">alert("There is a problem, please check the fields");window.history.go(-1);</script>';
                }
                else {
                if(mail($to, $subject_field, $body)) {
                    echo '<script type="text/javascript">alert("Message successfully sent");window.location = "https://www.blahblah.com/";</script>';
                    }}
                exit;
                  ?>

    html:
               <div class="clear"></div>
             <div class="container">
           <div class="row">
         <div class="col-md-3">
      <form role="form" action="php\mailer.php" method="post" id="form">  
              <div class="form-group">
                <input name="name" type="text" class="form-control" id="name" placeholder="Your Name" maxlength="30">
              </div>
              <div class="form-group">
                <input name="email" type="text" class="form-control" id="email" placeholder="Your Email" maxlength="30">
              </div>
              <div class="form-group">
                <input name="subject" type="text" class="form-control" id="subject" placeholder="Your Subject" maxlength="40">
              </div>
           <div><input type="submit" name="submit" class="btn btn-primary" value="Send Message"></input></div>
             </div>
             <div class="col-md-9">
             <div class="txtarea">
           <textarea name="message" rows="10" class="form-control" id="message"></textarea>
       </form>
          <div class="clear"></div>            
<div class="col-lg-12" style="text-align:center">           
<a class="btn btn-large btn-contact-link" href="#blahblah_home">Home</a>