简体   繁体   中英

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

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

Try window.location.href = '/index.html inside 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;
}

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

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

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

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

windows.location("index.php").

but it's now good way.

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

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:

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

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:

http://jsfiddle.net/GAk8C/

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

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

And use the javascript validation method as

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

    }
</script>

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

in head of HTML you put like that

<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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM