-6

So I have a guestlist in a PHP table, but I want to check the guests that have arrived.

$sql='SELECT * FROM guestlist LIMIT ' . $this_page_first_result . ',' .  $results_per_page;
$result = mysqli_query($connect, $sql);

echo "<table><tr><th>ID</th><th>Name</th><th>Surname</th><th>Check-in</th></tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr><td>" . $row["id"]. "</td>
            <td>" . $row["name"]. "</td>
            <td> " . $row["surname"]. "</td>
            <td><input type='checkbox' class='get_value' name='checkbox'  ".$row["check-in"]. "></td>
            <td><input type='submit' name='submit' value='submit' ".$row["submit"]. "></td>
          </tr>";
}
echo "</table>";
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
megi
  • 1
  • 1
  • 1
    Nothing there shows your checkboxes. .. – Ingus Jan 09 '19 at 14:21
  • Are you just looking for the `checked` attribute for an `` element? – David Jan 09 '19 at 14:22
  • $sql='SELECT * FROM guestlist LIMIT ' . $this_page_first_result . ',' . $results_per_page; $result = mysqli_query($connect, $sql); echo ""; while($row = mysqli_fetch_array($result)) { echo "";
    IDNameSurnameCheck-in
    " . $row["id"]. "" . $row["name"]. " " . $row["surname"]. "
    – megi Jan 09 '19 at 14:24
  • what does `$row["check-in"]` contain? is it a bool, a string? – Stender Jan 09 '19 at 14:24
  • Can you dump `$row`. And why do you tagged ajax ? – executable Jan 09 '19 at 14:25
  • 1
    it contains checkboxes... – megi Jan 09 '19 at 14:27
  • because someone told me that i can do it with ajax – megi Jan 09 '19 at 14:28
  • What's a PHP Table? You're showing some SQL code so that would be a database table - but then you mention checkboxes, which would be an HTML Table... could you add some clarity on what it is you're *actually* trying to do? – CD001 Jan 09 '19 at 14:29
  • 1
    @megi Didn't know there was a datatype called checkboxes... – Stender Jan 09 '19 at 14:30
  • i have a php table with the id,name and surname of some guests, i have a another row in the table with checkboxes. all i want to do is check the names of the guests who have arrived. i mean i check them and when i refresh the page, the checkbox does not have the tick in it – megi Jan 09 '19 at 14:32
  • 2
    In future, please format you code example so it is easily readable and if at all possible without needing to scroll into next week to see the end of a line. That will also make it easier for you to read and **debug** yourself – RiggsFolly Jan 09 '19 at 14:35

1 Answers1

2

I do not know what $row['check-in'] contains but you just need use html checked:

if($row["check-in"]=='on'){
    $checked=" checked='checked'";
}else{
    $checked='';
}
echo "<td><input type='checkbox' class='get_value' name='checkbox'  ".$checked."></td>";

Edit: to use in loop:

echo "<td><input type='checkbox' class='get_value' id='checkbox[]' name='checkbox[]'  ".$checked."></td>";
Ingus
  • 1,026
  • 12
  • 34
  • 1
    Just to be sure it works on all browsers https://stackoverflow.com/questions/7851868/whats-the-proper-value-for-a-checked-attribute-of-an-html-checkbox – RiggsFolly Jan 09 '19 at 14:38
  • no it did not help me...the checkboxes are still uncheck when i refresh the page – megi Jan 09 '19 at 14:46
  • 1
    You need to change the `'on'` to whatever you are looking for in the database – Stender Jan 09 '19 at 14:47
  • @megi it is probably because of `on` in `if($row["check-in"]=='on'){` you must compair to what `check-in` contains. I hope that `check-in` are not ton of unique values. .. – Ingus Jan 09 '19 at 14:48
  • it contains empty boxes where you can put a tick to show if a guest has arrived or not – megi Jan 09 '19 at 14:50
  • @megi what does `empty boxes` means? – Ingus Jan 09 '19 at 14:50
  • can you please `var_dump($row)` so we can see the data - this is getting us no where – Stender Jan 09 '19 at 14:51
  • 1
    @megi what value `$row["check-in"]` contains when guest have arrived and what value when guest has not arrived? – Ingus Jan 09 '19 at 14:52
  • i have not include any values. So all i want is a table with the names of some guests and to be able to check the names that have arrived. – megi Jan 09 '19 at 14:55
  • @megi if you are not able to click checkboxes you need to use `[]` for name `name='checkbox[]'` – Ingus Jan 09 '19 at 14:58
  • i did it, i click in the check box but when i refresh the page, the box is empy again – megi Jan 09 '19 at 15:00
  • 1
    Do you save the data after checking it in? – Stender Jan 09 '19 at 15:00
  • @megi you need to save data – Ingus Jan 09 '19 at 15:00