How to POST form data from within a table to PHP

31,133

Solution 1

I see some errors in your HTML.

I formatted your source locally. I dont know if you did this on purpose (just copy a small part of the table) but there are some tags missing.

I corrected your HTML, maybe you should try that. I dont know if it solves the problem. But here are some errors:

  • You never close the <b> tags in the first table row.
  • There is a table data tag (<td>) you do not close (And which is one too many) after Week 2 hours.
  • You do not close the table row tage for the first row. (<tr>)
  • You do not close the form tag.
  • The word form between the method and action in your opening form tag is incorrect. This should be removed i guess.

<html>

<form method="POST" action="result.php">
<table>
    <tr>
        <td><b>Day of Week</b></td>
        <td><b>Week 1 Hours</b></td>
        <td><b>Week 2 Hours</b></td>
    </tr>
    <tr>
        <td>Monday</td>
        <td><input type="text" name="Monday" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"> <input type="checkbox" tabindex="-1" name="Stime1">Sick?<input type="checkbox" tabindex="-1" name="Vac1">Vacation?</td>
        <td><input type="text" name="Monday2" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"> <input type="checkbox" tabindex="-1" name="Stime2">Sick?<input type="checkbox" tabindex="-1" name="Vac2">Vacation?</td>
    </tr>
</table>
<input type="submit" value="submit">
</form>
</html>

Let me know if this helps. Cheers.

Solution 2

I wanna start to apologize if i have not understood your trouble correctly.

First, you are missing some closing-tags.

Second, You can take a look at how i insert into my DB, and when gets the data afterwards. Hope this will help you a bit. I have it as a template for my self, so i don't need to create the files and the connection all over again when i make a new project, but just change the DB information.

I'm not saying this is THE right way to do it, theres a lot of other ways, but this is how i do it.

HTML

<form action="insert.php" method="post">
   //Insert your table here
   <input name="Monday" value="" type="text" placeholder="Wauw Talk" />
</form>

Insert.php

$conn = mysql_connect('HOST', 'USER', 'PASS');
mysql_select_db('DATABASE', $conn);
if(!$conn){
     die("Connection Error: " . mysql_error());
}

$Monday = $_POST['Monday']; //Var containing your input data

$sql = mysql_query('INSERT INTO table_in_db (Day) VALUES ("'. $Monday .'")')or die(mysql_error());

if (!$sql) {
    echo "Something went wrong...";
}

Header('Location: ...');

Get the data from DB

<?php
    $conn = mysql_connect('HOST', 'USER', 'PASS');
    mysql_select_db('DATABASE', $conn);
    if(!$conn){
         die("Connection Error: " . mysql_error());
    }

    $sql_select = mysql_query('SELECT * FROM table_in_db');

    if(mysql_num_rows($sql_select) > 0){
     while($row = mysql_fetch_assoc($sql_select)){
      ?><div class="Days">
         <label class="Mondayclass" ><? echo $row["Day"]; ?></label>
        </div>
<?php
   }
}
?>

Hope this helps. If this is totally not what you ware looking for, I'm sorry, i have misunderstood the problem you ware having.

Share:
31,133
LuckoftheLefty
Author by

LuckoftheLefty

Updated on August 01, 2020

Comments

  • LuckoftheLefty
    LuckoftheLefty almost 4 years

    I currently have a Timesheet form I am creating for the employees at my work and I am stuck at getting the input values to post correctly. I have a database set up but right now, I can't even get the values to show in a .php page. Here is a sample from my current table:

    <html>    
        <form method="POST" form action="result.php">
        <table>
            <tr><td><b>Day of Week</td><td><b>Week 1 Hours</td><td><b>Week 2 Hours</td>  <td>
            <tr><td>Monday</td><td>
            <input type="text" name="Monday" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')">
            <input type="checkbox" tabindex="-1" name="Stime1">Sick?<input type="checkbox" tabindex="-1" name="Vac1">Vacation?</td>
            <td><input type="text" name="Monday2" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')">
            <input type="checkbox" tabindex="-1" name="Stime2">Sick?<input type="checkbox" tabindex="-1" name="Vac2">Vacation?</td></tr>
        </table>
        <input type="submit" value="submit">
    </html>
    

    This pattern continues for each day of the week. And then when I try to view the results of the post I couldn't get anything to work. I resorted to trying:

    <html>
    <?php var_dump ($_POST);
    ?>
    </html>
    

    All I get is a blank page, and if I view source it just shows the php code used. It's late here so I must be too tired and missing something but I just can't figure it out.

  • LuckoftheLefty
    LuckoftheLefty about 9 years
    Thanks for the help guys, and so quick too! it was a matter of my server configurations but thanks for the html help also. I went down through and cleaned up the rest of it as well. Awesome advice from everyone!