Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Creating function for forms

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #203890
    Aitch
    Participant

    Please I have a code like if($_SERVER
    [‘REQUEST_METHOD’] ==
    ‘POST’){/* check for all input */} the code is working well but I want to like put it in a function, so I can use it that if the code is true then the processing can proceed to the confirmation page which is on another page.

    #203907
    makeshift67
    Participant
    <?php
    function post_process(){
      if(isset($_POST['uniqueVariableName'])){
        // do my stuff
      }
    }
    #203962
    Aitch
    Participant

    Maybe you don’t understand my explanation.

    The page is like this. Registration.php
    <?php
    /define variables and set to empty/
    $fnameErr=$lnameErr=$usernameErr=$passErr;
    $fname=$lname=$username=$pass;

    //validate form input

    function validate(){
    if($_SERVER[“REQUEST_METHOD”]==”POST”){
    if(empty($_POST[“fname”])){
    $fnameErr= “Name is required”;}

    else{
    

    $fname= test_input($_POST[“fname”]);
    /* check if name contains only letters and whitespace*/
    If (!preg_match(“/^[a-zA-Z]*$/”, $fname)){$fnameErr=”only letters and white space allowed”;}
    }
    if(empty($_POST[“lname”])){
    $lnameErr= “Name is required”;}

    else{
    

    $fname= test_input($_POST[“lname”]);
    /* check if name contains only letters and whitespace*/
    If (!preg_match(“/^[a-zA-Z]*$/”, $lname)){$lnameErr=”only letters and white space allowed”;}
    }
    if(empty($_POST[“username”])){
    $usernameErr= “Name is required”;}

    else{
    

    $fname= test_input($_POST[“username”]);
    /* check if name contains only letters and whitespace*/
    If (!preg_match(“/^[a-zA-Z]*$/”, $username)){$usernameErr=”only letters and white space allowed”;}
    }
    if(empty($_POST[“pass”])){
    $passErr= “Name is required”;}

    else{
    

    $pass= test_input($_POST[“pass”]);
    /* check if name contains only letters and whitespace*/
    If (!preg_match(“/^[a-zA-Z]*$/”, $pass)){$passErr=”only letters and white space allowed”;}
    }
    function test_input($data){
    $data =trim($data);
    $data= stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;
    }
    }?>

    <form name=”registration” id=”registration” action=”” method=”POST”>
    <input type=”text” name=”fname” value=<?php echo $fname; ?>/>
    <input type=”text” name=”lname” value=<?php echo $lname; ?>/>
    <input type=”text” name=”username” value=<?php echo $username; ?>/>
    <input type=”password” name=”pass” value=”password”/>
    <input type=”submit” name=”submit” value=”submit”/>
    </form>
    <?php
    if(validate()==true){

    header(“location:email-confirmation.php”);}?>

    Now I want the page to proceed to the confirmation page where the php processing is after the validate is true.

    #203966
    Anonymous
    Inactive

    Maybe you don’t understand my explanation.

    I’m pretty sure he does. Maybe you don’t understand his answer.

    The following is not good code. It makes assumptions about the validations required that are wrong. However, it is similar to what you currently do for validation but in the manner @traq suggests. Please use the principle rather than the code. Also, bear in mind that the === true is important.

    &lt;?php
    
    function validate($input, $fields) {
        $errors = array();
    
        foreach($fields as $field) {
            if(empty($input[$field])) {
                $errors[$field] = "absent";
            } else if(!preg_match("/^[a-zA-Z]*$/", $input[$field])) {
                $errors[$field] = "invalid";
            }
        }
    
        if(empty($errors)) {
            return true;
        }
        return $errors;
    }
    
    $fields = array("fname", "lname", "username", "pass");
    
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(validate($_POST, $fields) === true) {
            header("location:email-confirmation.php");
        }
    }
    
    #203976
    Aitch
    Participant

    Thanks for all the response. But the validate function was not included in the main code but I just want the registration.php page to to send the form info to the email-confirmation.php page after all the inputs has been checked.

    #203980
    Anonymous
    Inactive

    I just want the registration.php page to to send the form info to the email-confirmation.php page

    Huh. You’re right – I don’t think either of us realised that was your main question.

    Use include.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.