0

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']?

pythonic
  • 20,589
  • 43
  • 136
  • 219

2 Answers2

4

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'];
    }
}
Max Dominguez
  • 174
  • 1
  • 2
  • !empty($_REQUEST['YourEMail'] is enough for this check: [link](http://php.net/manual/en/function.empty.php) – jaro1989 Aug 17 '16 at 22:46
0

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.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Never use a ternary when the `else` wouldn't make sense. Just use `if` – 4castle Jul 29 '16 at 20:38
  • What part of returning a FALSE doesn't make sense @4castle? – Jay Blanchard Jul 29 '16 at 20:57
  • It's completely arbitrary because it's not being assigned to anything. It's just floating out there. – 4castle Jul 29 '16 at 21:02
  • Curious (I changed to FALSE because that makes more sense) where you came upon this advice? This exact method is used all of the time in a lot of codebases. FALSE is implied if there is no else....unless Java. – Jay Blanchard Jul 29 '16 at 21:02
  • It's also very popular to use `null` as a meaningful value, but that doesn't mean it's good practice. See [this question](http://stackoverflow.com/q/160218/5743988) for best practices for using a ternary (in any language). – 4castle Jul 29 '16 at 21:05
  • I am familiar with that post and nowhere in there does it say returning a NULL or FALSE doesn't make sense @4castle And nothing gets left floating anywhere. – Jay Blanchard Jul 29 '16 at 21:07
  • But you aren't returning it, you're just declaring it and letting it float out there. It's like saying `if (condition) { $x = 5; } else { 2; }` what does that even mean? It's clearer in this case if you don't give an `else` when there wouldn't be anything meaningful to put there. – 4castle Jul 29 '16 at 21:10
  • ¯\\_(ツ)_/¯ I know I am not technically returning anything. We'll just have to agree to disagree on the usage @4castle – Jay Blanchard Jul 29 '16 at 21:18
  • @4castle Your argument makes no sense and IMHO doesn't hold water. As Jay said, you'll just have to agree to disagree. IMO, Jay's answer makes perfect sense and is more streamlined than a full double expression. I agree that there are instances where a full expression would be required, but not in the OP's case. – Funk Forty Niner Jul 29 '16 at 23:41