php 如何发布禁用的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22989845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:24:50  来源:igfitidea点击:

How do I post disabled input

phppostinputdisabled-input

提问by Thephpdoge

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

你好,我有一些输入,但其中一个被禁用(是的,我需要它作为我的时间表)但是我如何将它发送到 autocomplete.php 到 insert.php 我有这个错误 Undefined index: client1 in C:\wamp\ www\testlp\insert.php 第 30 行

Here my code autocomplete.php

这里我的代码 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

这里我的代码 insert.php

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

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

EDITI 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

这里的错误: Notice: Undefined index: client1 in C:\wamp\www\testlp\insert.php on line 12

采纳答案by RiWe

use the answer from cypherabe: https://stackoverflow.com/a/22990008/2780941

使用密码的答案:https://stackoverflow.com/a/22990008/2780941

回答by cypherabe

use the attribute readonlyinstead of disabled.

使用属性readonly而不是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)
  • readonly:输入不能修改
  • 禁用:输入没有表单功能
  • (以及相关的第三个选项:input type=hidden: input is not visible, but the value is submit)

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)

您收到错误,因为提交表单时未发送禁用的元素,因此不存在$_POST$_POST['client1']在您的情况下根本没有)

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

编辑编辑:示例不完整 - 正如接受的答案所述,该name属性也必须存在

 <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.

如果你想有一个更像 xml 的语法。

回答by TimeTrax

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.

您甚至可以从第一个输入中删除名称。
这样,您禁用的输入仍将显示,但 php 会在您的隐藏输入字段中发布该值。

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

您可以使用 此处的某些答案<?php echo !empty($text)?$text:'';?>来填充value字段

TLDR;

TLDR;

<form action="index.php" method="post">
  <input type="text" disabled  value="my_value"/>
  <input hidden name="client" value="my_value"/>
</form>

回答by Suren

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".

如果您希望禁用它以便它不会在数据库中更改,那么您不必发布它。使用 SELECT 填充<input>并添加属性“禁用”。

<?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>