Jump to content

University exam exercise


Davecnll

Recommended Posts

Hi!! The day after tomorrow I've a HTML exam...

Would you mind help me with this exercise?? I couldn't find the way to do it looking to the tutorials.. but if there is, and I missed it, let me now!!

Create a HTML index.html containing a form:

  1. - with 2 text boxes to insert the name and the lastname;
    - a Submit button which sends the datas inserted by the user to a script php named namecontrol.php. 
  2. Create a PHP page which has to visualize the datas sent from the form, as a list.
Link to comment
Share on other sites

Yeah I think so, it has to be a HTML list.. I'm ok with the first HTML form part, but I've no idea how to do the second one with php.

Should be easy because, at university, we did only a basic part of php.

Edited by Davecnll
Link to comment
Share on other sites

If it is just a singe first name, last name, just retrieve using $_GET or $_POST depending on whatever the method used in form, predefine html list, and echo out the result to that list.

IF more than one user, you would have to store users, add new user, the loop through these stored names producing individual list item elements with these first names and last names.

Link to comment
Share on other sites

So this is my answer for your problem:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Name Control</title>
</head>
<body>
<form method="post" action="namecontrol.php" >
    <input type="text" name="name" placeholder="Insert your name ..." /><br/>
    <input type="text" name="lastname" placeholder="Insert your last name ..." /><br/>
    <input type="submit" value="Submit" name="submit" /><br/>
</form>
</body>
</html>

namecontrol.php (HTML LIST)

<?php

if($_SERVER['REQUEST_METHOD'] == "POST")
{
    if(isset($_POST['submit']))
    {
        $name = $_POST['name'];
        $lastName = $_POST['lastname'];

        if(empty($name) || empty($lastName))
        {
            echo "Please complete all fields to proceed !";
        }
        else
        {
            echo "<ul>";
            echo "<li>name: " . $name . " </li>";
            echo "<li>lastname: " . $lastName . "</li>";
            echo "</ul>";
        }
    }
}

?>

namecontrol.php (ARRAY)

<?php

if($_SERVER['REQUEST_METHOD'] == "POST")
{
    if(isset($_POST['submit']))
    {
        $name = $_POST['name'];
        $lastName = $_POST['lastname'];

        if(empty($name) || empty($lastName))
        {
            echo "Please complete all fields to proceed !";
        }
        else
        {
            $array = array("name" => $name, "lastname" => $lastName);
            print_r($array);
        }
    }
}

?>

 

Link to comment
Share on other sites

Seems wrong to ask for name then last name, name should represent full name, then you are asking for last name and repeating what you would have already provided.

Should Be First name/s or forename/s  and then last name in my opinion.

Edited by dsonesuk
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...