Jump to content

Form Help


premracer

Recommended Posts

I recently made a form because i was starting a racing tourney for a online game i play. I made the form and it works, after the query is submitted the user sees the info that they have submitted, but when the user submits their entry or data whatever you call it, i would like for every person that submits a entry for it to create a flatfile with their info in it under a seperate directory something like user_forms so it can be easily accessable and easily readable.

 

the code goes as follows

 

tourneyapp.php

<html><title>Tourney App</title><center>
<body><form action="submitted_data.php" method="post">
<b>Racername:</b><br> <input type="text" name="racername" /><br>
<b>Average ET:</b><br> <input type="text" name="averageet" /><br>
<b>Car:</b><br>
                 <INPUT TYPE=CHECKBOX NAME="car" VALUE="Integra"/>Integra<br>
                 <INPUT TYPE=CHECKBOX NAME="car" VALUE="Lancer"/>Lancer<br>
                 <INPUT TYPE=CHECKBOX NAME="car" VALUE="RSX"/>RSX<br>
                 <INPUT TYPE=CHECKBOX NAME="car" VALUE="G35"/>G35<br>
                 <INPUT TYPE=CHECKBOX NAME="car" VALUE="Mustang"/>Mustang<br>
                 <INPUT TYPE=CHECKBOX NAME="car" VALUE="Vette"/>Vette<br>
                 <INPUT TYPE=CHECKBOX NAME="car" VALUE="Ford GT40"/>Ford GT40<br>
<b>Which entry amount would you like to pay?</b><br>
                 <INPUT TYPE=CHECKBOX NAME="entry" VALUE="$300"/>$300<br>
                 <INPUT TYPE=CHECKBOX NAME="entry" VALUE="$400"/>$400<br>
<input type="Submit" />
</form></body>
</html>

 

submitted_data.php

<html><title>Submitted Data</title>
<body><center>Hello <?php echo $_POST["racername"]; ?>.<br /> 
You have chose to use a <?php echo $_POST["car"]; ?> 
for this tourney with the average et of <?php echo $_POST["averageet"]; ?>.<br> 
With the agreement to pay the entry fee of <?php echo $_POST["entry"]; ?>.<br>
Your information has been submitted to our tourney system.<br>
Thanks and enjoy the tourney.</body>
</html> 

 

If anyone can help me please it would be greatly appreciated.

Link to comment
Share on other sites

the reason i was doing it like this is because it would be easier accessable, if i store it in a db then i have to log into cpanel, go to mysql, go to phpmyadmin, just makes things easier for me. then i could just password protect the directory where the user forms are sent to after a person submits a entry.

Link to comment
Share on other sites

like creating a flat file for everytime a user submits a entry in a remote folder

 

 

that would be a rediculous waste of resources. it would be much more beneficial to take the time and do it in a database. as a web developer, you should get used to the idea.

Link to comment
Share on other sites

yes but your not understanding me at all, it would be a whole hell of alot easier to just do it like that intead of having to log in through cpanel, mysql, phpmyadmn makes it alot easier to go to my pw protected directory and bam i can see everyone who registered. Now can someone please help me with what im trying to do and stop fighting with me and let me do something the way i want to do it wheter if you like it or not. It's very important i get this help, someone please help.

Link to comment
Share on other sites

yes but i dont want to print it i just want to be able to view it easily, if i can store it in a db ad be able to access it without logging into cpanel that would be great. but then if i create a admin panel would i need to create a whole user system?

Link to comment
Share on other sites

when i say 'print', i don't mean using a printer, i mean printing output to the browser.

 

you can store the data in a database and create a password protected webpage inwhich to view the data. this would be no more difficult to access than using a password protected file or directory. you don't need to create a whole user system, just a simple webpage where you view the contents of your database only after typing in a password of your choosing. this password is also stored in your database. alternatively, if you're running a linux server, you can password protect the page on the server-level using an apache mod.

Link to comment
Share on other sites

can you edit the code i posted and make it so it will do that, i would greatly appreciate it.

 

sure. first, let's take a look at what's wrong with your form. judging by your submitted_data.php page, you only want the user to be able to choose one car and one entry. if this is the case, you shouldn't use checkboxes, use a dropdown menu or radio buttons. in my example i used dropdown menus. you also don't have any function to sanitize user input. that's important. the way you did the checkbox form is wrong, too. here's how i'd do your form AND display:

 

<?php
        /* print form */
        function printForm(){
                echo "
                        <html>
                        <head><title>Input Data</title></head>
                        <body>
                                <form action=\"{$_SERVER['php_self']}\" method=\"post\">
                                <table>
                                        <tr>
                                                <td><b>Racer Name</b>:</td>
                                                <td><input type=\"text\" name=\"racer_name\" value=\"{$_POST['racer_name']}\"></td>
                                        </tr>
                                        <tr>
                                                <td><b>Average ET</b>:</td>
                                                <td><input type=\"text\" name=\"average_et\" value=\"{$_POST['average_et']}\"></td>
                                        </tr>
                                        <tr>
                                                <td><b>Car</b></td>
                                                <td>
                                                        <select name=\"car\">
                \n";

                $cars = array('Integra', 'Lancer', 'RSX', 'G35', 'Mustang', 'Vette');
                foreach($cars as $car){
                        echo "<option value=\"{$car}\"";
                        echo (($_POST['car'] == $car) ? (" SELECTED") : (""));
                        echo ">{$car}";
                }
                echo "
                                                        </select>
                                                </td>
                                        </tr>
                                        <tr>
                                                <td><b>Entry Amount:</b></td>
                                                <td>
                                                        <select name=\"entry\">
                \n";

                $entries = array(300, 400);
                foreach($entries as $entry){
                        echo "<option value=\"{$entry}\"";
                        echo (($_POST['entry'] == $entry) ? (" SELECTED") : (""));
                        echo ">${$entry}";
                }
                echo "  
                                                        </select>
                                                </td>
                                                <td colspan=\"2\"><input type=\"submit\" value=\"Submit\"></td>
                                        </tr>
                                </table>
                                </form>
                        </body>
                        </html>
                \n";
        }

        /* verify user-submitted data */
        if(isset($_POST)){
                $errors = array();
                foreach($_POST as $key => $val){
                        if(empty($val){ $errors[] = "You must enter in a value for {$key}"; }
                }

                if(empty($errors)){
                        /* insert user into database */
                        $sql = "
                                INSERT INTO
                                        user_forms
                                (
                                        racer_name,
                                        car,
                                        average_et,
                                        entry
                                )
                                VALUES(
                                        '{$_POST['racer_name']}'.
                                        '{$_POST['average_et']}',
                                        '{$_POST['car']}',
                                        '{$_POST['entry']}',
                                )
                        ";

                        mysql_query($sql) OR die(mysql_error());

                        /* print submitted data */
                        echo "
                                <html>
                                <head><title>User Submitted Data</title></head>
                                <body>
                                        Hello {$_POST['racer_name']}, you have chosen to use
                                        the {$_POST['car']} for this tournament with an average
                                        et of {$_POST['average_et']}. Along with a payment of
                                        ${$_POST['entry']} dollars. Your information has been
                                        submitted to our tourney system.
                                </body>
                                </html>
                        \n";
                }else{
                        foreach($errors as $error){ echo "<font color=\"ff0000\">{$error}</font><br />\n"; }
                        printForm();
        }else{ printForm(); }
?>

 

and this is how you access the contents of your database with password protection:

<?php
        session_start();
        if(isset($_POST)){
                $_SESSION['username'] = $_POST['username'];
                $_SESSION['password'] = $_POST['password'];
        }

        if($_SESSION['username'] == 'your_username' && $_SESSION['password'] == 'your_password'){
                $sql = "
                        SELECT
                                *
                        FROM
                                user_forms
                ";

                $query = mysql_query($sql);

                echo "
                        <table border=\"1\">
                                <tr>
                                        <th>Racer Name</th><th>Average ET</th><th>Car</th><th>Entry</th>
                                </tr>
                \n";

                while($row = mysql_fetch_array($query)){
                        echo "
                                <tr>
                                        <td>{$row['racer_name']}</td><td>{$row['average_et']}</td><td>{$row['car']}</td><td>${$row['entry']}</td>
                                </tr>
                        \n";
                }
        }else{
                echo "
                        <form action=\"{$_SERVER['php_self']}\" method=\"post\">
                        user name: <input type=\"text\" name=\"username\"><br />
                        password: <input type=\"text\" name=\"password\"><br />
                        <input type=\"submit\" value=\"Submit\">
                        </form>
                \n";
        }
?>

 

NOTE: this should be used as an illustration of one way you can achieve what you're looking for. this code is untested. try to learn from this example. don't try to copy and paste and expect it to work.

Link to comment
Share on other sites

ill post everything i have that i got done so far. and i want it to do as he posted, the pw protected page to view the printed data the improved form so no more than one option can be selected, with the drop down menus, and i need to know what to run for mysql queries. Please help. i need to have this done by thursday.

 

config.php

<?php
$host = "localhost"; //edit as required
$user = "tourney"; //edit as required
$pass = "tourney"; //edit as required
$db = "tourney"; //edit as required

$dbcon = mysql_connect("$host","$user","$pass"); 
mysql_select_db($db) or die(mysql_error());
?>

 

mysql query

CREATE TABLE `tourney` (
`id` int(11) NOT NULL auto_increment,
`racername` varchar(30) NOT NULL default '',
`averageet` varchar(255) NOT NULL default '',
`car` varchar(55) NOT NULL default 'Integra',
`entry` varchar(40) NOT NULL default '$300',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

can you edit that so when i run the query again it adds a username and password for the pw protected page, or do i just put my username and pw in the file itself?

 

 

Link to comment
Share on other sites

i need to know what to edit in this code which i will title userforms.php for example

 

userforms.php

<?php
        session_start();
        if(isset($_POST)){
                $_SESSION['username'] = $_POST['username'];
                $_SESSION['password'] = $_POST['password'];
        }

        if($_SESSION['username'] == 'your_username' && $_SESSION['password'] == 'your_password'){
                $sql = "
                        SELECT
                                *
                        FROM
                                user_forms
                ";

                $query = mysql_query($sql);

                echo "
                        <table border=\"1\">
                                <tr>
                                        <th>Racer Name</th><th>Average ET</th><th>Car</th><th>Entry</th>
                                </tr>
                \n";

                while($row = mysql_fetch_array($query)){
                        echo "
                                <tr>
                                        <td>{$row['racer_name']}</td><td>{$row['average_et']}</td><td>{$row['car']}</td><td>${$row['entry']}</td>
                                </tr>
                        \n";
                }
        }else{
                echo "
                        <form action=\"{$_SERVER['php_self']}\" method=\"post\">
                        user name: <input type=\"text\" name=\"username\"><br />
                        password: <input type=\"text\" name=\"password\"><br />
                        <input type=\"submit\" value=\"Submit\">
                        </form>
                \n";
        }
?>

 

how do i include my config.php with that so it correctly connects to my sql db? here is my config.php

 

config.php

<?php
$host = "localhost"; //edit as required
$user = "tourney"; //edit as required
$pass = "tourney"; //edit as required
$db = "tourney"; //edit as required

$dbcon = mysql_connect("$host","$user","$pass"); 
mysql_select_db($db) or die(mysql_error());
?>

Link to comment
Share on other sites

if($_SESSION['username'] == 'your_username' && $_SESSION['password'] == 'your_password'){
             include 'config.php';
   $sql = "
                        SELECT
                                *
                        FROM
                                user_forms
                ";

Link to comment
Share on other sites

ok its all working now but if you go to www.pimpinex.downthe.net/tourney/userforms.php and use the user example and pw example its posting everything besides entry amount. here is the code

 

userform.php

<?php
        session_start();
        if(isset($_POST)){
                $_SESSION['username'] = $_POST['username'];
                $_SESSION['password'] = $_POST['password'];
        }


        if($_SESSION['username'] == 'prem' && $_SESSION['password'] == '123456'){
include 'config.php';
                $sql = "
                        SELECT
                                *
                        FROM
                                tourney
                ";

                $query = mysql_query($sql);

                echo "
                        <table border=\"1\">
                                <tr>
                                        <th>Racer Name</th><th>Average ET</th><th>Car</th><th>Entry</th>
                                </tr>
                \n";

                while($row = mysql_fetch_array($query)){
                        echo "
                                <tr>
                                        <td>{$row['racername']}</td><td>{$row['averageet']}</td><td>{$row['car']}</td><td>${$row['entry']}</td>
                                </tr>
                        \n";
                }
        }else{
                echo "
                        <form action=\"{$_SERVER['php_self']}\" method=\"post\">
                        user name: <input type=\"text\" name=\"username\"><br />
                        password: <input type=\"text\" name=\"password\"><br />
                        <input type=\"submit\" value=\"Submit\">
                        </form>
                \n";
        }
?>

 

can anyone see anything wrong with this?

Link to comment
Share on other sites

There you go

 

<?php
session_start();
include 'config.php';

if(isset($_POST['Submit'])) 
{
                                $self = $_SERVER['php_self'];
                $_SESSION['username'] = $_POST['username'];
                $_SESSION['password'] = $_POST['password'];
}
if($_SESSION['username'] == 'prem' && $_SESSION['password'] == '123456')
{
$sql = "SELECT * FROM tourney";
$query = mysql_query($sql);
?>
<table width="539">
<tr>
<th width="148">Racer Name</th>
<th width="138">Average ET</th>
<th width="143">Car</th>
<th width="90">Entry</th>
</tr>
<?
while($row = mysql_fetch_array($query))
{
?>
<tr>";
<td><? $row['racername']; ?></td>
<td><? $row['averageet']; ?></td>
<td><? $row['car']; ?></td>
<td><? $row['entry']; ?></td>
</tr>
<?
}
?>
</table>
<?
}
else
{
?>
<form action="<? $_SERVER['php_self']; ?>" method="post">
<table width="31%" border="1">
  <tr>
    <td>user name:</td>
    <td><input type="text" name="username"></td>
  </tr>
  <tr>
    <td>password:</td>
    <td><input type="text" name="password"></td>
  </tr>
</table>
<input type="submit" value="Submit" name="Submit">
</form>
<?
}
?>

Link to comment
Share on other sites

The problem was here

 

<td>{$row['racername']}</td><td>{$row['averageet']}</td><td>{$row['car']}</td><td>${$row['entry']}</td>

 

yeah, i believe it needs a backslash in front of it.

<td>{$row['racername']}</td><td>{$row['averageet']}</td><td>{$row['car']}</td><td>\${$row['entry']}</td>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.