pythonic
pythonic

Reputation: 21625

How to see if a form element was not posted

I have a form where e-mail is optional. To control that there is a checkbox. If that checkbox is unchecked, the e-mail textbox would be disabled and therefore not posted on submit. However, on the next page, if I have code like as shown below, it gives me an error if the e-mail textbox is disabled.

if (isset($_POST['submit'])) 
{ 
    $_SESSION["email"]      = $_REQUEST['YourEMail'];
    ....
}

To get around that problem, I progammatically enable a disabled e-mail textbox just before submitting besides setting its value to an empty string. The code for that is shown below.

document.getElementById('YourEMail').disabled = false
document.getElementById('YourEMail').value = ''

However, one annoying problem remains, which is that, if the user goes back to the original page, the e-mail textbox is enabled, since I enabled it problematically just before submitting the form. However, I want it to be disabled in that case. How, can I achieve that? Alternatively, how in the next page, I could see that e-mail box was disabled and therefore not even try to read $_REQUEST['YourEmail']?

Upvotes: 0

Views: 98

Answers (2)

Jay Blanchard
Jay Blanchard

Reputation: 34416

You can test it like this using a ternary:

(isset($_REQUEST['YourEMail']) && !empty($_REQUEST['YourEMail'])) ? $_SESSION["email"] = $_REQUEST['YourEMail'] : FALSE;

This would only set the session variable if the request variable is set.

Upvotes: 0

Max Dominguez
Max Dominguez

Reputation: 174

if the field "#YourEMail" is optional you can check if exists in PHP. There is no need for enable/disable the field using JS.

if (isset($_POST['submit'])) 
{
    if (isset($_REQUEST['YourEMail']) && !empty($_REQUEST['YourEMail'])){
        $_SESSION["email"] = $_REQUEST['YourEMail'];
    }
}

Upvotes: 4

Related Questions