0

I'm having a problem with my code where there's a select that's 'disabled' and it doesn't send the data to the next page. I've tried using hidden input but it didn't work.

This is the part where the code takes the value of the $payt variable and it is selected for the user, but he cannot change it.

<select name="payt" id="payt" <?php if ($tsktype == "csd") { echo "readonly"; } ?>>
<option value="<?php echo $payt ?>"><?php echo $payt ?></option>
<?php 
    while($rowpayt = @mysqli_fetch_array($aPAYT,MYSQLI_ASSOC))
    {
        $payt = $rowpayt['payTerms'];
    $paytd = $rowpayt['ptdescr'];
?>
<option value="<?php echo $payt ?>"><?php echo $paytd ?></option>
<?php
    }
?>

</select>

And here is the part of the other file where he was supposed to receive it.

if(!empty($_POST['payt'])){
    $payt = $_POST['payt'];
    echo "AQUI ESTÁ O PAYT: $payt";
}
else{
    echo "Não está definida";
}

I've already tried it as readonly and hidden, neither worked.

7
  • 1
    Disabled inputs are not submitted in a form. There's nothing you can do about that. But if you don't want the user to change the value then why bother to build a select box? That makes no sense. You should be able to use a hidden field (it's unclear why this didn't work for you because you didn't show a minimal reproducible example involving that issue), but even that can be manipulated by a user who knows what they're doing...so if it's completely essential that the user does not modify this value then you should not send it to the browser at all, but rather store and pass it server side, e.g. via the Session Commented Aug 13, 2024 at 16:40
  • @ADyson Well, I need this box because a group of x users will be able to change it and another group of x users will not be able to change it. Sorry about something, I'm new to this stackoverflow thing. Commented Aug 13, 2024 at 16:51
  • 1
    I would probably only echo the select if the user can make the change, and just show the value if they cannot. readonly is not supported on select types, so it would not be an option. Commented Aug 13, 2024 at 17:06
  • 1
    For those who cannot use it, don't render it... Commented Aug 13, 2024 at 17:20
  • hey, try to use javascript or Jquery get the value and pass to the next page. just an idea. Commented Aug 13, 2024 at 18:00

1 Answer 1

1

Good day! The issue here is that when a element is disabled or set to readonly, its value is not submitted with the form. This is why your hidden input approach didn't work as expected.

To solve this, you can keep the disabled for the user interaction but still send its value by using a hidden input field alongside it.

Firstly, Keep the disabled or readonly for the user. Use a hidden input to carry the value of the when the form is submitted. Try this:

<select name="payt_disabled" id="payt" disabled>
    <option value="<?php echo $payt ?>"><?php echo $payt ?></option>
    <?php 
        while($rowpayt = @mysqli_fetch_array($aPAYT, MYSQLI_ASSOC))
        {
            $payt = $rowpayt['payTerms'];
            $paytd = $rowpayt['ptdescr'];
    ?>
    <option value="<?php echo $payt ?>"><?php echo $paytd ?></option>
    <?php
        }
    ?>
</select>

<!-- Hidden input to pass the value -->
<input type="hidden" name="payt" value="<?php echo $payt; ?>" />

On the receiving page, you can access the value like this:

if (!empty($_POST['payt'])) {
    $payt = $_POST['payt'];
    echo "AQUI ESTÁ O PAYT: $payt";
} else {
    echo "Não está definida";
}

This approach ensures that the value from the disabled element is submitted and accessible on the next page.

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

2 Comments

So, I tried to put it with the hidden tag but it didn't send. I managed to find a solution that was adding an IF. @miltonhyndrex
Awesome, glad you found a way around it!

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.