Jump to content

Trouble with code


gophres

Recommended Posts

I'm rather new to php, and i'm curious why this code isn't working......

I'm trying to create a guestbook

 

 

 

<html>

<body>

<?php

include("test2.txt");

?>

<form action="test.php" method="post">

Name: <input type="text" name="name" />

Comment: <input type="text" name="comment" />

<input type="submit" />

<br>

<?php

if($_POST["name"] != 0  && $_POST["comment"] != 0)

{

$file = fopen("test2.txt", "a+");

fwrite($file, "name: ");

fwrite($file, $_POST["name"]);

fwrite($file, "<br />");

fwrite($file, "Comment: ");

fwrite($file, $_POST["comment"]);

fwrite($file, "<br />");

fclose($file);

}

 

$_POST["name"]=o;

$_POST["comment"]=o;

 

?>

<br>

<br>

<br>

</form>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/67172-trouble-with-code/
Share on other sites

Hi!

 

I've made some corrections (marked with comments)

 

<html>
<body>
<?php
include("test2.txt");
?>
<form action="test.php" method="post">
Name: <input type="text" name="name" />
Comment: <input type="text" name="comment" />
<input type="submit" name="submit" value="Submit" />  <!-- Should give submits name and value -->


<?php
# Should check if string inputs are empty, not if equal to 0
if(!empty($_POST["name"]) && !empty($_POST["comment"]))
   {
   $file = fopen("test2.txt", "a+");
   fwrite($file, "name: ");
   fwrite($file, $_POST["name"]);
   fwrite($file, "
");
   fwrite($file, "Comment: ");
   fwrite($file, $_POST["comment"]);
   fwrite($file, "
");
   fclose($file);
   }

# It's never necessary to empty out $_POST.  It is reset on every script call.

?>

</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/67172-trouble-with-code/#findComment-336935
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.