SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Help with simple form/php code

    Hi, im looking to create a page in which users can submit a tournament they would like featured on our site. Basically all the person has to do is fill in the form, click submit, and it shows up aligned in a table in its designated spot. I also want all the older tournament submissions to be there too, but under the newest one.

    Heres an example of what I have and its not working properly. Also if it is possible can someone show me how to make some of the fields required? Please be ez on me, im still a pretty big noob with all of this.
    PHP Code:
    <html><table width="1000">
     
    <form id="form1" name="form1" method="post" action="">
     <tr>
     <td><input type="text" name="tournamentname" placeholder="Tournament Name" size="13" maxlength="20"></td>
     <td><input type="text" name="start" placeholder="01/01/0101" size="13" maxlength="10"></td>
     <td><input type="text" name="location" placeholder="SC2: Sentinel Gaming" size="13" maxlength="25"></td>
     <td><input type="text" name="prize" placeholder="$10" size="13" maxlength="10"></td>
      <td><input type="url" name="brackets" placeholder="URL" size="13" ></td>
       <td><input type="url" name="signup" placeholder="URL" size="13" ></td>
        <td><input type="url" name="stream" placeholder="URL" size="13" ></td>
         <td><input type="url" name="forum" placeholder="URL" size="13" ></td>
         </tr>
          <p>
     <label>
     <input type="submit" name="submit" id="submit" value="Submit Tournament" />
     </label>
     </form>
     <tr>
    <td width=13><font color="white"><b><u>Tournament</b></u></td></font>
    <td width=13><font color="white"><b><u>Start</b></u></td></font>
    <td width=13><font color="white"><b><u>Location</b></u></td></font>
    <td width=13><font color="white"><b><u>Prize</b></u></td></font>
    <td width=13><font color="white"><b><u>Brackets</b></u></td></font>
    <td width=13><font color="white"><b><u>Signup</b></u></td></font>
    <td width=13><font color="white"><b><u>Stream</b></u></td></font>
    <td width=13><font color="white"><b><u>Forum</b></u></td></font>
    </tr>
    </html>
         
     <?php
        $tourneyname 
    $_POST["tournamentname"];
        
    $start $_POST["start"];
        
    $location $_POST["location"];
        
    $prize $_POST["prize"];
        
    $brackets $_POST["brackets"];
        
    $signup $_POST["signup"];
        
    $stream $_POST["stream"];
        
    $forum $_POST["forum"];
        
    $posts file_get_contents("posts.txt");
       
        
    $posts "<tr><td><font color=white>$tourneyname</td></font><td><font color=white>$start</td></font><td><font color=white>$location</td></font><td><font color=white>$prize</td></font><a href=><td><font color=white>$brackets</td></font><td><font color=white>$signup</td></font><td><font color=white>$stream</td></font><td><font color=white>$forum</td></font></tr></a>" "<tr><td>$posts</tr></td></table>";
        
        
    file_put_contents("posts.txt"$posts);
        
     
    ?>
     
    </body>

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    First, before we tackle anything else, let's fix that HTML output you are doing. For this, I'll write it using the HEREDOC syntax, as I think it will make it easier to see the mistakes, then I'll show you the corrected version.
    PHP Code:
    $posts <<< HTML
      <tr>
        <td>
          <font color=white>
    $tourneyname</td>
        </font>
        <td>
          <font color=white>
    $start</td>
        </font>
        <td>
          <font color=white>
    $location</td>
        </font>
        <td>
          <font color=white>
    $prize</td>
        </font>
        <a href=>
          <td>
            <font color=white>
    $brackets</td>
          </font>
          <td>
            <font color=white>
    $signup</td>
          </font>
          <td>
            <font color=white>
    $stream</td>
          </font>
          <td>
            <font color=white>
    $forum</td>
           </font>
         </tr>
       </a>
       <tr>
         <td>
    $posts</tr>
       </td>
    </table>
    HTML; 
    Do you see the problems?
    • Your closing </font> tags are in the wrong spot, they need to be inside the <td></td> tags.
    • You really should have quotes around your "white" color value. HEREDOC syntax will help you with that
    • You are opening a link around a <td>, that has no URL... and then it closes well after its usefulness
    • The very end of the HTML is just strange to say the least, plus I'm not sure why you are closing the table, when you never opened one...


    Okay, so the corrected version of your HTML (again in HEREDOC syntax)
    PHP Code:
    $updatedPosts <<< HTML
      <tr>
        <td>
          <font color="white">
    $tourneyname</font>
        </td>
        <td>
          <font color="white">
    $start</font>
        </td>
        <td>
          <font color="white">
    $location</font>
        </td>
        <td>
          <font color="white">
    $prize</font>
        </td>
        <td>
          <font color="white">
    $brackets</font>
        </td>
        <td>
          <font color="white">
    $signup</font>
        </td>
        <td>
          <font color="white">
    $stream</font>
        </td>
        <td>
          <font color="white">
    $forum</font>
        </td>
      </tr>
      
    $posts
    HTML; 
    Okay, now that the HTML is cleaned up, let's analyze why your file isn't containing both tournament entries. So let's take a look at your old code
    PHP Code:
        $posts file_get_contents("posts.txt"); 
        
        
    $posts "<tr><td><font color=white>$tourneyname</td></font><td><font color=white>$start</td></font><td><font color=white>$location</td></font><td><font color=white>$prize</td></font><a href=><td><font color=white>$brackets</td></font><td><font color=white>$signup</td></font><td><font color=white>$stream</td></font><td><font color=white>$forum</td></font></tr></a>" "<tr><td>$posts</tr></td></table>"
         
        
    file_put_contents("posts.txt"$posts); 
    Actually, I'm not 100% certain why it wouldn't be storing both values. My guess is it might be, but due to the invalid HTML, you are not seeing both records. However, the following code should work
    PHP Code:
    <?php 
        $tourneyname 
    $_POST["tournamentname"]; 
        
    $start $_POST["start"]; 
        
    $location $_POST["location"]; 
        
    $prize $_POST["prize"]; 
        
    $brackets $_POST["brackets"]; 
        
    $signup $_POST["signup"]; 
        
    $stream $_POST["stream"]; 
        
    $forum $_POST["forum"]; 
        
    $posts file_get_contents("posts.txt"); 
        
        
    $updatedPosts <<< HTML
      <tr>
        <td>
          <font color="white">
    $tourneyname</font>
        </td>
        <td>
          <font color="white">
    $start</font>
        </td>
        <td>
          <font color="white">
    $location</font>
        </td>
        <td>
          <font color="white">
    $prize</font>
        </td>
        <td>
          <font color="white">
    $brackets</font>
        </td>
        <td>
          <font color="white">
    $signup</font>
        </td>
        <td>
          <font color="white">
    $stream</font>
        </td>
        <td>
          <font color="white">
    $forum</font>
        </td>
      </tr>
      
    $posts
    HTML;
         
        
    file_put_contents("posts.txt"$updatedPosts); 
         
     
    ?>
    Okay, before I give you the A-OK on this, I do want to point out 1 more thing. The use of file_get_contents() and file_put_contents is overkill in this scenario. As you will be constantly reading the entire file and writing the entire data back to file.

    When the file gets REALLY REALLY large, you will have problems.

    You can resolve this by using fopen, fwrite, and fclose as shown below:
    PHP Code:
    <?php 
        $tourneyname 
    $_POST["tournamentname"]; 
        
    $start $_POST["start"]; 
        
    $location $_POST["location"]; 
        
    $prize $_POST["prize"]; 
        
    $brackets $_POST["brackets"]; 
        
    $signup $_POST["signup"]; 
        
    $stream $_POST["stream"]; 
        
    $forum $_POST["forum"]; 
        
        
    $updatedPosts <<< HTML
      <tr>
        <td>
          <font color="white">
    $tourneyname</font>
        </td>
        <td>
          <font color="white">
    $start</font>
        </td>
        <td>
          <font color="white">
    $location</font>
        </td>
        <td>
          <font color="white">
    $prize</font>
        </td>
        <td>
          <font color="white">
    $brackets</font>
        </td>
        <td>
          <font color="white">
    $signup</font>
        </td>
        <td>
          <font color="white">
    $stream</font>
        </td>
        <td>
          <font color="white">
    $forum</font>
        </td>
      </tr>
    HTML;
        
      
    $fileHandler fopen("posts.txt""c");
      
    fwrite($fileHandler$updatedPosts);
      
    fclose($fileHandler);
     
    ?>
    Okay, first thing I did was remove the file_get_contents() line
    Next I removed the $posts variable from the HTML code (it was previously before the HTML; line)
    Finally I replaced the file_put_contents() with the fopen, fwrite, and fclose code.
    The "c" states to open the file for writing, if the file doesn't exist, create it, if it does exist, put the cursor on the first line (so anything you write, will be written above the existing content).

    I'll have to get back to you on the validation bit, but if you start with these changes first, we can make our way to that process.

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Atlanta, GA
    Posts
    244
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Ditto.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I can't seem to get it to not say "unexpected < on line 33"

    Line 33: $updatedPosts <<< HTML

    Also how does it know what $updatedposts is when we never defined it?

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Oops, my bad, slight typo.

    PHP Code:
    $updatedPosts = <<<HTML 
    Update that line and you should be good to go.

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Parse error: syntax error, unexpected T_SL in /home/sglivest/public_html/tournaments.php on line 32

    Line 32: $updatedPosts = <<<HTML

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Ah, a google search shows that you need to delete any whitespace after <<<HTML (meaning HTML) must be the last characters on that line.

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh ok, got that fixed. Now its saying that there is an unexpected $end on the last line... Doesent really make much sense since we closed everything off.

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    I expected you may get that too, make sure there is NO whitespace in front of HTML; (the line with the semi-colon)

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Its always the small things... But thank you lol.

    Now the form is showing up with no errors but nothing is being posted. Nothing shows up after clicking submit. I can see data within the posts.txt though. Its just not being projected on the screen. Is the updatedposts file supposed to be ".txt"?

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    The updated file can be whatever extension you want it to be, but you will need to use
    PHP Code:
    <?php include("posts.txt"); ?>
    where you want the file to be inserted into your website.

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Okay, It works!

    I have a couple of questions now though, is there any way to make the cells locked width? When I submit a URL in the last 4 boxes and press submit it extends the width of the cell by itself. I have been trying to find a way to make the width fixed but nothing seems to work.

    And also, how hard would it be to make some parts of the form required? If thats too much of a hassle thats fine.

    Thanks

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sgsecret View Post
    Okay, It works!

    I have a couple of questions now though, is there any way to make the cells locked width? When I submit a URL in the last 4 boxes and press submit it extends the width of the cell by itself. I have been trying to find a way to make the width fixed but nothing seems to work.

    And also, how hard would it be to make some parts of the form required? If thats too much of a hassle thats fine.

    Thanks
    Can you provide a link to the page? I really need to see what you are referring to when you say "locked width".

    As for the validation, no, that isn't "too hard", but it does take some work, also, we need to do some data validation anyway, as there is a XSS vulnerability in this code that I just noticed. So I'll try to work on that this afternoon/evening so I can get you started in the right direction.

  14. #14
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    http://sglivestreams.host-ed.me/tournaments.php

    I need those 4 links on the end of each line to not extend out as far as they are because the page that I am putting this on is only 1000 px wide. I have <table width="1000"> at the top but it doesent seem to be changing anything. Those link are running off the page by about 30 pixels.

  15. #15
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Easiest way is to just update the HTML output so it uses a link and text.
    PHP Code:
        $updatedPosts <<< HTML 
      
    <tr
        <
    td
          <
    font color="white">$tourneyname</font
        </
    td
        <
    td
          <
    font color="white">$start</font
        </
    td
        <
    td
          <
    font color="white">$location</font
        </
    td
        <
    td
          <
    font color="white">$prize</font
        </
    td
        <
    td
          <
    font color="white"><a href="$brackets">Brackets</a></font
        </
    td
        <
    td
          <
    font color="white"><a href="$signup">Sign Up</a></font
        </
    td
        <
    td
          <
    font color="white"><a href="$stream">Stream</a></font
        </
    td
        <
    td
          <
    font color="white"><a href="$forum">Forum</a></font
        </
    td
      </
    tr
    HTML
    That will greatly reduce the length of those columns.

  16. #16
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yea I tried that but go to the page now and reload it. For some reason the page automatically puts those links over and over without you even submitting anything.

  17. #17
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Okay, your HTML output is all messed up. You have closing form tags inside a table, you have an opening form tag inside the table, etc etc. So we need to get that straightened out which may help resolve the problem.

    So here is the HTML that should be ABOVE your <?php include("posts.txt"); ?> line
    HTML Code:
    <form id="form1" name="form1" method="post" action="">
      <table>
        <tr>
            <td><input type="text" name="tournamentname" placeholder="Tournament Name" size="13" maxlength="20"></td>
            <td><input type="text" name="start" placeholder="01/01/0101" size="13" maxlength="10"></td>
            <td><input type="text" name="location" placeholder="SC2: Sentinel Gaming" size="13" maxlength="25"></td>
            <td><input type="text" name="prize" placeholder="$10" size="13" maxlength="10"></td>
            <td><input type="url" name="brackets" placeholder="URL" size="10" ></td>
            <td><input type="url" name="signup" placeholder="URL" size="10" ></td>
            <td><input type="url" name="stream" placeholder="URL" size="10" ></td>
            <td><input type="url" name="forum" placeholder="URL" size="10" > </td>
        </tr>
    
        <tr>
            <td colspan="8" align="right"><input type="submit" name="submit" id="submit" value="Submit Tournament" /></td>
        </tr>
    
        <tr>
            <td><font color="white" size="3"><b>Tournament Name</b></font></td>
            <td><font color="white" size="3"><b>Start</b></font></td>
            <td><font color="white" size="3"><b>Location</b></font></td>
            <td><font color="white" size="3"><b>Prize</b></font></td>
            <td><font color="white" size="3"><b>Brackets</b></font></td>
            <td><font color="white" size="3"><b>Signup</b></font></td>
            <td><font color="white" size="3"><b>Stream</b></font></td>
            <td><font color="white" size="3"><b>Forum</b></font></td>
        </tr>
    This is the HTML following your <?php include("posts.txt"); ?> line
    HTML Code:
      </table>
    </form>
    </body>
    </html>
    Also, remove the DIV tag from your $updatedPosts <<<HTML text.

    Then show me the results so I can look at it.

  18. #18
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    To short the width of the table even more, replace the following
    HTML Code:
        <tr>
            <td><input type="text" name="tournamentname" placeholder="Tournament Name" size="13" maxlength="20"></td>
            <td><input type="text" name="start" placeholder="01/01/0101" size="13" maxlength="10"></td>
            <td><input type="text" name="location" placeholder="SC2: Sentinel Gaming" size="13" maxlength="25"></td>
            <td><input type="text" name="prize" placeholder="$10" size="13" maxlength="10"></td>
            <td><input type="url" name="brackets" placeholder="URL" size="10" ></td>
            <td><input type="url" name="signup" placeholder="URL" size="10" ></td>
            <td><input type="url" name="stream" placeholder="URL" size="10" ></td>
            <td><input type="url" name="forum" placeholder="URL" size="10" > </td>
        </tr>
    
        <tr>
            <td colspan="8" align="right"><input type="submit" name="submit" id="submit" value="Submit Tournament" /></td>
        </tr>
    With
    HTML Code:
        <tr>
            <td><input type="text" name="tournamentname" placeholder="Tournament Name" size="13" maxlength="20"></td>
            <td><input type="text" name="start" placeholder="01/01/0101" size="13" maxlength="10"></td>
            <td><input type="text" name="location" placeholder="SC2: Sentinel Gaming" size="13" maxlength="25"></td>
            <td><input type="text" name="prize" placeholder="$10" size="13" maxlength="10"></td>
            <td><input type="url" name="brackets" placeholder="URL" size="10" ></td>
            <td><input type="url" name="signup" placeholder="URL" size="10" ></td>
            <td><input type="url" name="stream" placeholder="URL" size="10" ></td>
            <td><input type="url" name="forum" placeholder="URL" size="10" > </td>
        </tr>
    
        <tr>
            <td colspan="8" align="right"><input type="submit" name="submit" id="submit" value="Submit Tournament" /></td>
        </tr>
      </table>
      <table>

  19. #19
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Okay, there is a LOT more I could have done with this, but I don't quite have the time, so maybe others can help tweak what I've done up to this point. It now forces all fields to have data and it protects against XSS attacks.

    I put the code at PasteBin, because I can't get it to post here in the thread.

  20. #20
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    46
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Alright I think I can take it from here (even though I didnt really do anything) haha. I have learned a lot through both of these projects though.

    Can't thank you enough cpradio! I got a lot of projects for my website so im sure ill be back here soon enough lol.

    To view your code in action feel free to visit http://www.sentinelgaming.net/tournaments , it should be live sometime tonight.

    Thanks!!


Bookmarks

Posting Permissions

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