最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting value of a disabled textbox in PHP - Stack Overflow

programmeradmin0浏览0评论

I am getting two numbers from users and then displaying their sum in a disabled textbox. I am also storing the inputs in my db.

Everything works fine. I can pass number1 and number2 to my database but i am not able to pass sum to db. I've spent too much time to solve this problem by myself but no luck.

Can you please check my code and let me know what i am missing here? What i want is to store sum into my db.

So this is my form:

    <p>Enter number1:</p>
    <input id="number1" name="number1"/>

    <p>Enter number2:</p>
    <input id="number2" name="number2"/>

    <p>Sum</p>
    <input id="sum" name="sum" disabled="disabled"/>

this is my script:

    var sum = (number1 + number2);
    $('#sum').val(sum);

and this is my php code:

    <?php
    ..connection info here..
    $number1 = $_POST['number1']; 
    $number2 = $_POST['number2']; 
    $sum = $_POST['sum']; 
    $stmt = $mysqli->prepare("INSERT INTO mytable VALUES( '$number1', '$number2', '$sum')");
    if($stmt)
    echo "Done.";
    else
    echo "Error.";

    $stmt->execute();
    $stmt->close();
    $mysqli->close();
    ?>

I am getting two numbers from users and then displaying their sum in a disabled textbox. I am also storing the inputs in my db.

Everything works fine. I can pass number1 and number2 to my database but i am not able to pass sum to db. I've spent too much time to solve this problem by myself but no luck.

Can you please check my code and let me know what i am missing here? What i want is to store sum into my db.

So this is my form:

    <p>Enter number1:</p>
    <input id="number1" name="number1"/>

    <p>Enter number2:</p>
    <input id="number2" name="number2"/>

    <p>Sum</p>
    <input id="sum" name="sum" disabled="disabled"/>

this is my script:

    var sum = (number1 + number2);
    $('#sum').val(sum);

and this is my php code:

    <?php
    ..connection info here..
    $number1 = $_POST['number1']; 
    $number2 = $_POST['number2']; 
    $sum = $_POST['sum']; 
    $stmt = $mysqli->prepare("INSERT INTO mytable VALUES( '$number1', '$number2', '$sum')");
    if($stmt)
    echo "Done.";
    else
    echo "Error.";

    $stmt->execute();
    $stmt->close();
    $mysqli->close();
    ?>
Share Improve this question edited Jul 28, 2015 at 10:58 Mohit Kanwar 3,0508 gold badges40 silver badges61 bronze badges asked Sep 2, 2014 at 22:02 thatthingthatthing 6764 gold badges17 silver badges39 bronze badges 1
  • possible duplicate of I want to get the value of disabled text box in our next jsp but i am getting null value – Mohit Kanwar Commented Jul 28, 2015 at 10:15
Add a comment  | 

3 Answers 3

Reset to default 13

Disabled inputs are not included in form submissions. That is the point of disabling them.

You might want to use readonly instead.

<input id="sum" name="sum" readonly="readonly"/>

So it will be included into submission and not let anyone edit the value too.

Please reference this information:

https://stackoverflow.com/a/3757816/6113439

Because you can't get any values of form control [method='post'] when you insert attribute disabled='disabled'.

You can use readonly='readonly' instead of disabled='disabled'

发布评论

评论列表(0)

  1. 暂无评论