1

I want to get a value by the function with forms as the user input but I don't think I'm doing it right can anybody help me.

The code is like when enter number 1 in the form then it will output it by the days like Sunday.

Output:-

Enter days values between (0-6): 1
The day is Monday.

My Code:-

<?php
        function day_of_week($week){
           $week = $_POST['week'];
           $week = array(0 => Sunday, 1 => Monday, 2 => Tuesday, 3 => Wednesday, 4 => Thursday, 5 => Friday, 6 => Saturday);

            return $week;
        }

        if(isset($_POST['submit'])){
            $value = $_POST['value'];
            echo "The day is " . day_of_week($value);
        }
    ?>

    <form method="POST" action="">
        <label>Enter days value between (0-6):</label>
        <input type="text" name="value">
        <input type="submit">
    </form>

I search some code and some code the week can be assigned like

date('w'); 

if(date('w') == 1){
    echo "its monday baby";
}

or also in case code. But I'm not too sure how to actually call the value properly and assigning. Which one is better for assigning, array or date() or case for assigning?

Do help me out to explain why my value is not showing properly too.

Appreciate anyone's helps thank you so much.

1
  • Your function day_of_week($week)s wrong. Commented May 25, 2021 at 6:55

3 Answers 3

1

Change the function as

function day_of_week($x){
    $week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return $week[$x];
}

Don't forget to fill the action part in your form.

<form method="post" action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'>
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, it doesn't actually show the output why is that? I did write echo
Your form action is empty. <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
I still can't get the answer to show but what does <?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> do actually?
It means your form data will be PHP processed in the same file. Your submit button not properly designed (missing name to triggering the POST call*. Replace it with <input type="submit" name="submit" value="Submit">
Oh I learn something new thank you so that mean if i want something to be process in the same file i use <?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>.
1

You need to add a double quote to a string in key-value pair and then return the days with its position

<?php 
function day_of_week($day){
           $week = array(0 => "Sunday", 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday");

            return $week[$day];
        }

if(isset($_POST['submit'])){
            $value = $_POST['value'];
            echo "The day is " . day_of_week($value);
        }
?>
 <form method="POST" action="">
        <label>Enter days value between (0-6):</label>
        <input type="text" name="value">
        <input type="submit">
    </form>

2 Comments

Oh okay, but what the difference between double quotes and single quote because sometimes I come across a whole code that only uses a single quote and sometimes there like mixes with a single and double quote. Why is that?
Hi, it doesn't actually show the output why is that? I did write echo.
1

Generate days of week dynamically by for loop with built-in function date with format l which represents the day of the week.

$weekdays = [];
for($i=0;$i<7;$i++){
    $weekdays[$i] = date('l', strtotime("Sunday this week +$i days"));
}

So array $weekdays will contains days starting from index 0 which is Sunday through Saturday.

print_r($weekdays);

Prints:

Array
(
    [0] => Sunday
    [1] => Monday
    [2] => Tuesday
    [3] => Wednesday
    [4] => Thursday
    [5] => Friday
    [6] => Saturday
)

It would be better to use type="number" instead of text, And use some attributes such as start and max.

<input type="number" name="value" start='0' value="2" max="6">

3 Comments

Thank you for giving me some tips and the start and max is like telling it can't be over this number right?
No problem! Yeah that is what I meant, since it is number, and actually It can be forced to exceed the range of max 6, but you need to control it in the back-end with PHP.
Oh, okay I see really thank you I will take note of this. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.