Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

OK so I have an email form on index.php. I am using mail_check.php to validate that both fields are filled in. (there are more that is being validated, but not included as it is not the issue)

The main issue is that from the mail_check.php I want to be sent back to the index.php with a message in placed in the div id="mailrespond". I have chosen both PHP and Javascript to achieve this.

Index.php

<div id="mailrespond"></div>
    <form method="post" action="mail_check.php">
    <span>Name: <input type="text" name="name" maxlength="30" /></span><br />
    <span>Email: <input type="text" name="email" maxlength="30" /></span><br />
    <input type="submit" value="Sign Up" name="registration">
    </form>
</div>

mail_check.php

if(isset($_POST['registration']))

$name = $_POST['name'];
$email = $_POST['email'];


if(empty($email) || empty($name)){ 
    // if email and name are empty
    //header ("location: index.php");
    ?>
    <script language="javascript" type="text/javascript">
        window.location.replace("index.php");

        function msg() {
            // creating elements are a safer method then innerHTML
            dv = document.createElement('div'); // creates a Div element 
            dv.setAttribute('class', 'error_msg'); // adds error styling
            txt = document.createTextNode('please enter your name and email '); // create the message
            dv.appendChild(txt); // place the text node on the element 

            document.getElementById("mailrespond").appendChild(dv);
            }
            window.onload = msg;

    </script>
    <?php }

The code goes on, but My issue is that I am not getting the feed back messages. I am a little new to this all - if you can help that would be much appreciated! :)

share|improve this question
    
You are being redirected before executing function msg() –  shadow Sep 2 '13 at 11:22
    
You cannot place a location.replace before any other code and you cannot assign a function to onload inside the function you assign onload –  mplungjan Sep 2 '13 at 11:22
    
Did you get your system work without any javascript? –  Your Common Sense Sep 2 '13 at 11:23
    
Not an answer, more of a suggestion. But jqueryvalidation.org is a great way to implement javascript validation. –  CharliePrynn Sep 2 '13 at 11:24
add comment

3 Answers 3

up vote 0 down vote accepted

<---- @downvoter please leave a reason! Thanks

Try this (and don't call a form field name, please since a form can have a name too)

Index.php

<!DOCTYPE html>
<html>
<head>
<title>Mail respond</title>
<script>
window.onload=function() {
  document.getElementById("mailform").onsubmit=function(){
    if (this.fullname.value=="" || this.email.value=="") {
      alert("Please fill in name and email");
      return false;
    } 
    return true; // allow form submission
  }
}
</script>
</head>
<body> 
<div id="mailrespond"></div>
    <form method="post" action="mail_check.php" id="mailform">
    <span>Name: <input type="text" name="fullname" maxlength="30" /></span><br />
    <span>Email: <input type="text" name="email" maxlength="30" /></span><br />
    <input type="submit" value="Sign Up" name="registration">
    </form>
</div>
</body>
</html>

mail_check.php

<?PHP 
  if(isset($_POST['registration']))
  $name = $_POST['fullname'];
  $email = $_POST['email'];


  if(empty($email) || empty($name)){ 
  // if email and name are empty - only seen if JavaScript is turned off
?>

    <h3>You did not fill in name and email</h3>
    <p>please wait to be redirected or click <a href='index.php'>here</a></p>;
    <meta http-equiv="refresh" content="3; url=http://example.com/index.php" />
    <script type="text/javascript">
     window.onload=function() {
        setTimeout(function() {
          window.location.replace("index.php");
        },3000);
     }
</script>
   <?php } ?>
share|improve this answer
    
And here we go again. Downvoting without telling WHY!? –  mplungjan Sep 3 '13 at 6:41
    
Thank you for the help mplungjan, I have edited my work to be similar to yours and it is working well. ps. I didn't down vote :S –  Charlene Turner Sep 3 '13 at 11:27
    
You are welcome. I am sure you didn't - it really galls me when I write some code that I believe in, that someone who disagrees does not give me a chance to learn something new or to defend my suggestions or fix a typo or whatever gave reason for the vote. –  mplungjan Sep 3 '13 at 11:35
    
And another downvote uncommented. People. What is wrong with you? If you disagree with a post, help the poster AND the asker by telling us what you disagree with!!! Fly-by downvoting is completely and utter useless –  mplungjan Sep 6 '13 at 9:59
add comment
 window.onload = msg;

You call a function inside this function

share|improve this answer
add comment

You're redirecting to index.php in the msg function, so it does nothing else.

You should do it by pure PHP, set a component in $_SESSION, redirect by PHP to index.php and in the index.php check if the component exists.

share|improve this answer
    
Thank you SkarXa, So Sessions are a little beyond me at the moment, are you able to give me a quick overview on how you would change the two files? Thank you in advance :) –  Charlene Turner Sep 2 '13 at 11:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.