开发者

PHP QueryString with Array

What am i doing wrong here?

firstpage.php

<html>
    <body>
        <?php
        if (!isset($_GET['error']))
            $error=array();
        else
            $error = $_GET['error'];
        ?>
        <script>
            function clearText(field){
                if (field.defaultValue == field.value) field.value = '';
                else if (field.value == '') field.value = field.defaultValue;

            }
        </script>
        <?php

        foreach($error as $key => $value){
            if ($value != '')
                echo '<p style="color:red;">', $value, '</p>';
        }
        ?>


        <form method="GET" action="validar.php">

            First Name: <input type="text" name="firstname" value="first name here" onFocus="clearText(this)" onBlur="clearText(this)"> <br>
            Last Name: <input type="text" name="lastname" value="last name here" onFocus="clearText(this)" onBlur="clearText(this)"> <br><br>

            <input type="submit" value="SEND">

        </form>

    </body>
</html>

secondPage.php

<?php
# confirmation.php

$firstname = $_GET['firstname'];
$latname = $_GET['lastname'];

?>
<html>
<body>
<h3>Congratulations <?php echo $firstname; $lastname; ?>, you have been successfully registered</h3>

</body>
</html>

validar.php

<?php

    $firstname = $_GET['firstname'];
    $lastname = $_GET['lastname'];

    $error['firstname'] = '';
    $error['lastname'] = '';

    $firstPage = "firstPage.php";
    $secondPage = "secondPage.php";

    $server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . '/';

    if($firstname == "first name here"){
        $firstname == "";
        $error['firstname'] = 'Please introduce firstname <br>';
    }

    if($lastname == "last name here"){
        $lastname == "";
        $error['lastname'] = 'Please introduce last <br>';
    }

    if($error == ''){
        $query_string = '?firstname=' . $firstname;
        header('Location: http://' . $server_dir . $secondPage . $query_string);
    }
    else {

    $query_string = http_build_query($error);
    header('Location: http://' . $server_dir . $firstPage . $query_string);
}

    ?>

ERROR MESSAGE: Access Denied! I'm new to PHP, so i dont know how to build a query string w开发者_如何学编程ith multiple fields error. I'm doing with with an array with key as "field value" cause i think its easier. But i'm not this is the best approach, and even if it is, i'm not sure im doing this good. Any help would be appreciated, John


http_build_query function only builds the query. You will need to prepend it with the question mark to mark the beginning of the query string.

Lets take your code:


    header('Location: http://' . $server_dir . $firstPage . $query_string);

You will need to insert "?" between firstPage and query_string :


    header('Location: http://' . $server_dir . $firstPage . '?' . $query_string);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜