简体   繁体   中英

html/PHP - Form POST empty

I am wondering where my mistake is. I've been looking at different solutions on why the $_POST is empty, and none have come up with an answer as to why it is happening.

i have tried the following changes in HTML method="GET" and method="POST" - neither works action="test.php" / action="localhost/test.php" / action="c:\xampp\htdocs\test.php"

Here is the HTML Form code

<form name="Driftslog" action="localhost/test.php" method="POST">
    Init: <input type="text" id="Init" size="5" maxlength="5" autocomplete="on" required> <br></br>
    LID: <input type="text" id="LID" size="8" maxlength="8" required><br></br>
    Ticket-ID: <input type="text" id="TicketID" size="20" maxlength="15" required><br></br>
    Kunde: <input type="text" id="Kunde" size="25" maxlength="50" required><br></br>
    Start tid: <input type="datetime" id="StartTid" size="15" value="" required> <br></br>
    Slut tid: <input type="datetime" id="SlutTid" size="15" value="" required><br></br\
    Tilkald <input type="checkbox" id="Tilakd"><br></br>
    Planlagt <input type="checkbox" id="Planlagt"><br></br>
    Andet <input type="checkbox" id="Andet"><br></br>
    <input type="submit" value="Opret">
</form>

and here is my PHP file test.php in this PHP file i tried various changes. i commented out the entire IF section and just tried to print the variable to see what was in it, using var_dump($_POST) /print_r and a for each loop. all came back empty

it is like the HTML does not pass the data to the php file

The only thing that works in the php file is the last echo and page redirect

<?php 
if(isset($_POST['submit']))
{
  $Init = trim($_POST["Init"]);
  $LID = trim($_POST["LID"]);
  $TicketID = trim($_POST["TicketID"]);
  $Kunde = trim($_POST["Kunde"]);
  $StarttTid = trim($_POST["StartTid"]);
  $SlutTid = trim($_POST["SlutTid"]);
  $data = [ $Init, $LID, $TicketID, $Kunde, $StartTid, $SlutTid, "\n"];

$f = fopen("db.csv","a");
fputcsv($f, $data);
fclose($f);

print $TicketID;
} 

echo "oprettet med success";
header("Refresh:3; url=http://localhost"); 
exit();
?>

In your input fields you are missing the name='' attributes

Problem -

Your form values do not make it to your PHP.

Solution -

  • Updated if statement to check for "REQUEST_METHOD"
  • Use "name" in form fields. You can not rely on ID.

Example -

index.html -

 <form name="Driftslog" action="localhost/test.php" method="POST">
        Init: <input type="text" id="Init" name="Init" size="5" maxlength="5" autocomplete="on" required> <br></br>
        LID: <input type="text" id="LID" name="LID" size="8" maxlength="8" required><br></br>
        Ticket-ID: <input type="text" id="TicketID" name="TicketID"  size="20" maxlength="15" required><br></br>
        Kunde: <input type="text" id="Kunde" name="Kunde" size="25" maxlength="50" required><br></br>
        Start tid: <input type="datetime" id="StartTid" name="StartTid" size="15" value="" required> <br></br>
        Slut tid: <input type="datetime" id="SlutTid" name="SlutTid" size="15" value="" required><br></br\
        Tilkald <input type="checkbox" id="Tilakd" name="Tilakd"><br></br>
        Planlagt <input type="checkbox" id="Planlagt" name="Planlagt"><br></br>
        Andet <input type="checkbox" id="Andet" name="Andet"><br></br>
        <input type="submit" value="Opret">
    </form>

test.php -

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $Init = trim($_POST["Init"]);
  $LID = trim($_POST["LID"]);
  $TicketID = trim($_POST["TicketID"]);
  $Kunde = trim($_POST["Kunde"]);
  $StarttTid = trim($_POST["StartTid"]);
  $SlutTid = trim($_POST["SlutTid"]);
  $data = [ $Init, $LID, $TicketID, $Kunde, $StartTid, $SlutTid, "\n"];

$f = fopen("db.csv","a");
fputcsv($f, $data);
fclose($f);

print $TicketID;
} 

echo "oprettet med success";
header("Refresh:3; url=http://localhost"); 
exit();
?>

To read fields from the post request the input fields must have the name attribute.

Change your HTML to something like this:

<form name="Driftslog" action="localhost/test.php" method="POST">
    Init: <input type="text" id="Init" name="Init" size="5" maxlength="5" autocomplete="on" required> <br></br>
    LID: <input type="text" id="LID" name="LID" size="8" maxlength="8" required><br></br>
    Ticket-ID: <input type="text" id="TicketID" name="TicketID" size="20" maxlength="15" required><br></br>
    Kunde: <input type="text" id="Kunde" name="Kunde" size="25" maxlength="50" required><br></br>
    Start tid: <input type="datetime" id="StartTid" name="StartTid" size="15" value="" required> <br></br>
    Slut tid: <input type="datetime" id="SlutTid" name="SlutTid" size="15" value="" required><br></br\
    Tilkald <input type="checkbox" id="Tilakd" name="Tilakd"><br></br>
    Planlagt <input type="checkbox" id="Planlagt" name="Planlagt"><br></br>
    Andet <input type="checkbox" id="Andet" name="Andet"><br></br>
    <input type="submit" name="submit" value="Opret">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM