最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

showing alert box using php and javascript - Stack Overflow

programmeradmin3浏览0评论
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???

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

Share Improve this question edited Nov 7, 2013 at 11:28 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Oct 31, 2013 at 4:37 HungryDBHungryDB 5753 gold badges11 silver badges31 bronze badges 0
Add a ment  | 

10 Answers 10

Reset to default 3

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

You'll want to do this pletely 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/GAk8C/

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>

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.

发布评论

评论列表(0)

  1. 暂无评论