0

EDIT: Clicked post way too early on this!! Basically the values don't pull through and update the database at all but it treats it as though it is a success with no error messages.

I wonder if anyone can solve this for me.

I've got a HTML form that auto submits when a checkbox is ticked and/or unticked.

The HTML looks like this:

        <form action='propupdate.php' id='propupdate' method='post'>
  <input type="hidden" value="<?php echo $userData['id'] ?>" id="ID" form="propupdate">
  <input type="checkbox" value="Yes" id="slideThree" name="check" onchange="document.getElementById('propupdate').submit()" <?php echo ($userData['prop']=='Yes' ? 'checked' : '');?> form="propupdate"/>
  <label for="slideThree"></label>
        </form>

When I check the hidden value it is correct, I've also tested the php which makes it auto checked or unchecked and this works. At first I though the autosubmit was missing the hidden input so I tried adding in form= into the inputs but this didn't seem to work.

propupdate.php looks like this:

<?php

include 'index.php';
include 'credentials.php';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//Create variables
$ID=$_POST['ID'];
$prop=$_POST['slideThree'];

$sql = "UPDATE users SET prop='$prop' WHERE ID='$ID'";

if ($conn->query($sql) === TRUE) {
    header("Location:example.com");
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

Any help is massively appreciated!

1 Answer 1

2

All <input.... that you want to be sent when you submit the form MUST have a name attribute. The id attribute is only used by the DOM and javascript

So add a name attribute to this line

<input type="hidden" value="<?php echo $userData['id'] ?>" name="ID" id="ID" form="propupdate">

and it will get sent to the PHP script

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect dude! Rookie error on my part, thanks for the help. I'll accept it when I can, says I have to wait another 8 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.