Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I post disabled input

Hello I have some input but one of them is disabled ( yes and i need it for my time sheet )but how do I send it autocomplete.php to insert.php I've this error Undefined index: client1 in C:\wamp\www\testlp\insert.php on line 30

Here my code autocomplete.php

<form action = 'insert.php' method="post"  >

    <input type="text" name="client1" class = "client" size="12" id ="client1" disabled />

        </form>

here my code insert.php

    session_start(); 
    $date = $_POST['data'] ;
    $client1 = $_POST['client1'] ;

    echo($client1);
    echo($date);

EDIT I tried this :

<input type="text" name="client1" class = "client" size="12" id ="client1"readonly />

here the error : Notice: Undefined index: client1 in C:\wamp\www\testlp\insert.php on line 12

like image 334
Thephpdoge Avatar asked Apr 10 '14 13:04

Thephpdoge


People also ask

How do I make input type submit disabled?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

Are Disabled Fields posted?

If a field is disabled , the value of the field is not sent to the server when the form is submitted. If a field is readonly , the value is sent to the server.

How can I get the value of a disabled input field when posting a form to a PHP script?

How can I get the value of a disabled input field when posting a form to a PHP script? use the attribute readonly instead of disabled .

How do I disable a TextBox?

We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.


2 Answers

use the attribute readonly instead of disabled.

  • readonly: input can't be modified
  • disabled: input has no form function
  • (and the related third option: input type=hidden: input is not visible, but the value is submitted)

you get an error because an disabled element is not sent when the form is submitted and thus is not present in $_POST (there simply is no $_POST['client1'] in your case)

edit edited: the examples were not complete - as the accepted answer states, the name attribute must be present, too

 <input type="text" name="client1" class = "client" size="12" id ="client1" value="something" readonly />

or

 <input type="text" name="client1" class = "client" size="12" id ="client1" value="something" readonly="readonly" />

if you want to have a more xml-like syntax.

like image 161
cypherabe Avatar answered Sep 29 '22 06:09

cypherabe


Here is an idea of how you can solve this

<form action = 'insert.php' method="post"  >
  <input type="text" name="client1" class="client" size="12" id="client1" disabled />
  <input hidden name="client1" value="inserted_value_of_client1"/>
</form>

You can even remove name from the first input.
With this, your disabled input will still be displayed but php will post the value in your hidden input field.

You can use <?php echo !empty($text)?$text:'';?> to populate the value fields as shown in some answers here

TLDR;

<form action="index.php" method="post">
  <input type="text" disabled  value="my_value"/>
  <input hidden name="client" value="my_value"/>
</form>
like image 35
TimeTrax Avatar answered Sep 29 '22 08:09

TimeTrax