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

0

10 Answers 10

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

This will cause the redirect to occur after the alert is dismissed, which may confuse users (this may be his desired effect though, who knows). I would do the redirect server side.
Maybe so, but they still need to learn. Noobs can't stay noobs forever
Yeah, still learning. But it'll be nice if you do constructive critisism.
0

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/

Comments

0

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.

Comments

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

Comments

0

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/

Comments

0

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>

4 Comments

Client side validation is always good form, but a completely sound application must always validate on the server side after the fact too.
Very good point! +1 for redundant security, but merely wanted to open the OP's eyes to JQuery form validation.
yeah, i heard from somewhere that i shouldn't do any validation with JS, user has control over JS turn on/off. is that true?? (totaly new to JS and jquery)
@HungryDB - that is true, but client side validation can make for a cleaner user experience and as ElGavilan pointed out above you should always validate on the server side as well to prevent any hacking.
0

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>

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.