1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP form redirects before checking input

Discussion in 'PHP' started by Lermy, Jul 26, 2012.

  1. #1
    I have a form with basic "name and email" fields and after the user submits the info, it grabs the data into txt file on my server and redirects the user to page2.html.

    The form I'm using for this job works, but not exactly like I would like it. For instance, when someone enters name and email in the form and submits, it doesnt go directly to page2.html.

    It stops for a second on inforedirect.php as if thats some kind of a transition page ?

    And, to make it more interesting, if the user for some reason leaves the fields empty and clicks submit, "all fields are mandatory" warning displays, but not in a popup box, it shows the warning on inforedirect.php 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 inforedirect.php
    
    <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="page2.html"; </script>';
               
    <?
    } else {
    echo "All fields are mandatory";
    ?>
    <script language="javascript">
    alert("All fields are mandatory");
    </script>
    <?
    }
    ?>
    
    Code (markup):

    and this is the form in html

    
    <form method="post" action="inforedirect.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>
    
    Code (markup):
     
    Lermy, Jul 26, 2012 IP
  2. geforce

    geforce Active Member

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #2
    Your best option would be to use javascript to check the form before it submits.
    Here is some code to do this for you :)

    
    <script type="text/javascript">
    function CheckForm()
    {
      var isValid = true;
       if (document.getElementById('name').value == "")
       {
          isValid = false;
       }
       if (document.getElementById('email').value == "")
       {
          isValid = false;
       }
       if (isValid == false)
       {
          alert('All Fields are mandatory');
       }
      return isValid;
    }
    </script>
    <form method="post" action="inforedirect.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" onClick="return CheckForm();" /></fieldset> <br />   
    </form>
    
    HTML:

    The above code is untested but it should work.
     
    geforce, Jul 26, 2012 IP
  3. khalonn

    khalonn Greenhorn

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    You will also have to validate data from forms server side with php, if only need to check if they were submitted empty you can check with php isset, if you need more refined validation you can check lenght or valid email address in the fields.
     
    khalonn, Aug 8, 2012 IP