Jump to content

Pressing 'Submit' on html form brings blank page


LivingLearning

Recommended Posts

Hi all,  Having some problems getting my HTML <form> to work correctly when I press the 'Submit' button.

When I put data in the 'text' field and then press Submit, the page goes 'white' with no response, but the URL is still the same.

What I'm really trying to do at this point, is to just make sure that my <form> and related GetFormData.php file are setup correct as I get ready to try to send data to my mariadb on my local test server.

Have checked out a lot of forum posts, but not finding the missing link, lol.

Using my test web site for fun:

Here are important parts from MainPage Html file (<form> section has 5 q's, I'll pull 1st):

** This line in the <head> section:

    <link href="http://localhost/GetFormData.php" type="text/php" rel="stylesheet">
 

** Here is <form> from <body>:

        <!-- This section is for: Take a quick Godzilla survey /-->
        <div id="survey-div">
            <form id="survey-form" class="MainPgTxtBckgColor" action = "GetFormData.php" method = "post">
                <fieldset id="survey-fieldset">
                    <legend>Take one of our quick surveys.</legend>
                    <p>
                        <label>1. Who is your <strong>favorite monster</strong> character (Godzilla related or not):</label>
                        <input id="FavMonster" type = "text" value = "" />
                    </p>
                    <p>
                        <label>Press 'Submit' when you are done.</label>
                        <input type="submit" value="Submit" name="submit_button">
                    </p>
                </fieldset>
            </form>
        </div>
 

** Here is contents of GetFormData.php file:

<!DOCTYPE html>
<html lang = "en-US">
    
<head>
    <meta charset = "UTF-8">
    <title>GetFormData.php</title>
</head>
    
<body>
    <p>
    <?php
    // define variables and set to empty values
    $favmonsterErr = "";
    $favmonster = "";

    // verify that field is not empty
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["favmonster"])) {
            $favmonsterErr = "Favorite Monster is required";
        } else {
            $favmonster = test_input($_POST["favmonster"]);

    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$favmonster)) {
      $favmonsterErr = "Only letters and white space allowed"; 
        }
    }
    // validate all form data
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    ?>

</body>
</html>

** Am I missing something with how <form> Submits work or am I just, missing something!

Link to comment
Share on other sites

You need to have a name attribute in your input aswell, id doesn't work with Post just names. Otherwise you don't send any value to php logic to work with.

 

   <input id="FavMonster" name="FavMonster" type = "text" value = "" />

Link to comment
Share on other sites

Hi and thanks for the response.

I checked all my <input> lines and made sure they all have a 'name=' entry and tested it again.  I get same results.

I have both my MainPage.html and GetFormData.php file in my 'htdocs' folder as expected ad I read that a regular browser check of this won't work because PHP is not understood by regular browser session without going through my Apache Web Server.

So, with that up and going, here is my result when I hit the Submit button:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\GetFormData.php on line 38

Atleast this is something different then I've been getting so far.

So, i'm at this point, is there anything wrong with the syntax of my GetFormData.php file from what anyone can see.  My eyes are crossing, lol.

I've run through syntax checker, but you know how good that is.

Please let me know, i'm very interested in keeping this going so i can figure it out and continue learning and moving forward.

I'm sure it's pretty straight forward, I just don't know enough yet.

Link to comment
Share on other sites

Hi there,  I think I got that issue worked out. (But can't be sure, since I only get a blank page when I hit the submit button now and no other messages).

Any thoughts on what I'm missing for this 'blank page' issue and how could I better set this up to see if it is working correctly.

My next step after this is to pass it to the database, but I figured I'd try to setup each piece step-by-step so I could test/see how it works better.

When you press the 'submit' button, what should people really see.

Thanks,

Any chance anyone would be interested in helping me more via 'pm' or is this the best route.  I've done all the reading I can, but can't quite get to the carrot!!

 

Link to comment
Share on other sites

 your input field  look like this FavMonster  where as in your field definition they are written like  this "favmonster" they need to match  also like dsonesuk

said your missing  curly } right before your closing delimiter ?>

Link to comment
Share on other sites

  • 1 month later...

index.html

Give the "Text Input" a name not an ID.

       <!-- This section is for: Take a quick Godzilla survey /-->
        <div id="survey-div">
            <form id="survey-form" class="MainPgTxtBckgColor" action = "GetFormData.php" method = "post">
                <fieldset id="survey-fieldset">
                    <legend>Take one of our quick surveys.</legend>
                    <p>
                        <label>1. Who is your <strong>favorite monster</strong> character (Godzilla related or not):</label>
                        <input name="favmonster" type = "text" value = "" />
                    </p>
                    <p>
                        <label>Press 'Submit' when you are done.</label>
                        <input type="submit" value="Submit" name="submit_button">
                    </p>
                </fieldset>
            </form>
        </div>

 

GetFormData.php
 Watch here how to print code output

 <?php
    // define variables and set to empty values
    $favmonsterErr = "";
    $favmonster = "";

    // verify that field is not empty
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (empty($_POST['favmonster'])) {
            echo "Favorite Monster is required";
            echo '<input type="button" style="display: block" value="Back" onclick="window.history.back()" />';
        } else {
            $favmonster = test_input($_POST['favmonster']);
            echo "You Favorite Monster is: " .$favmonster;
            echo '<input type="button" style="display: block" value="Back" onclick="window.history.back()" />';
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if (!preg_match('/^[a-zA-Z ]*$/',$data)) {
            $data = "Only letters and white space allowed";
        }
        return $data;
    }
    ?> 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...