Hello and welcome to our community! Is this your first visit?
Register
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
    Location
    FL, USA
    Posts
    20
    Thanks
    2
    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.

    index.php
    <html>
    <body>

    <?php
    $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 = '';
    }
    else
    if (!$website)
    {
    echo'URL address not valid';
    }
    else
    if (!$fp)
    {
    echo 'Please fill out all required fields';
    }
    else
    {
    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));
    fclose($fp);
    Header("Location:index.php");
    }
    ?>



    <center><table>
    <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>
    </form></table></center>


    <left><h2>Comments:</h2></left><br>

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



    </html>
    </body>
    Last edited by PHKatz; 07-15-2013 at 07:47 PM. Reason: forgot to add code

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 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)))
        {
            list(
    $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
                
    $website
                
    $name,
                
    $email,
                
    date('F j Y \a\t h:i:s'$time),
                
    $message);
        }
        
    fclose($fh);

    Simple as that. That saves tons in space and is a little less rigid.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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