tags:

views:

400

answers:

4

I have the following php form.

I am trying to make it so that when the form is loaded, the values will be assigned the appropriate check- variable. This variable will contain either "checked or "". If it contains checked, the way it is displayed with the html should cause the relevant checkbox to be checked.

As it is, the variables do not seem to be being passed. When I echo out $deleted or $notice from within the submitinfo branch, they are blank. Furthermore, nothing is being inserted into the database, and I am not getting any database error. How can I check this?

<?php
if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"]; else
if (isset($_POST["cmd"]))
  $cmd = $_POST["cmd"]; else
die("Invalid URL");
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","password", "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 STATUS where ARTICLE_NO = '$pk'");
$checkDeleted = "";
$checkNotice = "";
while ($row = mysqli_fetch_assoc($getformdata)) {
    $checkDeleted = $row['deleted'];
    $checkNotice = $row['notice'];
}
if($cmd=="submitinfo") {
    $statusQuery = "INSERT INTO STATUS VALUES (?, ?)";
    if ($statusInfo = $con->prepare($statusQuery)) {
     $statusInfo->bind_param("ss", $deleted, $notice);
     $statusInfo->execute();
     $statusInfo->close();
     echo "true";
    } else {
     echo "false";
    }
    print_r($con->error);
}
if($cmd=="EditStatusData") {
    echo "<form name=\"statusForm\" action=\"test.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." />
<br />
Abmahnung:
<input type=\"checkbox\" name=\"notice\"  value=\"checked\" ".$checkNotice."  />
<br />
<input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
<input name=\"Submit\" type=\"submit\" value=\"submit\" />
</form>";
} else {
    print_r($con->error);
}
A: 

About checking the errors check the web server logs or make sure php is displaying errors in your php.ini.

Are you POSTing to the web page? $deleted and $notice will only be set on POST requests.

gacrux
A: 

IIRC, if the value of the input type "checkbox" is an empty string, it will not be checked. If it is anything else it will be checked. As it looks there, the value will always be "checked".

Stefan Thyberg
from what I was reading, value is what will be POSTed, if you have the word checked after value then it will be checked. e.g. <input type="checkbox" name="x" value="anything" checked>
Joshxtothe4
@Stefan: That would be incorrect. value is what the control will be set if the checked property is also set. <input name="blah" type="checkbox" value="true" checked="checked" /> would add blah=true to the POST STDIN or GET query string.
R. Bemrose
A: 

I'm not sure I follow your code...

Your HTML has:

value="checked"

Which means that if the user checks the box and uploads the form, it will give that field a value of "checked" (as opposed to a value of, say, "Male").

Since you only have those as the values, then there would never be anything inserted into the table as "unchecked", also, your input field has the unchecked and checked DB output just sitting there without any attribute assigned to it... Is that intentional? Is that build into the DB value, otherwise you have something to the effect of:

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

Which would not really do much of anything, at least nothing helpful.

Lastly, in your result fetch, if I'm reading it right, you have:

$checkedValue = $row['checked'];

This means that if you have multiple results, you are only assigning the last row that value, so if you did have, for whatever reason, a blank row at the bottom, this would give you blank variables and no error. Did you mean to assign that to an array, perhaps? Or at least do a check on the result count, like mysql_num_rows(), so that you can confirm that it is only the first row (or only one row, even). You could even add a failsafe and have it end the while loop after one iteration, just in case.

Oh, one last thing, your code will be much cleaner and easier to write and read if you use Heredoc syntax aka EOD. With this, you won't have to comment out all of your double quotes and you can still user variables inline.

Anthony
Based on an example at http://www.echoecho.com/htmlforms09.htm, a checkbox will be checked if there is the word checked after the value identifier. You are right that if the checkbox is not checked there will not be any info such as "unchecked", I had thought this would instead submit "", which would be fine.There should only be one row returned for my result fetch, as ARTICLE_NO is a primary key.I will look into Heredoc syntax
Joshxtothe4
ahh...sorry, spacing got removed from the comment
Joshxtothe4
I just ran that sample code through the w3cvalidator (after I gave it a good doctype and all that stuff) and it returned 25 errors. Most of these are pretty 2005 type errors, like not closing elements with the /> when the element doesn't wrap anything, etc. Finally cleaned it up to where it was just the floating "checked" attribute, and it says that "name and VI delimiter can be omitted from an attribute specification only if SHORTTAG YES is specified", so careful with that. But even then, the value shouldn't be set to checked, checked is just floating, not equal to anything.
Anthony
@Anthony: Leaving checked set to nothing is OK in HTML4. XHTML requires it to be set as checked="checked" .
R. Bemrose
A: 

Easy answer for the form submission side is include a hidden input before each checkbox with the same name and a value of "unchecked". The checkbox will overwrite the value of the hidden input only if checked.

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

That way you always have a value for $_POST['notice'].

For the validation error mentioned above and setting the checked status of the checkbox. According to the w3c standards for xhtml the attribute "checked" needs to have a value, in this case also "checked.

<?php

while ($row = mysqli_fetch_assoc($getformdata)) {
    $checkDeleted = ($row['deleted'] == 'checked') 
        ? 'checked="' . $row['deleted'] . '"' 
        : '';
    $checkNotice = ($row['notice'] == 'checked') 
        ? 'checked="' . $row['notice'] . '"' 
        : '';
}
?>

Now your attribute "checked" will conform to the standard

aaronmccall
R. Bemrose
R. Bemrose: I have never experienced or heard of a browser re-ordering GET or POST order of form fields to something other than the HTML source order. Do you have examples?
aaronmccall
It is extremely bad practice to rely on observed behavior unless it is garanteed never to change, and variable ordering is not something that is set in stone as far as I know.
It is true that simply relying on observed behavior is a bad practice. It is, however, part of the html4 and xhtml1 standards as defined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
aaronmccall