Need hep in creating smple form

Hi,
I am learning php, andi got a task, i tried but couldnt code this becuase i dont know how to get the date and the time as well as how can we do it without html.

Please help me to Create a form that contains:

  1. Name, textbox
  2. Submit button

If the user inputs the Name textbox and clicks the Submit button, the system must:

  1. Display "Good morning, " if the server time is within 12am-12 noon
  2. Display "Hello, " if the server time is not within 12am-12 noon
    is the value as inputted by the user into the textbox

Here’s you homework assignment ->
[php]<?php
date_default_timezone_set(‘America/New_York’);
if ( isset($_POST[‘submit’]) && $_POST[‘submit’] == “Submit” ) {
$time = date(‘G’); // 24 Hour format without leading zeros
if ( $time < 13 ) {
$message = "Good Morning " . $_POST[‘name’] . “!”;
} else {
$message = "Hello " . $_POST[‘name’] . “!”;
}
}
?>

Good Day

<?php echo ( isset($message) ) ? $message : "Welcome"; ?>

First Name:
[/php]

Homework Code refinement.

[php] if ( isset($_POST[‘submit’]) && $_POST[‘submit’] == “Submit” ) {[/php]

TO (KISS)

[php] if ($_POST) {[/php]

Also, the empty form action is not valid html5

"The action and formaction content attributes, if specified, must have a value that is a valid [b]non-empty[/b] [u]URL[/u] potentially surrounded by spaces."
  1. Yes, it’s simplier to do $_POST, but in my opinion might lead to bad programming practices in the future when a person might have multiple forms on a page. :wink:
  2. An empty form action is not valid in HTML5 is a joke in itself when comes to HTML5 in my opinion for HMTL5 allows sloppy practices in itself, besides simply put the name of the file in between the quotes. Or use the unsafe $_SERVER[‘PHP_SELF’] method (I’m joking ;D)
An empty form action is not valid in HTML5 is a joke in itself when comes to HTML5 in my opinion for HMTL5 allows sloppy practices in itself

I had nothing to do with creating the html5 standards. I didnt even so much as make a recommendation for it. :-X

Or use the unsafe $_SERVER['PHP_SELF'] method

So true on $_SERVER[‘PHP_SELF’]. Dont use it ever, ever, ever…but you may safely use:

[php]<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>"[/php]

Now I wouldn’t recommend turning the following in if indeed this in homework, but here’s how to make the code even tighter and keeping security in mind. LOL ;D
[php]<?php
date_default_timezone_set(‘America/New_York’);
$time = date(‘G’); // 24 Hour format without leading zeros
if ( $_POST ) $message = ( $time < 13 ) ? "Good Morning " . filter_input(INPUT_POST, ‘name’, FILTER_SANITIZE_SPECIAL_CHARS) . “!” : "Hello " . filter_input(INPUT_POST, ‘name’, FILTER_SANITIZE_SPECIAL_CHARS) . “!”;
?>

Good Day

<?php echo ( isset($message) ) ? $message : "Welcome"; ?>

First Name:
[/php]

Is there a reason why you are doing homework for someone? It’s one thing to assist, it’s another when you remove them from the equation and it isn’t like the date function is hard to find.

Thanks a lot for the response. It was just a single post but was a great learning. I was simply loking at the coding in a very simple way but i didnt know that we could do the same work in much refined way. It feels great to take advice from such learned peopl :slight_smile: :smiley:

I have tried my own code, but it does not work. I want to find out where is the fault.
If somebody can help me to find the fault, it will be great

<?php $name = $_POST['name']; $today = getdate(); if($today['hours']<=12) echo "Good Morning <".$name.">"; else echo "Hello ".$name; ?> Name:

This code is valid and works,

[php]<?php
$name = $_POST[‘name’];
$today = getdate();
if($today[‘hours’]<=12)
echo “Good Morning <”.$name.">";
else
echo "Hello ".$name;
?>[/php]

The question would be, is it in show.php, or is it somewhere else?

I am sorry, let me be more specific. The issue is name doesn’t appear with " good morning " I am not sure why the name entered is not getting displayed with " good morning "

I am not sure if this is the issue, but remove the < and >. The browser may be thinking it is a new element on the page

IS there any possiblity… that the below line is causing issue ?

echo “Good Morning <”.$name.">";

The double quotes ? do i replace it with single quotes ? or is there any other ways ?

I ran this exact code,

[php]<?php
$name = $_POST[‘name’];
$today = getdate();
if($today[‘hours’]<=12)
echo “Good Morning <”.$name.">";
else
echo "Hello ".$name;
?>

Name: [/php]

And it worked as I would have expected it to, (‘undefined variable name’ before the form is submitted). Post the errors/ warnings that you are receiving.

Sponsor our Newsletter | Privacy Policy | Terms of Service