0

I have a form which calculates a cost and sets the value of an input accordingly. For obvious reasons, I have used:

$('#totalcost').attr('disabled',true);

to prevent the user from being able to edit the cost.

However, when I do this, the PHP script I'm using to mail the form doesn't pick up the input (not just the value - it doesn't read the input at all). When the input is enabled, it works fine.

How can I prevent the user from editing the input while still having the PHP mailing the value? Or is there a better way to do this anyway?

ElendilTheTall
  • 1,344
  • 15
  • 23
  • The values of disabled form elements are not send – this is explicitly _specified_ this way. Use `readonly` instead. – CBroe Nov 28 '13 at 12:56
  • Well better place an hidden field also , this might help http://stackoverflow.com/questions/6241943/how-can-i-make-an-input-field-read-only-but-still-have-it-send-data-back-to-a-fo – Deepanshu Goyal Nov 28 '13 at 12:58
  • Then again, inputs can still be edited after pressing submit, an example tool would be the Firefox extension: `tamperdata` – Daryl Gill Nov 28 '13 at 12:59

6 Answers6

6

Make it readonly, not disabled:

$("#totalcost").attr('readonly', true);

You could also do it in the original HTML, since it's not something you really want to change back and forth dynamically.

<input id="totalcost" type="text" readonly>
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Use Read Only property, Though i guess it wont work in internet exploer.

sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
0

Add readonly attribute, like this:

$("#totalcost").attr('readonly', true);
display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
0

Add property readonly="true".

$('#totalcost').attr('readonly',true);
Tom Chung
  • 1,412
  • 9
  • 12
0

you can try:

$("#totalcost").prop("readonly",true);
JosephC
  • 166
  • 1
  • 7
0

Use read only in html itself or in script

<input type="text" name="totalcost" id="totalcost" value="" readonly>

$('#totalcost').attr("readonly", true);
user3040610
  • 750
  • 4
  • 15