Formulario PHP (los valores desaparecen, no sé cómo se está procesando)

I'm a complete newbee in php (started just last week)

Issue is like this:

Basically, I was trying to ensure that once a sub-form is filled, then it is not altered. So, I used !isset to display the sub-form (i.e. if !isset is true) and if !isset is false, then it hides that sub-form and shows the next sub-form (the individuals form has only been designed).

        <?php include($_SERVER['DOCUMENT_ROOT'].'/officespace/includes/functions.php');

    echo'<html>
        <head>
            <title> Create </title>
        </head>

    <body>';

    if(!isset($_POST["Category"])){
    /* if no category is selected, then this code will display the form to select the category*/

    Echo "Pls Select Category before clicking on Submit Category";
    /* Breaking out of PHP here, to make the form sticky by using a php code inside form action*/
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <fieldset>
        <legend>Select the Category of Person: </legend><br />
            <input type="radio" name="Category" value="Individual" /> Individual<br /><br />
            <input type="radio" name="Category" value="Company, Pvt Ltd" /> Company, Pvt Ltd<br /><br />
            <input type="radio" name="Category" value="Company, Ltd" /> Company, Ltd<br /><br />
            <input type="radio" name="Category" value="Partnership Firm" /> Partnership Firm<br /><br />
            <input type="radio" name="Category" value="LLP Firm" /> LLP Firm<br /><br />
            <input type="radio" name="Category" value="HUF" /> HUF<br /><br />
            <input type="submit" name='Submit Category' value="Submit Category" /><br />
    </fieldset>
    </form>
    <?php
    } Else {

    $Category = $_POST["Category"];


        Echo "$Category";
        Echo "<br />";
    /* Using swich statement to test the value of Category, and accordingly echo appropriate forms*/
        switch ($Category) {
            case "Individual":
            if(!isset($_POST['Submit_Data_for_Individual'])){

            //if no data for individual is submitted,
            //then this code will display the form to enter and submit data for Individual

                Echo  /*displays message*/
                "Pls Enter the Data for the Individual";
    ?>
                <form action="<?php Echo $_SERVER['PHP_SELF']; ?>" method="post"> 
                <fieldset>
                <br />
                    First Namee: <input type="text" name="Individual_First_Name" />
                <br />
                    Middle Name: <input type="text" name="Individual_Middle_Name" />
                <br />
                    Last Name: <input type="text" name="Individual_Last_Name" />
                <br />
                    Date of Birth: <input type="text" name="date_of_birth/incorporation" />
                <br />
                    Gender: 
                <br />
                    <input type="radio" name="Gender" value="male" /> Male
                <br />
                    <input type="radio" name="Gender" value="female" /> Female
                <br />
                    Email 1: <input type="text" name="email_1" />
                <br />
                <input type="submit" name="Submit_Data_for_Individual" value="Submit Data for Individual" />
                </fieldset>
                </form>
    <?php                               
                }Else
                {
                $email_1 = $_POST["email_1"];
                $Gender = $_POST["Gender"];

                validate_email($email_1); // this is a custom function which i made
                Echo $Gender; // just to see if value has been passes. problem lies here because its not showing anything

                // run other validations here
                // and if all valid then run mysqli insert query for individuals record
                }

                break;
            case "Company, Pvt Ltd":
                echo "Company, Pvt Ltd";
                break;
            case "Company, Ltd":
                echo "Company, Ltd";
                break;
            case "Company, Ltd":
                echo "Company, Ltd";
                break;
            case "Partnership Firm":
                echo "Partnership Firm";
                break;
            case "LLP Firm":
                echo "LLP Firm";
                break;
            case "HUF":
                echo "HUF";
                break;
            case NULL:
                echo "Error: nothing selected";
                break;

        }
    }

    echo '</body>


    </html>';

    ?>

preguntado el 08 de noviembre de 11 a las 15:11

1 Respuestas

Is see one problem immediately.

You are checking for a form input called Submit Data for Individual, but that is the value of a submit button which has no name attribute. Set a name='submit-data' attribute on the submit button and change the conditional to check for the name instead of its value:

// This will never match.
if(!isset($_POST["Submit Data for Individual"])){

// Change it to
if(!isset($_POST["submit-data"])){

// Then change this
<input type="submit" value="Submit Data for Individual" />

// To this:
<input type="submit" name='submit-data' value="Submit Data for Individual" />

Additionally, the default case in a switch declaración usa un default palabra clave:

    // You may safely change this:
    case NULL:
        echo "Error: nothing selected";
        break;

    // To this:
    default:
        echo "Error: nothing selected";
        break;

Apéndice:

The following code is never reachable, since the form posts to another script, create.php. If you change the <form> action attribute to post back to <?php $_SERVER['PHP_SELF'];?> en lugar de create.php, deberías ver el else case. Right now, it doesn't work because your if pruebas que $_POST["submit-data"] is set. It can only be set if the form has been submitted, but the form submits to an external script.

        // This else case can never be reached...
        }Else
        {   
        validate_email($_POST["email_1"]); // this is a custom function which i made
        Echo $_POST["Gender"]; // just to see if value has been passes. problem lies here because its not showing anything

        // run other validations here
        // and if all valid then run mysqli insert query for individuals record
        }

To fix this and see your Gender echoed out, temporarily change

<form action="create.php" method="post"> 
// change to
<form action="' . $_SERVER['PHP_SELF'] . '" method="post"> 

Anexo 2

You are checking if Category is set, but after posting the user form, it will not be:

// Change
if(!isset($_POST["Category"])){

// To check that the user form was not submitted
if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {

Then you need to test if the user form was submitted. Before the Else { $Category = $_POST['Category']; section, add an else if to process the user form.

if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {
  // Show the Category form...
}
// Process the user form...
else if (isset($_POST['submit-data'])) {
    validate_email($_POST["email_1"]); // this is a custom function which i made
    Echo $_POST["Gender"];
}
// Now process the categories or show the user form...
else {
    $Category = $_POST['Category'];
    // etc...
}

Finally, remove the whole Else bloque de tu individual case, as it cannot be used there.

respondido 08 nov., 11:22

Thank you sir.. Im sorry, i have no experience in html (started with forms today only) - Chinmay Kamat

@ChinmayKamat We all start somewhere. - Michael Berkowski

@ChinmayKamat What isn't working? I ran the code with my suggested changes, and it seems to do what you ask of it - displays a list of radio buttons, and if Individual is selected, displays a different form for user info. Otherwise, displays back the selected radio option. - Michael Berkowski

Yes, it does that. But then after i Submit Data for Individual, there is a custom function and some echo thing (for testing), which it isnt doing. Its supposed to echo Gender and validate Email. - Chinmay Kamat

@ChinmayKamat See my addendum above. Your else case where Gender is to be displayed is unreachable since the form posts to create.php. - Michael Berkowski

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.