Search code examples
phphtmldatabasesubmit

How to show the result when I click the submit button? (Like View history)


Here's the concept. The users will input the data from the text bar.. And when click the submit button, the data will be saved to the "VIEW HISTORY" menu bar. View history is where the data's inputted from the home.php are being made.

E.g:

Home.php

(data1)
Enter Name: (I type..)Black Cat
Age: (I type..)21
Job: (I type..)Spy

(data2)
Enter Name: (I type..)Black Dog
Age: (I type..)24
Job: (I type..)Cook

Submit Button (I click it) and it says the data is submitted to my database. Then when I click the "View History Menu Bar".. The ff will be shown:

DATA1
DATA2
<Click next to view next page.. etc>

When I click data1, the whole name, age and job from data1 will appear. And so is from data2.. and so on.. :)

That's my only problem.. I can't show those data :( Please help! I have some codes in here.. But I dunno how to fix it :( I hope you can give some samples so I can imitate the moves, or better if we help on fixing this. :( THANKS Y'ALL!

<?php
$cfccon = include("E:/xampp/conn1/cfmscsd.php");
//This is about the localhost, username, password stuff

$query="SELECT * FROM contents";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$B1=mysql_result($result,$i,"B1");
$B2=mysql_result($result,$i,"B2");
$B3=mysql_result($result,$i,"B3");
$B4=mysql_result($result,$i,"B4");
$B5=mysql_result($result,$i,"B5");
$B6=mysql_result($result,$i,"B6");
$B7=mysql_result($result,$i,"B7");
$B8=mysql_result($result,$i,"B8");
$B9=mysql_result($result,$i,"B9");
$B10=mysql_result($result,$i,"B10");

$i++;
}
?>


<html>
<tr>
                <td bgcolor="#bfb9a7"><span class="style77">CCC</span></td>
                <td bgcolor="#CCCCCC"><div  align="center" class="style66"><input name="B1" type="text" size="18" id="B1" value="<?php echo $B1;?>" disabled="disabled"/></div></td>
                <td bgcolor="#CCCCCC"><div  align="center" class="style66"><input name="B2" type="text" size="18" id="B2" value="<?php echo $B2;?>" disabled="disabled"/></div></td>
                <td bgcolor="#CCCCCC"><div  align="center" class="style66"><input name="B3" type="text" size="18" id="B3" value="<?php echo $B3;?>" disabled="disabled"/></div></td>
                <td bgcolor="#CCCCCC"><center><div class="style66"><input type="button" onclick="tbsp1()" value="<?php echo $tbsp1;?>add" disabled="disabled"/></center></td>
                <td bgcolor="#FEEDD8"><div  align="center" class="style66"><input name="B4" type="text" size="18" id="B4" value="<?php echo $B4;?>" disabled="disabled"/></div></td>
              </tr>
              <tr>
                <td><span class="style77">SCC</span></td>
                <td><div align="center" class="style66"><input name="B5" type="text" size="18" id="B5" value="<?php echo $B5;?>" disabled="disabled"/></div></td>
                <td><div align="center" class="style66"><input name="B6" type="text" size="18" id="B6" value="<?php echo $B6;?>" disabled="disabled"/></div></td>
                <td><div align="center" class="style66"><input name="B7" type="text" size="18" id="B7" value="<?php echo $B7;?>" disabled="disabled"/></div></td>
                <td><center><input type="button" onclick="tbsp2()" value="<?php echo $tbsp2;?>add" disabled="disabled"/></center></td>
                <td bgcolor="#FEEDD8"><div align="center"><input name="B8" type="text" size="18" id="B8" value="<?php echo $B8;?>" disabled="disabled"/></div></td>
              </tr>
              <tr>
                <td bgcolor="#bfb9a7"><span class="style77">NCC</span></td>
                <td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B9" type="text" size="18" id="B9" value="<?php echo $B9;?>" disabled="disabled"/></div></td>
                <td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B10" type="text" size="18" id="B10" value="<?php echo $B10;?>" disabled="disabled"/></div></td>
             </tr>
</html>

Solution

  • First of all, I recommend that you rename your table columns, B1, B2, B3 etc are very very bad names, instead use something descriptive like name, age, email, etc. That will make your code a lot easier to read and debug.

    To achieve what you're asking for, do something like this:

    1) When the user clicks the submit button, do a form post to a script on your site, e.g

    <form method=post action=home.php>
    Enter data: <input type=text name=data>
    <input type=submit value=Submit />
    </form>
    

    2) When the user clicks the submit button, you could get the data he posted in $_POST, e.g $_POST['data']. Save this to a mysql database. You should give the user a unique user ID. Each row in your table should have these fields:

    dataId (int,primary, auto_increment)
    userID (int)
    name (varchar 255)
    age  (int)
    etc...
    

    Then you should have a different table for users, called a user table. Each user who visits the website should have a unique userId. You can store this userId in $_SESSION, I recommend you read about session handling.

    Then whenever the user submits his form, you add a new row to your data table with his info, and the userId field with the given user's id.

    3) When the user clicks 'next' to see the data, you would do something like this:

    $userId = $_SESSION['userId'];
    $sql = "SELECT * FROM data WHERE userId='$userId'";
    $result = mysql_query($sql) or die(mysql_error());
    $history = array();
    while ($row = mysql_fetch_assoc($result))
    {
      $history[] = $row;     
    }
    

    4) Then simply loop through the $rows using foreach and display the history.