The problem of passing variables in php form

I have the following php form.

I am trying to make it so that when the form is loaded, the values ​​are assigned the appropriate control variable. This variable will contain either "checked" or "". If it contains a checkbox, then the html way to render it is to check the corresponding checkbox.

However, the variables do not seem to be passed. When I checkout $ deleted or $ notice from the submitinfo branch, they are empty. Also, nothing is being inserted into the database and I am not getting any database errors. 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);
}

      

0


a source to share


4 answers


Check your web server logs about error checking or make sure php displays errors in your php.ini.



Are you POSTing to a webpage? $ deleted and $ notice will only be set in POST requests.

0


a source


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



0


a source


I'm not sure I am following your code ...

Your HTML has:

value="checked"

      

This means that if the user checks the window and loads the form, they will give this field a "checked" value (as opposed to, say, "Male").

Since you only have these values ​​then there will never be anything inserted into the table as "unchecked" furthermore your input field has unchecked and checked DB output just sitting there without any attribute assigned to it. Is this intentional? This is built into the DB value, otherwise you can do something:

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

      

Whatever really means nothing, at least nothing useful.

Finally, in your sample of results, if I am reading it correctly, you have:

$checkedValue = $row['checked'];

      

This means that if you have multiple results, you are only assigning the last line, so if you have, for some reason, an empty line at the bottom, this will give you empty variables and no errors. Perhaps you meant to assign this to an array? Or at least check the result counter like mysql_num_rows () so you can confirm that this is only the first row (or just one row, even). You can even add fault tolerance and end its while loop after one iteration just in case.

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

0


a source


The simple answer for the form submission side includes hidden input in front of each checkbox with the same name and value "unchecked". The checkbox will overwrite the hidden input value only if checked.

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

      

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

For the above error checking and checking the checkbox. According to the w3c standards for xhtml, the "checked" attribute must 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'] . '"' 
        : '';
}
?>

      

Your "checked" attribute will now conform to the standard

0


a source







All Articles