PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 23, 2022

[FIXED] Stop a disabled and selected option from sending blank value in a form

 February 23, 2022     forms, php, phpmyadmin, post, sql     No comments   

Issue

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.


Solution

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)) {


Answered By - Fariz Aliyev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing