简体   繁体   中英

How do i pass variables to another page in php?

I have moved over the validation to the bikeInfo.php file. Not much changes have been made to the code, but not sure why the validation is not being processed... appreciate the help!

<?php 
    
    $nameErr = $phoneErr = $emailErr = $sErr = $errorMsg = "";
    $name = $phone = $email = $serial = $type = $formSubmit = $description = "";  
?>
<head>
    <title>Register your bikes!</title> 
</head>
<style>
    body {
        margin-left: auto;
        margin-right: auto;
        text-align: center; 
        padding: 8px;
    }
    div.sellerInfo {
        position: relative; 
        top: 50px;
 
    }
    
    .error {
        position: absolute;
        color: red;
    }
    
    
</style>
<html>
<body>
    <form method="post" action="bikeInfo.php">
    <b style="font-size: 20px;">Bike Information</b>
    </br></br>
        <div class="sellerInfo">
        Name: 
        <input type="text" name="sName" value="<?php echo $name;?>"/>
        <span class="error">* <?php echo $nameErr;?></span>
        <br><br>
        
        Phone: 
        <input type="text" name="sNum" value="<?php echo $phone;?>"/>
        <span class="error">* <?php echo $phoneErr;?></span>
        <br><br>
        
        Email: 
        <input type="text" name="sEmail" value="<?php echo $email;?>"/>
        <span class="error">* <?php echo $emailErr;?></span>
        <br><br>
        
        Serial: 
        <input type="text" placeholder="yy-nnn-cc" name="serial" value="<?php echo $serial;?>"/>
        <span class="error">* <?php echo $sErr;?></span>
        <br><br>
        
        Type: 
        <input type="text" name="type" value="<?php echo $type;?>"/>
        <span class="error">* <?php echo $errorMsg;?></span>
        <br><br>
                
        Description: 
        <textarea name="description" rows="5" cols="50" value="<?php echo $description;?>"></textarea>
        <br><br>

        <input type="submit" name="formSubmit" value="Submit"/>
        </div><br><br><br>
        
        </form> 
</body> 
</html>   

This is my bikeInfo.php file which does validation and displaying of the submitted values. Not sure am i suppose to separate them...

<?php
    //set to empty strings 
    $name = $phone = $email = $serial = $type = $formSubmit = $description = ""; 
    $nameErr = $phoneErr = $emailErr = $sErr = $errorMsg = ""; 
        
    if (isset($_POST["formSubmit"]))
    {   

        if (empty($_POST["sName"])) 
        {
            $nameErr = "Name is required";
            
        } else {
            $name = test_input($_POST['sName']); 
        }
        
        //validate phone number 
        if (empty($_POST["sNum"]))
        {
            $phoneErr = "Phone number is required"; 
        } else {    
            $phone = test_input($_POST['sNum']);
                if (!is_numeric($phone)) //check for letters 
                {
                     $phoneErr = "No letters allowed";
                }
        } 
        
        //validate email 
        if (empty($_POST["sEmail"]))
        {
            $emailErr = "Email is required";  
        } else { 
            $email = test_input($_POST['sEmail']); 
                if (!filter_var($email, FILTER_VALIDATE_EMAIL))
                {  
                    $emailErr = "Invalid email format"; 
                }
        }
        
        //validate serial 
        if (empty($_POST["serial"]))
        {
            $sErr = "Serial number is required"; 
        } else {
            $serial = test_input($_POST['serial']);
                //determing the pattern of the serial no. yy-nnnn-cc
            if (!preg_match("/[0-9][0-9]\-\d{3}\-[a-z]{2}/", $serial))
                {
                    $sErr = "Format is yy-nnn-cc"; 
                }
        }
        
        //validate type 
        if (empty($_POST["type"]))
        {
            $errorMsg = "Type is required";  
        } else {
            $type = test_input($_POST["type"]); 
        }
    }
    
    
    
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
        }
    
    


?>
<html>
<style> 
    .body {
        text-align: center;
        padding: 20px;
    }
</style>
<body>
    <h1 style="text-align:center; padding: 20px">Bike listings</h1>
    <?php
    $listings = $name . "<br>" . $phone . "<br>" . $email . "<br>" . $serial . "<br>" . $type . "<br>" . $description;
    echo "<div style='text-align:center; padding: 50px'>$listings</div>"; 
    ?>
</body>
</html>

I had a little play about refactoring the code so that you could use a single page to perform both the validation and display. Perhaps it may be of use.

<?php

    error_reporting( E_ALL );

    $errors=array();
    $nameErr = $phoneErr = $emailErr = $sErr = $errorMsg = '';
    $sName = $sNum = $sEmail = $serial = $type = $description = '';
    
    
    
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset(
        $_POST['sName'],
        $_POST['sNum'],
        $_POST['sEmail'],
        $_POST['serial'],
        $_POST['type']
    )){
        # modified to accept uppercase chars at end and limited to 2 integers at atart
        $pttn='/[0-9]{2}\-\d{3}\-[a-zA-Z]{2}/';
        
        # config to filter POST vars
        $args=array(
            'sName'         =>  FILTER_SANITIZE_STRING,
            'sNum'          =>  FILTER_SANITIZE_STRING,
            'sEmail'        =>  FILTER_SANITIZE_EMAIL,
            'serial'        =>  FILTER_SANITIZE_STRING,
            'type'          =>  FILTER_SANITIZE_STRING,
            'description'   =>  FILTER_SANITIZE_STRING
        );
        
        # test for unaccounted POST fields - possibly malicious
        foreach( $_POST as $field => $value ){
            if( !in_array( $field, array_keys( $args ) ) ){
                $errors[]=sprintf('Unknown field %s',$field);
            }
        }
        
        
        if( empty( $errors ) ){
        
            # rebuild the POST array with only filtered values
            $_POST=filter_input_array( INPUT_POST, $args );
            
            # extract known values from POST array into variables
            extract( $_POST );
            
            
            #error messages
            $nameErr=empty( $sName ) ? 'Name is required' : '';
            $phoneErr=empty( $sNum ) ? 'Phone number is required' : '';
            $emailErr=empty( $sEmail ) ? 'Email is required' : '';
            $sErr=empty( $serial ) ? 'Serial number is required' : '';
            $errorMsg=empty( $type ) ? 'Type is required' : '';
            
            
            # Validate particular variables
            $sEmail=filter_var( $sEmail, FILTER_VALIDATE_EMAIL );
            # to filter the phone number might remove leading zero and thus appear invalid
            
            # possibly reassign error message variables
            if( !preg_match( $pttn, $serial ) )$sErr='Invalid Serial. The format is: yy-nnn-cc';
            if( !$sEmail )$emailErr='Invalid email format';
            if( !$sNum )$phoneErr='Invalid phone number. No letters allowed!';
            
            
            
            # save to database, email somewhere, write text etc etc
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Register your bikes!</title>
        <style>
            body {
                margin:auto;
                text-align:center; 
                padding:8px;
            }
            div.sellerInfo {
                top:50px;
            }
            .error {
                position:absolute;
                color:red;
            }
            section{
                margin:2rem auto;
                width:300px;
                padding:1rem;
                display:block;
                border:1px solid black;
            }
            section *{
                text-align:left;
            }
        </style>
    </head>
    <body>
        <form method='post'>
            <b>Bike Information</b>
            <div class='sellerInfo'>
                <div>
                    Name: 
                    <input type='text' name='sName' value='<?php echo $sName;?>' />
                    <span class='error'>* <?php echo $nameErr;?></span>
                </div>
                <div>
                    Phone: 
                    <input type='text' name='sNum' value='<?php echo $sNum;?>' />
                    <span class='error'>* <?php echo $phoneErr;?></span>
                </div>
                <div>
                    Email: 
                    <input type='text' name='sEmail' value='<?php echo $sEmail;?>' />
                    <span class='error'>* <?php echo $emailErr;?></span>
                </div>
                <div>
                    Serial: 
                    <input type='text' placeholder='yy-nnn-cc' name='serial' value='<?php echo $serial;?>' />
                    <span class='error'>* <?php echo $sErr;?></span>
                </div>
                <div>
                    Type: 
                    <input type='text' name='type' value='<?php echo $type;?>' />
                    <span class='error'>* <?php echo $errorMsg;?></span>
                </div>
                <div>   
                    Description: 
                    <textarea name='description' rows='5' cols='50' value='<?php echo $description;?>'></textarea>
                </div>
                
                
                <input type='submit' />
            </div>
        </form>
        
        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' && isset(
                $sName,
                $sNum,
                $sEmail,
                $serial,
                $type,
                $description
            )){
                if( empty( $errors ) ){
                    
                    printf('
                        <section>
                            <h1 style="text-align:center; padding: 20px">Bike listings</h1>
                            <ul>
                                <li>%s</li>
                                <li>%s</li>
                                <li>%s</li>
                                <li>%s</li>
                                <li>%s</li>
                                <li>%s</li>
                            </ul>
                        </section>',
                        $sName,
                        $sNum,
                        $sEmail,
                        $serial,
                        $type,
                        $description
                    );
                }else{
                    foreach( $errors as $error )printf('<div class="error">%s</div>',$error);
                }
            }
        ?>
    </body> 
</html>
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM