Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: submit form

  1. #1
    New Coder
    Join Date
    Aug 2011
    Thanked 0 Times in 0 Posts

    Question submit form

    Have a webpage for comments. Would like to have webpage refresh and comments appear on the same page after clicking submit.


    $name = htmlentities ($_POST['name']);
    $email = htmlentities ($_POST['email']);
    $website = htmlentities ($_POST['website']);
    $message = htmlentities($_POST['message']);
    $time = time();
    @ $fp = fopen('comments.txt','a');

    if (!$email)
    $email = '';
    if (!$website)
    echo'URL address not valid';
    if (!$fp)
    echo 'Please fill out all required fields';
    if(strlen($name) > 0 && strlen($message )> 0)
    if(filesize('comments.txt') > 0)
    $pre =',<br /><br />';
    $outputstring = $pre.'<h4>Hi, <a href="http://'.$website.'" target="_blank">'.$name.'</a>. <a href="mailto://'.$email.'" target="_blank">'.date('F j Y \a\t h:i a',$time).'</a></h4><br><h2>'.$message .'</h2>';
    @fwrite($fp, $outputstring, strlen($outputstring));

    <form action="comment.php" method="post">
    <tr><td><h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name:<input type="text" name="name"></td></br>
    <tr><td><h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email:<input type="text" name="email"></td></br>
    <tr><td><h2>&nbsp;&nbsp;&nbsp;Website:<input type="text" name="website"></td>
    <tr><td><h2>Comments:<br><textarea name="message" cols="40" rows="4" ></textarea></br>
    <center><input type="submit" value="Submit"></center>


    <?php include("comments.txt");?>

    Last edited by PHKatz; 07-15-2013 at 08:47 PM. Reason: forgot to add code

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    You'll need to remove the header() if you want to do that. As it is now, it will redirect to location. You don't need to do anything else since there is an include() call (which although is probably not what you want, it is sufficient since the text is plain/html). It needs some work to deal with isset()'s properly in order to prevent errors, but it would run otherwise.

    These branches confuse me. There seems to be no direct relationship to the if/elseif combinations for them. The flow seems chopped up unnecessarily.
    Personally I don't like the format of the stored data. Assuming that every record within contains the same format, it would be more efficient to simply store, line by line, $website, $name, $email, $time and $message, and parse when reading. Simple example of that using fgetcsv (assuming you csv delimit them):
    PHP Code:
    if ($fh fopen('comments.txt''r+'))
        while (
    false !== ($line fgetcsv($fh)))
    $website$name$email$time$message) = $line;
    printf('<h4>Hi, <a href="http://%s" target="_blank">%s</a>.  <a href="mailto://%s" target="_blank">%s</a></h4><br/><h2>%s</h2>' PHP_EOL
    date('F j Y \a\t h:i:s'$time),

    Simple as that. That saves tons in space and is a little less rigid.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts