0

Here is my HTML code :

<input type="text" name="Estimation1" id="Val1" maxlength="3" disabled=true/>
<input type="text" name="Estimation2" id="Val2" maxlength="3" disabled=true/>
<input type="text" name="Estimation3" id="Val3" maxlength="3" disabled=true/>
<input type="text" name="Estimation4" id="Val4" maxlength="3" disabled=true/>

And My jQuery :

function CalculAuto()
{

    // get the values from this row:
    var val1 = $('#Val1').val();
    var val2 = $('#Val2').val();
    var val3 = $('#Val3').val();
    if (val1=="") val1=0;
    if (val2=="") val2=0;
    if (val3=="") val3=0;
    var total = 100 - ((val1 * 1) + (val2 * 1) +  (val3 * 1));
    // set total for the row
    $('#Val4').attr('value', total);
}

My problem is : when I send my form, $_POST['Estimation4'] is not recognized. I don't understand why ??

6
  • it is empty or not initialized ? Commented Jul 9, 2014 at 7:40
  • Show your ajax code or the code through which you send your values. Commented Jul 9, 2014 at 7:40
  • what is result of var_dump($_POST); ? Commented Jul 9, 2014 at 7:42
  • is this really disabled? of course it undefined since it won't reach $_POST, change to <input type="text" name="Estimation4" id="Val4" maxlength="3" readonly /> instaed Commented Jul 9, 2014 at 7:46
  • Not initialized==> "Notice: Undefined index: Estimation4 in..." Commented Jul 9, 2014 at 7:49

1 Answer 1

2

Important You need to remove disabled attribute.

<input type="text" name="Estimation4" id="Val4" maxlength="3"/>

Use, .val() to set value. like

$('#Val4').val(total);

Instead of

$('#Val4').attr('value', total);

.val() function gives you string. Hence you need to convert string value into appropriate one.

  1. Use parseFloat() for floating point number. operation
  2. Use parseInt() for operation on integers.

Complete Code

function CalculAuto()
{

    // get the values from this row:
    var val1 = $('#Val1').val();
    var val2 = $('#Val2').val();
    var val3 = $('#Val3').val();

    val1 = val1=="" ? 0 : parseFloat(val1);
    val2 = val2=="" ? 0 : parseFloat(val2);
    val3 = val3=="" ? 0 : parseFloat(val3);

    var total = 100 - ((val1 * 1) + (val2 * 1) +  (val3 * 1));
    // set total for the row
    $('#Val4').val(total);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I've tested .Val() and it has the same effect that .Attr('value', 'string')
@user3710123, No they don't have same effect. Also did you removed disabled attribute.
But how can I do to block this field?
And I have changed CalculAuto() with parseInt() function. Thx!
@user3710123, You can use readonly attribute. You can programmatically change its value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.