23

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

Thephpdoge
  • 233
  • 1
  • 2
  • 9

3 Answers3

104

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.

cypherabe
  • 2,562
  • 1
  • 20
  • 35
9

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>
Timetrax
  • 1,373
  • 13
  • 15
  • here " value="inserted_value_of_client1"/> " means value ="" right? – User0434 Apr 16 '19 at 10:25
  • depends. value=6, value=tokenvalue, whatever and however you populate this value is up to the developer. so value can come from php (serverside) value can come from javascript, value can be hardcoded. The question here is how to move this value **FROM THE CLIENT to the server**. doing $_POST['client1'] means that you are moving the value **FROM THE SERVER** (php)/backend to the client(ui)/html/javascriipt. At the time of the question: we don't care how the value reached the client. We are only concerned with getting it from the client to the server – Timetrax Jun 03 '19 at 18:39
  • 1
    simple and efficient :) – vieroli Sep 18 '19 at 15:16
-2

If you want it disabled so it does not change in the DB, then you do not have to POST it. Use the SELECT to populate the <input> and add the attribute "disabled".

<?php
if ( !empty($_POST)) {
$other_inputs= $_POST['other'];

$valid = true;
if (empty($text)) {
    $valid = false;
}

if ($valid) {
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "UPDATE table set text = ? WHERE id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($other_inputs,$id);
}
} else {
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM table where id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($id));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $text = $data['client1'];
}
?>
<form action = 'insert.php' method="post"  >
    <input type="text" name="client1" class = "client" size="12" id ="client1" disabled vlaue="<?php echo !empty($text)?$text:'';?>" />
</form>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Suren
  • 1
  • 4