for some reason when i hit submit button it doesnt go run php code.

i have this form below. when i hit sumbit button it should start the php code.

    <form action="" method="post">
        <?php
        //print errors
        if(array_key_exists('reg_error', $_SESSION) && !empty($_SESSION['reg_error']))
        {
            echo $_SESSION['reg_error'];
            unset($_SESSION['reg_error']);
        }
        ?>
    Firstname: <input class = "text" type="text" name="firstname"><br>
    Lastname: <input class = "text" type="text" name="lastname"><br>
    <input type="submit" name="submit" value="Register">
    </form>

so when i hit the button it should run this code.

if(isset($_POST['submit']))
{ ... }

here is my full code.------------------------------------------------------------------------------------------------------------------------------

<?php
session_start();
include("connect.php");

//grab submitted data
if(isset($_POST['submit']))
{
    echo "workinging";  
    $reg_error = "";
    echo "herere";
    //check for empty fields
    if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['username']) || 
    empty($_POST['password']) || empty($_POST['confirm_password']) || empty($_POST['dob_year']) 
    || empty($_POST['dob_month']) || empty($_POST['dob_day']) || empty($_POST['gender']))
    {
        $reg_error .= "Missing field! Please fill in  all field!";
    }
    else 
    {
        //grab submitted data   
        $firstname =  $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];

        $dob_year = $_POST['dob_year'];
        $dob_month = $_POST['dob_month'];
        $dob_day = $_POST['dob_day'];
        $gender = $_POST['gender']; 

        //check len of firstname, lastname, username
        if(strlen($firstname) > 20 || strlen($lastname) > 20 && strlen($username) > 20)
        {   
            $reg_error .= "Fistname, lastnmae and username must be no more than 25 characters!";
        }
        else
        {
            //check len of passwords
            if(strlen($password) > 25 || strlen($password) < 6)
            {
                $reg_error .= "Password must be between 6 and 25 characters!";
            }   
            else
            {
                //compare password
                if($password != $confirm_password)
                {
                    $reg_error .= "Password do not match!";
                }
                else
                {
                    //check for dob is number
                    if(!(is_numeric($dob_year) || is_numeric($dob_month) || is_numeric($dob_day)))
                    {
                        $reg_error .= "Date of birth must be in number form. For example, 1989/06/09";
                    }
                    else
                    {
                        //check value limit of dob
                        if(strlen($dob_year) > 4 || strlen($dob_month)>2 || strlen($dob_day)>2)
                        {
                            $reg_error .= "Date of birth year musb be 4 characters, month and day must be 2 characters.";
                        }
                        else
                        {
                            //check value of dob
                            if($dob_month > 12 || $dob_day > 31)
                            {
                                $reg_error .= "Date of birth month or day is bigger than expected!";
                            }
                            else
                            {
                                //check for gender
                                if($gender != "Male" && $gender != "Female")
                                {
                                    $reg_error .= "Gender must be Male or Female!";
                                }
                                else
                                {
                                    //check for existing user
                                    $query = mysql_query("Select * FROM user WHERE username = '$username'");        
                                    if(mysql_num_rows($query) >= 1)
                                    {
                                        $reg_error .= "That username is already taken";
                                    }
                                    else
                                    {
                                        //success
                                        $dob_db = "$dob_year-$dob_month-$dob_day";
                                        $password_db = $password;
                                        switch($gender)
                                        {
                                            case "Male";
                                                $gender_db = "M";
                                            break;
                                                case"Female";
                                            $gender_db = "F";
                                            break;
                                        }
                                        $register = mysql_query("INSERT INTO user VALUES('','$firstname','$lastname','$username','$password','$dob','$gender')");
                                        //header('Location: index.php');
                                        echo"WORKS";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
        $_SESSION['reg_error'] = $reg_error;      //for errors
        header('Location: register.php');         //go back to register page    
}   
else
{
echo "dead";
}
?>



<html>
<title>Register</title>
<head>
<link rel = "stylesheet" href = "css.css">
</head>
<body>
                         <!-- go to top php -->
    <form action="" method="post">
        <?php
        //print errors
        if(array_key_exists('reg_error', $_SESSION) && !empty($_SESSION['reg_error']))
        {
            echo $_SESSION['reg_error'];
            unset($_SESSION['reg_error']);
        }
        ?>
    Firstname: <input class = "text" type="text" name="firstname"><br>
    Lastname: <input class = "text" type="text" name="lastname"><br>
    Username: <input class = "text" type="text" name="username"><br>
    <!-- HINT -->
    <p class="hint">20 characters maximum</p>
    Password: <input type="password" name="password"><br>
    <!-- HINT -->
    <p class="hint">20 characters maximum (letters and numbers only)</p>                  
    Confirm your password: <input type="password" name="confirm_password" size="30"><br>
    <!-- HINT -->
    <p class="hint">20 characters maximum (letters and numbers only)</p>
    Email: <input type="text" name="email"><br/>
    DOB:
        <input type='text' name='dob_year' maxlength='4' size='3' value='YYYY'> / 
        <input type='text' name='dob_month' maxlength='2' size='1' value='MM'> /
        <input type='text' name='dob_day' maxlength='2' size='1' value='DD'> <br/>
    Gender:
        <select name="gender">
             <option>Female</option>
            <option>Male</option>
         </select> 
    <input type="submit" name="submit" value="Register">
    </form>
</body>
</html>
Member Avatar for diafol

So this doesn't run?

echo "workinging";

<form action="" method="post">

Don't you still need to specify the current page address for the postback to work?
disclaimer: it's entirely possible I'm wrong...

Also, for debugging purposes you could add print_r($_POST) to the top of the page to see what the actual post data contains on submit.

workinging

Quoted Text Here

yes, echo "workinging"; is not working -_-.
for some reason it doesnt go in this if statment

> if(isset($_POST['submit']))

i really dont see any reason why it shouldnot work.

oh also i also tried. <form action = "register.php" method = "post"> but still no luck.
and this whole file is called register.php

Quoted Text Here

Also, for debugging purposes you could add print_r($_POST) to the top of the page to see what the actual post data contains on submit.

i tried print_r($_POST); and it outputed

Array( )

do you think thats the reason? it is not geting the sumbit from form.

yes, it would seem so.

I wonder if your call to session_start() or something in that include file is resetting/clearing the POST variable?

maybe try something like this and see if you get your post data

if (!isset($_SESSION)) {
  session_start();
}
if(isset($_POST['submit']))
{ ... }

... needs to be ...

if($_SERVER['REQUEST_METHOD'] == 'POST')
{ ... }

Also answered your other thread, same reason and solution.

With the looks of it, you would expect to see the $_POST['submit'] variable set in your example above, but perhaps the 'type="submit"' causes it to be unset or unregistered by the browser (it doesn't send it), but I'm not 100% sure on that...

nope it didnt worked,
but i change it to this

<?php 
...
$submit = $_POST["submit"];
if($submit)
{ ...
}

it gave me a error. line 10 is:

$submit = $_POST['submit']

Notice: Undefined index: submit in C:\xampp\htdocs\login_test\register.php on line 10

so i think my form submit button and php submit are not connect. may be.

Smeagel13,

i tried this but it still doesnt go inside if statment.

   <?php
    session_start();
    include("connect.php");


    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {...}
    else
    {}
    ?>

can you see if iam using server[] code right?

Do you get any output, the 'dead' statement, or none at all?

What's in include("connect.php"); ?

If you don't get any output at all, then perhaps something in there causes the script to finish.

i get "dead" statement.

and in my connect. php

   <?php
    $connect  =  mysql_connect('localhost',   'root',     '');
    if($connect != true)
    {
       die('Error - Problem connecting to database.' . mysql_error());  
    }
    $db_selected = mysql_select_db("test");
    if($db_selected != true)
    {
       die('Error - Problem selecting the database.' . mysql_error());  
    }
    ?>

i am stuck with this poblem for a week. and really dont see what iam missing.

Check the session_start() call like I said before as it may be resetting the POST variable.

Also, not sure if this may be causeing a problem or not, but all your html input tags are poorly formed having no close. ie:

<input type="submit" name="submit" value="Register">

should be

<input type="submit" name="submit" value="Register" />
                          // see tag close here ..  ^

In my experience a named submit button should be available in PHP via the POST variable, and should have the value assigned to the button (ie: its display text) in this case, it should be true that $_POST['submit'] == "Register"

It is possible that naming the button 'submit' may also be an issue, although I doubt it, but this is an easy thing to test.

Another thought... clear your browser cache and make sure you use ctrl+F5 to force a full refresh of the page (clear cookies as well to ensure you get a fresh session) as empty POST data can sometimes be caused by browser or proxy caching the POST request.

Quoted Text Here

Check the session_start() call like I said before as it may be resetting the POST variable.

tierd it but no differece

Also, not sure if this may be causeing a problem or not, but all your html input tags are poorly formed having no close. ie:

tierd but no differce.

ctrl+F5

no differece

$_POST['submit'] == "Register"

nope.. i just get "dead" statment

could the problem be bc my form is out side of php code?

hmm... seems very strange. I have run and tested your code above and I get all the POST data correctly, and your isset($_POST['submit']) check is successful.

I can only conclude that the problem is not in this code file. Can you post what is in the included connect.php file?

Is there any parent code (such as templates or CMS stuff) that might be affecting / intercepting the code?

actually... I may have just spotted it.

at line #114 above, on completion of processing a successful post you are redirecting the browser to the same page. This will clear the previously posted data and rerun the same script

header('Location: register.php');

It is also redundant as they will already be on the register page, and redirecting will override any messages you wish to display to the user. I suggest removing this line and try it again, you may find it has been working the whole time. ;)

...
update... just tested this theory on my server, and I get the "dead" path executed with no post data as you have described. I will almost guarantee that this redirect is your root cause

commented: good catch +14

OMG it finally works :) thanks alot hearth

no problem, glad I could help. :)

hi just have one question. it comes to this part when there are no errors. than i want to take all the info and put in a "user" table when i have created.

but problem is that it doesnt insert in sql table in database.
it does go to statment echo "work"; so the problem is in mysql_query(INSERT INTO user)

    else


          {
             //success
              $dob = "$dob_year-$dob_month-$dob_day";
               $password = $password;
               switch($gender)
                  {
                    case "Male";
                    $gender = "M";
                      break;
                      case"Female";
                       $gender = "F";
                        break;
                    }
           $register = mysql_query("INSERT INTO user VALUES('','$firstname','$lastname','$username','$password','$dob','$gender')");
                 //header('Location: index.php');
                echo"WORKS";
                }


my sql tablae name "user" has 8 things.
user_id firstname   lastname    username    password    email   dob gender

OK, firstly, the number of fields does not match up to your insert query. Looks like you are missing the email field out of the insert.

Sencondly, I assume user_id is an autoincrement key field. In order to make it work properly you need to insert NULL not and empty string.

"INSERT INTO user VALUES(NULL, '$firstname', '$lastname', '$username', '$password', '$email', '$dob', '$gender')"

You will need to make sure you grab the email from the post data, as it looks like you missed that (or I just can't see it)

It's also advisable to check the data for single quotes ' and other characters that may cause errors in the mysql and escape or remove them.

$firstname = str_replace("'","\'",$firstname);

etc.

also, your DOB variable is $dob_db, so check that in your insert statement.

works! yeahhhhhhhhh

Member Avatar for diafol

Just a note here - print_r($_POST) will ALWAYS give Array() - empty or otherwise as $_POSTis always set, regardless of whether a form has been sent or not.

Apologies if you were already aware of this. :)

I missed the header() - nice catch hearth!

Yeah, the post variable will always be set, but the print_r call will show you if it contains any values and what they are, or if it is empty.

Cheers :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.