52833

How to see if a form element was not posted

Question:

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

Answer1:

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']; } }

Answer2:

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.

Recommend

  • How to check status of an SMTP server from Java?
  • Github error message: git-it: command not recognized
  • How can I validate the email writing on UWP?
  • mod_rewrite not passing querystring
  • How to add extra text to the tag in Joomla
  • jQuery Plugin (DataTables) only loads properly on page refresh
  • Unsupported operand types [closed]
  • Giving uploaded images a unique name for mysqli
  • What is supported by broadcasting in tensorflow? How dimensions matches determined?
  • Compare variables PHP
  • Yii: any way to save the images in compressed form?
  • HTML code to upload images
  • Encode string to match encoded form field name in PHP POST array
  • Passing parameter through “window.location.href”
  • array_search() in Session Array
  • PHP: Calling a private method from within a class dying badly
  • Chart.js Multiple dataset
  • How do I access an unhandled exception in an MVC Error view?
  • How do I get HTML corresponding to current DOM tree?
  • Jackson Parser: ignore deserializing for type mismatch
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • Email verification using google app script and google forms
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Email format validation in mvc3 view
  • Why is an OPTIONS request sent to the server?
  • Join two tables and save into third-sql
  • sending/ receiving email in Java
  • How to model a transition system with SPIN
  • Change an a tag attribute in JavaScript based on screen width
  • Upload files with Ajax and Jquery
  • ORA-29908: missing primary invocation for ancillary operator
  • Delete MySQLi record without showing the id in the URL
  • AT Commands to Send SMS not working in Windows 8.1
  • php design question - will a Helper help here?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • Converting MP3 duration time