Jump to content

retrieve data from form with some disabled elements


RLJ

Recommended Posts

Hi, I have an HTML form with a "submit" button that sends form data to a PHP script.

 

Firstly, the form doesn't seem to send the data to the PHP script, unless I include a line in the PHP script as follows:

$var1 = $_REQUEST['var1'];

 

Is this always the case or is there a way to automatically have the form send through data? The reason I ask is because the form includes elements that can be disabled, in which case (if element 'var1' is disabled) the above PHP line returns an error "Notice: Undefined index: var1"

 

i.e. I want to only send through data from enabled elements and not from disabled elements.

 

Alternatively, is there a line I can include in the PHP script something along these lines:

if (defined($var1)==FALSE) {$var=" ";} so that when the PHP script tries to retrieve var1, which is undefined because the corresponding form element has been disabled, it sets it to a particular value.

 

my HTML form is along these lines:

 

<form action=something.php method=POST>

<select id="var1" name="var1">  
 	<option> </option>
 	<option selected="selected"> option1</option>
	<option> option2</option>
	<option> option3</option>
</select>

<input type="submit" value="next">

</form>

 

Thanks!

Link to comment
Share on other sites

Firstly, the form doesn't seem to send the data to the PHP script, unless I include a line in the PHP script as follows:

$var1 = $_REQUEST['var1'];

 

it always send ALL data that you defined in the form, but NEVE the disabled elements...

 

Alternatively, is there a line I can include in the PHP script something along these lines:

if (defined($var1)==FALSE) {$var=" ";} so that when the PHP script tries to retrieve var1, which is undefined because the corresponding form element has been disabled, it sets it to a particular value.

 

if (isset($_POST['var1])) {
    $var1 = $_POST['var'];

 

This

Link to comment
Share on other sites

Forms also do not send un-checked checkboxes (I wish it would -- I think it should -- but it doesn't).

 

If your form method is POST form fields end up in the $_POST array keyed by the element's name. If the form method is GET they end up in the $_GET array.  You may have seen old code that made them automatically into variables, but this feature has been depricated and is turned off by default now.

 

While jskywalker's suggestion is correct; it will leave the variable undefined if the form field was disabled when the form was posted.  So, expanding that solution you can use an else:

if (isset($_POST['var1'])) {
    $var1 = $_POST['var1'];
} else 
    $var1 = "";
}

 

or you can use the ternary operator:

$var1 = (isset($_POST['var1']) ? $_POST['var1'] : "");

Link to comment
Share on other sites

Forms also do not send un-checked checkboxes (I wish it would -- I think it should -- but it doesn't).

 

an unchecked checkbox is not checked, so there's no need to send it?

for this checkbox you can do this:

 

$var1 = (isset($_POST['var1']) ? true : false);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.