input type hidden does'nt post

Viewed 43

I have a system where people can indicate whether they are attending an event. There are several events to choose from. The events come from the database and have a unique id. Everything works fine, except the event and date can't be posted together. Only when I make a checkbox of the date (or the event) everything neatly posted. A hidden textfield always posts the wrong date or event.

<form action="insert.php" method="post">

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "user", "pass", "db");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql = "SELECT * FROM events WHERE datum >= CURDATE() AND toon > 0 ORDER BY datum";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){

        while($row = mysqli_fetch_array($result)){
                
echo "<input type=\"radio\" id=\"datum\" name=\"datum\" value=\" "; echo "" . $row['datum'] . "\" required> ";
echo "<input type=\"checkbox\" id=\"event\" name=\"event\" value=\" "; echo "" . $row['event'] . "\"> ";
print date('l d/m/Y', strtotime($row['datum']));
   
              
              echo " &#149; ";
              if(strlen($row['url']) > 0):
              echo "  <a href=\" " . $row['url'] . " \" target=\"_blank\">";
              endif;
              echo "" . $row['event'] . "</a>";
              echo " &#149; " . $row['info'] . "";
              echo "<br>";
        }

        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// Close connection
mysqli_close($link);
?>

<br><br>
<label for="naam">Naam:</label>
<input type="text" name="naam" id="naam" placeholder="Jouw naam..." required>
<input type="hidden" id="mee" name="mee" value="mee">

<label for="opmerking">Opmerking:</label>
<input type="text" name="opmerking" id="opmerking" placeholder="Eventuele opmerkingen...">

<input type="hidden" id="toon" name="toon" value="0">


<input type="submit" value="Ik ga mee!" class="w3-button w3-black">
</form>

</div>
1 Answers

There are a number of issues

  • Each input must have a different name, otherwise how are you going to know which one has been entered?

  • Because you have used escape characters for the double quotes, there is confusion there. To, clarify I have used single quotes inside.

  • Each id must also be unique

As a suggestion, in the loop

$event = $row['event'];
echo "<input type='radio' id='datum-$event' name='datum-$event' value='' $row['datum'] required> ";

So, now the id and name are unique. When this radio button is selected you will have

$_POST['datum-eventname'];

You dont need two inputs in the loop.

Related