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.

How to load next page after validating forms

Discussion in 'PHP' started by FoxIX, Apr 9, 2016.

  1. #1
    Hi all,

    I'm currently learning php and have got myself a bit stuck. I've tried searching, but I don't know exactly how I need to phrase it to get the correct answer.

    What I have is a simple two field form which upon clicking 'submit', checks the fields to see if they are empty or not. If they aren't, then an error message is displayed and the user has to fill out the relevant field.

    The only thing is, once everything is checking out happily (e.g. both fields have content), it just stays on the same page and I don't know how to transfer to the next page to be able to process the form content further.

    What I currently have is:

    
    <?php
    session_start();
    
    require 'includes/database.php';
       
    $forename = $surname = "";
       
    $forenameErr = $surnameErr = "";
       
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
         if (empty($_POST["forename"])) {
              $forenameErr = "Forename required.";
         } else {
              $forename = $_POST["forename"];
      }
       
         if (empty($_POST["surname"])) {
              $surnameErr = "Surname Required.";
         } else {
              $surname = $_POST["surname"];
      }
      }
    ?>
    <!DOCTYPE html>
      <html>
        <head>
          <link rel="stylesheet" href="css/style.css" type="text/css">
        </head>
        <body>
          <form method="post" action="index.php">
            <input type="text" name="forename" value="<?php echo htmlspecialchars($forename);?>" /><span class="error"><?php echo $forenameErr;?></span>
      <br /><br />
      <input type="textarea" name="surname" value="<?php echo htmlspecialchars($surname);?>"/><span class="error"><?php echo $surnameErr;?></span> <!-- Style size in CSS -->
      <br /><br />
      <input type="submit" />
      </form>
      </body>
      </html>
    
    Code (markup):
    I have tried changing the action to the page that I want it transferred to, but then the error checking part doesn't work and just loads the next page.

    If anyone could point me in the right direction, or give me some clue as to what I should be looking for or using, would be a great help :)

    Foxy
     
    Solved! View solution.
    FoxIX, Apr 9, 2016 IP
  2. #2
    Just do a check for the following b
     
    if (isset($_POST) && empty($forenameErr) && empty($surnameErr)) {
    //redirect to the page you want
    } 
    
    PHP:
     
    PoPSiCLe, Apr 9, 2016 IP
  3. FoxIX

    FoxIX Well-Known Member

    Messages:
    211
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    140
    #3
    I've just hobbled back to my chair after kicking myself repeatedly. The second I saw isset, I knew what was missing! Thanks for your help. I probably would've been on that for several days :D

    EDIT: I am using header to transfer to the next page, but for some reason it keeps resetting the error variables to empty again and so doesn't load the next page. I tried entering a value of 1 in the error variables and true enough, after pressing the submit button, nothing happens except the values return to 1.

    I didn't think this would be possible seeing as though I declared the values as blank at the top of the page and there is no other reset code in any of the if statements!

    Was hoping I would be able to work this out myself but I've got a mental block telling me that this shouldn't be happening.
     
    Last edited: Apr 9, 2016
    FoxIX, Apr 9, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Hm - could you post the current code?
     
    PoPSiCLe, Apr 9, 2016 IP
  5. FoxIX

    FoxIX Well-Known Member

    Messages:
    211
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    140
    #5
    For some reason (maybe my cache) it has started jumping over to the next page, but the first page doesn't load at all. It just instantly loads the second page which is blank, saying that the post variables don't exist.

    I tried working through the code and it seems that there is an extra closing curly bracket but if I remove it (the one I think it is anyway) I get an error saying unexpected end of file.

    
    <?php
    session_start();
    
    require 'includes/database.php';
    
    $forename = $surname = "";
    
    $forenameErr = $surnameErr = "";
    
     if (isset($_POST) && empty($forenameErr) && empty($surnameErr)) {
      header("Location: generated.php");
      exit();
      } else {
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["forename"])) {
    $forenameErr = "Forename required.";
    } else {
    $forename = $_POST["forename"];
    }
    
    if (empty($_POST["surname"])) {
    $surnameErr = "Surname Required.";
    } else {
    $surname = $_POST["surname"];
    }
    }
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    </head>
    <body>
    <form method="post" action="index.php">
    <input type="text" name="forename" value="<?php echo htmlspecialchars($forename);?>" /><span class="error"><?php echo $forenameErr;?></span>
    <br /><br />
    <input type="textarea" name="surname" value="<?php echo htmlspecialchars($surname);?>"/><span class="error"><?php echo $surnameErr;?></span> <!-- Style size in CSS -->
    <br /><br />
    <input type="submit" />
    </form>
    </body>
    </html>
    
    Code (markup):
     
    FoxIX, Apr 9, 2016 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Bah, just me going a little too quick. Try this (you might have to modify it a little bit).
    
    <?php
    session_start();
    
    //require 'includes/database.php';
    
    $forename = $surname = '';
    $forenameErr = $surnameErr = '';
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
       if (empty($_POST['forename'])) {
         $forenameErr = 'Forename required.';
       } else {
         $forename = $_POST['forename'];
       }
    
       if (empty($_POST['surname'])) {
         $surnameErr = 'Surname Required.';
       } else {
         $surname = $_POST['surname'];
       }
    }
    
    if (isset($_POST['submit']) && empty($forenameErr) && empty($surnameErr)) {
      header('Location: generated.php');
    } else {
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    </head>
    <body>
    <form method="post" action="">
    <input type="text" name="forename" value="<?php echo htmlspecialchars($forename);?>" /><span class="error"><?php echo $forenameErr;?></span>
    <br /><br />
    <input type="textarea" name="surname" value="<?php echo htmlspecialchars($surname);?>"/><span class="error"><?php echo $surnameErr;?></span> <!-- Style size in CSS -->
    <br /><br />
    <input type="submit" name="submit" value="Enter">
    </form>
    </body>
    </html>
    <?php
    }
    ?>
    
    PHP:
    Btw, no need to use double quotes in the variable assignments and such.
     
    PoPSiCLe, Apr 9, 2016 IP
    Bitpalace likes this.
  7. FoxIX

    FoxIX Well-Known Member

    Messages:
    211
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    140
    #7
    Thank you again. I've been playing around with it and have it doing exactly what I want it to now. I did have to change the code slightly as the variables weren't getting passed to the other page:

    
    
    if (isset($_POST['submit']) && empty($forenameErr) && empty($surnameErr)) {
      $_SESSION['forename'] = $_POST['forename'];
      $_SESSION['surname'] = $_POST['surname'];
      header('Location: generated.php');
    
    Code (php):
    And then on the next page I had to change from post to session on calling the variables, but that's no biggy.

    And thanks for the tip about the quotes. The other day I bookmarked an article about when to use single quotes and when to use double quotes. I'll have a read through that before I go any further and end up having to scroll my way through too much code.

    Thanks again :)

    Foxy
     
    FoxIX, Apr 10, 2016 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    There is really no need to use double quotes for anything, but the following situations:
    * if you want to use variables inside a text (for instance directly within echo-statements) - personally, I prefer just concatinating the variables
    echo "this is a $variable" vs echo 'this is a '.$variable.''
    * if you're using JSON - there are specific problems with JSON encoding return-arrays, and it's recommended that you use double quotes for this.
    this will work: json_encode("content"=>"$content","infotype"=>"$infotype")
    this will not: json_encode('content'=>$content,'infotype'=>$infotype)

    Apart from that, there is really no reason to use double quotes for anything.
     
    PoPSiCLe, Apr 10, 2016 IP
  9. FoxIX

    FoxIX Well-Known Member

    Messages:
    211
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    140
    #9
    Well that's certainly simple enough! I don't know about JSON yet.
     
    FoxIX, Apr 10, 2016 IP