Writing/creating a file

Hello everyone!

I am pretty new to programming but i have learned CSS and HTML(yes, i know, no biggie :P).
I am now learning PHP and i sometimes fall into situations where i do not know who to ask my questions to so i was looking for a community to join where i can seek help!

So this might be very easy for some of you but here i go with my first question!
I have been working on this small “program” where you can enter your name and click submit.
If no name was entered and the submit was hit, it should say “Please enter name”.

If a name was set, then it should write it down, after creating the new file for it, in the names.txt file.
Right now, it is not working and i have no idea why.
Here is the code:
[php]<?php
if(isset($_POST[‘name’])){
$name = $_POST[‘name’];
if(!empty($name)){
$handle = fopen(‘names.txt’, ‘a’);
fwrite($handle, $name."\n");
fclose($handle);
}else{
echo ‘Please type a name.’;
}
}

?>

Name:


[/php]

Also, test.php is the same file i am using.

Thanks in advance!

Well, first you should not check for the value of the name to start. You should first check to see if the user
posted the page or not. That is done using the submit button. Therefore, you should first see if they did
actually press the submit button like this: if (isse($_POST[‘submit’]) { …
( Of course, you need to add name=‘submit’ to the submit button so the post can find it… )

Next, if you know they pressed the submit button, then you grab the data in the name field and check for
it’s value. I think that should work for you…

Here is a nice tutorial on how to create and use forms with PHP. Just keep pressing the green next-chapter
button on the lower-right to walk thru various things to learn. I think it might help you. Also, this site has a
huge list of tutorials for just about anything programming.
http://www.w3schools.com/php/php_form_complete.asp

Hope this helps… Good luck and glad you could join us… Once you solve a puzzle, let us know so others
can help from your code…

Hey and thank you for the reply!

So check if submit was hit and then check if a value was entered in “name”, right?

I will rewrite it and see what happens :slight_smile:

Thanks

Yes! You are correct… But, let me explain some things to you that will make it easier to understand.

PHP is SERVER-SIDE only. HTML and CSS is BROWSER-SIDE only. What this means is that all the HTML is
show in your computer’s browser. You will NEVER see any PHP there unless it was posted as text in a
display area. PHP is handled on the server and then it’s OUTPUT is combined with the HTML and sent to
the browser.

To prove that, go to any site in the world. RIGHT-CLICK on a page and select VIEW-SOURCE and you will
see the code that makes up that page. BUT, you will never see the PHP code behind it.

Now, the HTML form you created contains fields and a submit button. Nothing happens until the user
presses the submit button. Once that is pressed, the entire list of fields are sent to the page inside the
form’s ACTION area. In this case test.php. There it handles any inputs and etc…

Hope that helps make more sense of it all… It’s quite easy once you get your first page running…

Thanks for the reply!

Alright well i have rewritten it but i now get 2 errors and i am not sure why:
[php]

Name:


<?php $name = $_POST['name']; $submit = $_POST['Submit']; if(isset($submit)){ if(isset($name)){ $handle = fopen('names.txt', 'a'); fwrite($handle, $name."\n"); fclose($handle); }else{ echo "Please enter a name"; } } ?>[/php]

I get the 2 following errors:

Notice: Undefined index: name in C:-----\XAMPP\htdocs/----\test.php on line 10

Notice: Undefined index: submit in C:--------\XAMPP\htdocs-----\test.php on line 11

If you haven’t submitted (POSTed) the form yet $_POST doesn’t contain ‘name’ and ‘Submit’. If you want to perform something after form submission you could

[php]if (!empty($_POST)) {
// something
}[/php]

or if you just want some default values

[php]$name = isset($_POST[‘name’]) ? $_POST[‘name’] : ‘’;[/php]

Here $name will be the posted form field “name” - if it is available, or an empty string if not.

Hmmm, well, a couple of things… First, you did not add a name for your submit button so the $_POST[]
for it will always be empty and not set… And, the error is caused because the input field for name is missing
an equal sign. name"name" should be name=“name”

Also, when writing a page of PHP code to use the form that is on the page, it is handled differently if you
have data you need to show on the page. PHP is done on the server even if it is on your same machine.
So it is basically something like this…

<?PHP // php code that handles posted variables and database access, etc. // This is where you would place all of your checking of inputs... // Nothing here is displayed on the page because the HTML page does not exist at this point ?> ...

So to save you some time as I am heading out soon, here is the layout using your code and with a few
new tricks on how to display your error messages… Remember, PHP ouput can be combined with the HTML.

file named: test.php
[php]

<?php $error_message = ""; if(isset($submit)){ if(isset($name)){ $handle = fopen('names.txt', 'a'); fwrite($handle, $name."\n"); fclose($handle); }else{ $error_message = "Please enter a name"; } } ?> Testing writing names to file... <?PHP echo $error_message; ?> Name:


[/php] I wrote this off the top of my head, but, should work for you. Hope this helps...

Thank you for the replies!

Alright i got everything working now thanks to you guys.
I just have 1 more problem:
When the names are entered, they aren’t being placed on a line different line each time.
I am using this exact line:
[php]fwrite($handle, $name."\n");[/php]

Thanks

Alright i got it all to work just the way it is suppose to!
Here is the final code(if anyone cares):
[php]<?php
$error_message = “”;
if(isset($_POST[‘submit’])){
if(!empty($_POST[‘name’])){
$name = $_POST[‘name’];
$handle = fopen(‘names.txt’, ‘a’);
fwrite($handle, $name."\n");
fclose($handle);
}else{
$error_message = “Please enter your name”;
}
}

?>

<?php echo $error_message; ?> Name:


[/php]

Thank you for the help and see you on the next question!

VERY GOOD! Congrats, zBrain ! ! ! Isn’t is a nice warm feeling to solve a programming puzzle?!?

We will call this one closed… When you find the next puzzle, we will be here to help !

Just a heads up (if anyone cared :stuck_out_tongue: )
I have re-written the program to include 2 pages now.
One to process take the order, coded in HTML and a php file where the order will go to.
I actually had no problem coding this one!

Here is the basic HTML order form:

[code]
























<?php echo $error_message; ?>

Item Quantity
Tires
Oil
Spark Plugs

[/code]

and here is the php file:
[php]

Garage Parts Order Form


Zig’s Auto Parts


Order Results:


<?php
		$error_message = "";
		if(isset($_POST['submit'])){
			if(!empty($_POST['tireqty'] && !empty($_POST['oilqty']) && !empty($_POST['sparkqty']))){
				$tireqty = $_POST['tireqty'];
				$oilqty = $_POST['oilqty'];
				$sparkqty = $_POST['sparkqty'];
				$date = date('H:i, jS F Y');

				$output = $date. "\t".$tireqty." tires \t".$oilqty." oil \t".$sparkqty." spark plugs \n";

				$handle = fopen('oders.txt', 'a');
				fwrite($handle, $output);
				fclose($handle);

			}else{
				exit("No good");
			}
		}
			
		?>
</body>
[/php]
I actually had no problem coding this one!

Good for you! Yes, we all do care! ;D

Now, you can move onto learning how to do it with database code… LOL Always something to learn in
the programming world…

Sponsor our Newsletter | Privacy Policy | Terms of Service