skip to Main Content

So got a small problem here, tried so many different solution but it always ends up sending a blank value to the database. So i got a dropdown with two values and one value that is never going to be allowed to make a POST. So if you accidentally click on the “Oppdater” button before selecting a value from the dropdown menu i dont want it to be send, but now it does and just adds a blank value to the database. Example:

                <form action="" method="post">
                    <input type="hidden" name="id" value="' . $row['id'] . '">
                    <select name="status2" class="endre-status">
                        <option selected="selected" disabled="disabled">Change status</option>
                        <option value="Done">Done</option>
                        <option value="Pending">Pending</option>
                    </select>
                    <input type="submit" name="insert2" value="Oppdater">
                </form>

Here you can see that i have it selected and disabled and that should mean it will not be sent when i check with isset(), but it still does and puts a blank value in my database.

Here is the code i use to update the database with the new value from the dropdown.

if (isset($_POST['insert2']))
{
    $status2 = $_POST['status2'];
    $id = $_POST['id'];

    $sql2 = ("UPDATE test3 SET status='$status2' WHERE id='$id'");

    if (mysqli_query($conn, $sql2)) {
    header("Location: index.php");
    exit;
} else {
    echo "Error: " . $sql2 . "<br>" . mysqli_error($conn);
}}

Here it how it looks:

enter image description here

I have also tried with setting the value of the disabled option to “0” and checking with empty(), but it still sends “0” to the database. If someone can help me that would be very nice. Thank you.

2

Answers


  1. If your options are static you can check it that if value is from your options or not?

    in_array function can help you

    for example:

    $options = array("Done","Pending");
    if (in_array($_POST['status2'], $options)) {
    
    Login or Signup to reply.
  2. in line :

    <option selected="selected" disabled="disabled">Change status</option>
    

    you have mentioned selected so by default , the first option would be returned as selected option and it seems logical that it returns 0 .
    Suggested solution :
    set a break point on insert2 button and leave dropdown empty , then check the returned value . at last try to prevent code side to send that value by control statements such as If

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search