Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy PHP form redirects before checking input

    The form I'm using for this example is recycled one I've been using for a while, since I don't have knowledge or time to learn PHP. It works, but not exactly like I would like it. For instance, when someone enters name and email in the form and submits, its supposed to redirect the user to freepage.html. However, it "blinks" the actual "freepage.php" with the info that has been input. Usually that lasts 1-2 seconds, and then the redirect begins.

    The other problem I have is if a user doesnt enter the info, "All fields are mandatory" warning displays on that freepage.php, only this time that page doesny "blink" on the screen for a second, it stays there with the warning displayed and the only way to fix it is to click the "back" button in the browser.

    Can somebody point me how could I make this form bit better....

    Here is the freepage.php

    Code:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">google.load("jqueryui", "1.5.2");</script>
    <? 
    	if($_POST['name']!="" and $_POST['email']!=""){
    		
    		
    		$headers	= "From: Sender";
    		$message	= 
      strtoupper($_POST['name'])."
    ".strtoupper($_POST['email'])."
    ";
    		echo str_replace("\n","<br />", $message);
    		$headers2	= "From: Sender <info@gmail.com>\nContent-Type: text/plain; charset=UTF-8; format=flowed\nMIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\nX-Mailer: PHP\n";
    		$message2	= "
    		Hello ".($_POST['name'])."	
    		 
    ";
    		
    		mail("$_POST[email]", "Thanks for entering", $message2, $headers2);
    		
    		
    		$myFile = "info-file.txt";
    		$fh = fopen($myFile, 'a') or die("can't open file");
    		$stringData = "$_POST[name]*$_POST[email]*".$_SERVER['REMOTE_ADDR']."*".date("d-m-Y H:i")."
    ";
    		fwrite($fh, $stringData);
    		fclose($fh);
    		
    
    		?>
            
            echo '<script>document.location="freepage.html"; </script>';
            
            <?
    	} else {
    		echo "All fields are mandatory";
    		?>
            <script language="javascript">
    		alert("All fields are mandatory");
    		</script>
            <?
    	}
    ?>

    and this is the actual form

    Code:
    <form method="post" action="freepage.php" name="popups" id="popups">
                <fieldset>
                <label for=name accesskey=U ><span class="required">*</span> Name</label>
                <br />
                <input name="name" type="text" id="name" size="30" value="" /> 
                <br />
                <label for=email accesskey=E ><span class="required">*</span> Email</label>
                <br />
                <input name="email" type="text" id="email" size="30" value="" />
                <br />
                <input type="submit" value="Submit" id="submit" name="submit" class="button" />
                </fieldset>  
                <br />
          
    </form>

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hi,
    Try this code in "freepage.php":
    PHP Code:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">google.load("jqueryui", "1.5.2");</script>
    <?php
    if($_POST['name']!="" and $_POST['email']!=""){
      
      
      
    $headers  "From: Sender";
      
    $message  
    strtoupper($_POST['name'])."
    "
    .strtoupper($_POST['email'])."
    "
    ;
      echo 
    str_replace("\n","<br />"$message);
      
    $headers2  "From: Sender <info@gmail.com>\nContent-Type: text/plain; charset=UTF-8; format=flowed\nMIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\nX-Mailer: PHP\n";
      
    $message2  "
      Hello "
    .($_POST['name'])."  
       
    "
    ;
      
      
    mail("$_POST[email]""Thanks for entering"$message2$headers2);
      
      
      
    $myFile "info-file.txt";
      
    $fh fopen($myFile'a') or die("can't open file");
      
    $stringData "$_POST[name]*$_POST[email]*".$_SERVER['REMOTE_ADDR']."*".date("d-m-Y H:i")."
    "
    ;
      
    fwrite($fh$stringData);
      
    fclose($fh);

      
    header('Location: freepage.html');
    } else {
        echo 
    'All fields are mandatory<br/>
        <button onclick="history.go(-1)">Go Back</button>
        <script type="text/javascript">
        alert("All fields are mandatory");
        </script>'
    ;
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •