Solved

PHP $_POST and Disabled Fields

Posted on 2010-08-16
13
1,826 Views
Last Modified: 2012-08-13
in PHP using $_POST, how do I retrieve data from html disabled readonly field?

Using form tags and method=post, which contain disabled fields change by ajax, javascripts from onchange events of other associated fields.

When user submits page, I want to retrieve values from the disabled and readonly fields.  How do I do this in PHP?
0
Comment
Question by:thewyzzard
  • 3
  • 2
  • 2
  • +4
13 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You can only get what the form or javascript submits.  PHP running on the server can't get anything from the web page any other way.  It just receives what is sent.
0
 
LVL 3

Expert Comment

by:mpvvee
Comment Utility
just fill the value property for that input and you will get the info
0
 

Author Comment

by:thewyzzard
Comment Utility
I have the following input textbox on screen that is disabled within a form tag, that gets populated from AJAX....as user selects beginning date, AJAX will populate the ending date.  Ending date is disabled.

When I do a postback to page after user clicks submit there isn't any value getting posted, although there is a value on screen.  This is true for any disabled text box when value='something'.

html:
------------------
<tr>
  <td align="right" valign="top">Ending Date:</td>
  <td align="left" valign="top">
    <span id="txtEndingDate">
       <input class="readonly" readonly="true" disabled="disabled" type="text" name="EndingDate" size="13" maxlength="10" value="$ending_date">
    </span>
  </td>
</tr>



php
------------------
if(isset($_POST['EndingDate']))   { $ending_date    = $_POST['EndingDate']; }



How can I get values from the post?


0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I'm afraid you can't.  I took your code above and made a test page.  As long as the field is 'disabled', it doesn't appear in the 'POST' array, it isn't being submitted.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Here's the test page if you want it.  Save it as 'disabled.php' and let it 'post' to itself.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>disabled.php</title>
</head>
<body>
<?php 
print_r($_POST);
$ending_date = "||";
if(isset($_POST['EndingDate']))   { $ending_date = $_POST['EndingDate']; }
 ?>
<table summary="">
<tr>
  <td align="right" valign="top">Ending Date:</td>
  <td align="left" valign="top">
<form action="disabled.php" method="post">
    <span id="txtEndingDate">
       <input class="readonly" readonly="true" disabled="disabled" type="text" name="EndingDate" size="13" maxlength="10" value="<?php echo $ending_date; ?>">
 <!--       <input class="readonly" readonly="true" type="text" name="EndingDate" size="13" maxlength="10" value="<?php echo $ending_date; ?>"> -->
    </span>
<input type="submit" name="submit" value="Submit" />
</form>

</td>
</tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 6

Accepted Solution

by:
ramelong earned 43 total points
Comment Utility
You can "duplicate" the field with a hidden one:

You will receive the hidden field instead of the text one, while you still can show the disabled text field.
I think that you even name both fields (whith the same name)...
<input class="readonly" readonly="true" disabled="disabled" type="text" size="13" maxlength="10" value="<?=$ending_date?>">
<input type="hidden" name="EndingDate" value="<?=$ending_date?>">

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Assisted Solution

by:sscotti
sscotti earned 82 total points
Comment Utility
You can try to dynamical enable the form after you populate it via AJAX and still leave it readonly.  That way the user won't be able to change the value but it should be submitted when you post the page.

I tried that and it seems to work:


2.  document.forms[0].EndingDate.value = 'test';
2.  document.forms[0].EndingDate.disabled = false;

POST form.  It will pass the value and the field is still not writable if it is set to readonly.
0
 
LVL 5

Assisted Solution

by:sscotti
sscotti earned 82 total points
Comment Utility
Why do you have the form element disabled in the first place?  Do you only want to submit that field if you've populated it with something dynamically without user input?
0
 
LVL 2

Expert Comment

by:Tiller79188231
Comment Utility
use a javascript function to submit the values, javascript can read the value of a disabled field
0
 

Author Comment

by:thewyzzard
Comment Utility
From within the developers IDE, Websmart BCD, I found this function that will read disabled html elements when the form is submitted..........thanks for all your ideas....but this solution is the one that worked for me....  xl_EnableDisabledElements

<form id="Detail"  action="thisfile.php" method="post" onsubmit="return xl_EnableDisabledElements(this)">

0
 
LVL 2

Expert Comment

by:Tiller79188231
Comment Utility
that's pretty sure that's what sscotti posted for your to do...
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now