Php form problems

I have a form that I am trying to display a form with checkboxes and then insert into the database if these checkboxes are checked. Then, every time the page is called, check the value and add information to the html form to display the checkbox if it is checked or not.

I modified the example code below to make it short and I am aware that I am assigning a checked "value" which will do nothing.

An approach

<input type="checkbox" name="notice"  value="checked" checked/>

      

to display a checkbox checked and not valid html works in every browser and is based on the example here . I would prefer to take this approach.

Now I can't see what is wrong with my code.

The first steps should be to fetch the values ​​from the database, from which one single row will be fetched, since article_no is the primary key. If nothing is received, the variables are simply assigned "", which causes the checkbox to be unchecked.

When the value is checked, if selected, it should be inserted into the database and will be displayed as specified the next time the page is viewed.

Now like this, this code fails. firebug shows nothing that is being sent or received in the console, no erros are written from either php or mysql, nothing appears in the database despite the fields and therefore correct, no mysql errors are reported ...

What is the problem?

The code:

<?php

error_reporting(E_ALL);
if (isset($_GET["cmd"]))

  $cmd = $_GET["cmd"];

else
if (isset($_POST["cmd"]))

  $cmd = $_POST["cmd"];


  if (isset($_GET["pk"]))

  { $pk = $_GET["pk"];}
  if (isset($_POST["deleted"]))
  { $deleted = $_POST["deleted"];}
  if (isset($_POST["notice"]))
  { $notice = $_POST["notice"];}

$con = mysqli_connect("localhost","user","pass", "db");

if (!$con) {

   echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();

   exit;

}
$con->set_charset("utf8");
$getformdata = $con->query("select * from TEST where ARTICLE_NO = '$pk'");

$checkDeleted = "";
$checkNotice = "";


  while ($row = mysqli_fetch_assoc($getformdata))

  {

    $checkDeleted = $row['deleted'];

    $checkNotice = $row['notice'];

   } 
if($cmd=="submitinfo"){
$statusQuery = "INSERT INTO TEST VALUES (?, ?, ?)";

if ($statusInfo = $con->prepare($statusQuery)) {

    $statusInfo->bind_param("sss", $pk, $deleted, $notice);

   $statusInfo->execute();

   $statusInfo->close();
} else {

print_r($con->error);

}
}

echo "<form name=\"statusForm\" action=\"x.php\" method=\"post\" enctype=\"multipart/form-data\">

<h1>Editing information for auction: ".$pk."</h1>
Löschung Ebay:
<input type=\"checkbox\" name=\"deleted\" value=\"checked\" ".$checkDeleted." align=\"right\"/>
<br />
Abmahnung:
<input type=\"checkbox\" name=\"notice\"  value=\"checked\" ".$checkNotice."  align=\"left\"/>
<br />
<input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
<input name=\"Submit\" type=\"submit\" value=\"submit\" />
</form>";

      

In the actual form / page, I have the corresponding disinfection.

The big problem is that nothing has been inserted into the database and no errors are returned at all!

0


a source to share


3 answers


It's important to remember that an unchecked checkbox does not add any meaning to the form when it is submitted. When the deleted field is unchecked, the $ _POST ['deleted'] value is not set.

With this in mind, it looks like you are not actually setting the $ deleted value if the checkbox is cleared. Use this idiom.



$deleted = isset($_POST["deleted"]) ? 1: 0;

      

Replace 1 and 0 with whatever values ​​you want in the database table for checked and unchecked states (in your case "checked" and "")

+1


a source


In the same vein as Paul Dixon, I did this from time to time:

<input type="hidden" name="notice" value="not checked" />
<input type="checkbox" name="notice" value="checked" checked="checked" />

      



If the user unchecks the checkbox, a hidden input value is sent instead. This way it $_POST['notice']

always gets posted to your script.

0


a source


I thought the checkboxes should be done like this:

<input type="checkbox" name="notice1"  value="value1" checked="checked"/>
<input type="checkbox" name="notice2"  value="value2" checked="checked"/>

      

-1


a source







All Articles