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.

<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>

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.

$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?

[list][]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…
[/list]

Okay, so the corrected version of your HTML (again in HEREDOC syntax)

$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

    $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
    $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
    $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.

Ditto.

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?

Oops, my bad, slight typo.

$updatedPosts = <<<HTML

Update that line and you should be good to go.

Parse error: syntax error, unexpected T_SL in /home/sglivest/public_html/tournaments.php on line 32

Line 32: $updatedPosts = <<<HTML

Ah, a google search shows that you need to delete any whitespace after <<<HTML (meaning HTML) must be the last characters on that line.

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.

:slight_smile: I expected you may get that too, make sure there is NO whitespace in front of HTML; (the line with the semi-colon)

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”?

The updated file can be whatever extension you want it to be, but you will need to use

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

where you want the file to be inserted into your website.

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.

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.

Easiest way is to just update the HTML output so it uses a link and text.

    $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.

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.

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


<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


  </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.

To short the width of the table even more, replace the following


    <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


    <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>

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.

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!!