0

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 with 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

3
  • 1
    Are you getting Access denied (403) from the web server? echo out the result of the string passed to your header() calls 'Location: http://' . $server_dir . $firstPage . $query_string tto be sure that it is a valid URL and is going to where you expect it to go Commented Jul 27, 2011 at 13:18
  • "Access denied" when? What operation does the user perform, and where do you see "Access Denied"? This is unlikely to be anything to do with your query string. Commented Jul 27, 2011 at 13:20
  • output: Location: localhost/php_teste1/treino/… Commented Jul 27, 2011 at 14:31

1 Answer 1

0

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.