0

I have a project to do that is entirely in PHP. All the code must be in a single file. I must create a form that accepts the user's name or names, the days of the week checkbox, and an activity as a dropdown list. There is a submit button at the bottom that, when clicked, will take all of the data that the user provided, check it, and then send it to a new form. If the data is incorrect, I'll explain what to look for later, the user will see a message in the first form. All the fields are sticky. I will start with the fact that the user should put some data:

print_text_input, print_text_input, print_activity, printValues(), are functions that take the user's data and do some stuff with it. I will explain them later.

print("<form action='' method='post'>\n");
print_text_input('text', 'name', 'Όνοματεπώνυμο');
print_day('checkbox', 'days', 'Ημέρα της εβδομάδας');
print_activity('select', 'activity', 'Δραστηριότητα');
print("<button onclick='printValues()'>Υποβολή</button>\n");
print("</form>\n");

Here is the first function that takes the name, last name, middle name, etc. and just checks if the pattern letter, space, or letter occurs. I used a regular expression to check that. Also, the user must not put a space before the first word or after the last. Also, this must be sticky.

function print_text_input($name) 
{
    if (isset($_POST['name']) && !empty($_POST['name'])) 
    {
        $_SESSION[$name] = $_POST['name'];
    }

    print("<p>Ονοματεπώνυμο<input type='text' name='name'");

    if (isset($_SESSION[$name])) {
        print(" value='" . $_SESSION[$name] . "'");
    }

    print(">");

    if(isset($_POST['name']) && !empty($_POST['name'])) 
    {
        if(preg_match('/^[a-zA-Z]+( [a-zA-Z]+)*$/', $_POST['name'])) 
        {
            $correct1 = true;
            return $name;
        } 
        else 
        {
            print("<p>Please enter a valid name with the pattern 'letter, space, letter' and no leading or trailing spaces or double spaces.</p>");
        }
    }
    print("</p>\n");
}

Later there is this function, which requires that the user choose at least two days, with one of them being Saturday or Sunday. Also, this is a sticky checkbox.

function print_day($days)
{
    $strings = array();

    print("<p><b>Ημέρες</b></p>");
    $days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

  foreach($days as $day) 
  {
    if(isset($_POST['days']) && in_array($day, $_POST['days'])) 
    {
      $checked = "checked";
    } 
    else 
    {
      $checked = "";
    }

    print("<input type='checkbox' name='days[]' value='$day' $checked>" . $day . "<br>");
    }

    if(isset($_POST['days']) && count($_POST['days']) >= 2 && (in_array('Saturday', $_POST['days']) || in_array('Sunday', $_POST['days'])))
    {
        $strings[] = $day;
    } 
    else 
    {
        print("<p>Please select at least two days, including Saturday or Sunday.</p>");
    }
    return $strings;


    print("</p>\n");
}

Here's a function that takes an array of activities (in Greek), displays them in a drop-down list, and the user simply selects one of them. Also, this is sticky.

function print_activity($activity)
{
    $activity = array('Τρέξιμο', 'Κολύμπι', 'Ποδηλασία', 'Ορειβασία', 'Σκι');

    if (isset($_POST['activity']) && !empty($_POST['activity'])) 
    {
        $_SESSION['activity'] = $_POST['activity'];
    }
    
    print("<p><label for='activity'>Παρακαλώ επιλέξτε μία δραστηριότητα από τις παρακάτω:</label></p>\n");
    
    print("<select name='activity' id='activity'>");
    foreach($activity as $option) 
    {
        print("<option value='$option'");
        if (isset($_SESSION['activity']) && $_SESSION['activity'] == $option) 
        {
            print(" selected");
        }
        print(">$option</option>");
    }
    print("</select>\n");
    
    if(isset($_POST['activity']) && !empty($_POST['activity'])) 
    {
        if(in_array($_POST['activity'], $activity)) 
        {
            return $activity;
        } 
        else 
        {
            print("<p>Please select a valid activity from the list.</p>");
        }
    }
}

At last, there is this method that is used when the user clicks the submit button. It should take the values of the functions above, if the values are correct after the checks in each function occur, and present them as a new form in the same window. What I am trying to say is that the original form with name, days, and activity should "disappear" when the user clicks the submit button and when all the data provided are correct, have a new form with only: Name: "name" , Day: "name of days", Activity: "activity".

function print_values() 
{
    print("<form method='POST'>\n");
    
    $name1 = print_text_input('name');
    $day1 = print_day('day');
    $activity1 = print_activity('$activity');
    
    if(isset($_POST['name1']) && isset($_POST['activity1']) && isset($_POST['day1']))
    {
        print("<label>Name: <input type='text' name='name1'></label>\n");
        foreach ($day as $day1) 
        {
            print("<li>$day</li>\n");
        }
        print("<label>Activity: <input type='text' name='activity1'></label>\n");
    }
    print("</form>\n");
}

-First, I need that the checkbox have a title of Activities inside the checkbox, but it cannot be chosen. That will be an enhansement, so if you are not in the mood to respond to that, that is fine.

-Secondly, the problem is that when I click the submit button, the functions check the inputs and make them sticky, but the submit, as in taking me to a new form, does not work.

Note: All of the code must be in a single file. The new form included.

Thank you in advance for bothering to look in my code.

YannisAm
  • 29
  • 4
  • Code would appear to be complete nonsense! You call these functions with multiple parameters but the function prototypes all have only one parameter, which in most cases you promptly overwrite inside the function! – RiggsFolly Dec 10 '22 at 14:46
  • 1
    Add [error reporting](http://stackoverflow.com/questions/845021/) to the top of your file(s) _while testing_ right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. ` – RiggsFolly Dec 10 '22 at 14:47

0 Answers0