【问题标题】:How to capture type of html input如何捕获html输入的类型
【发布时间】:2015-08-02 11:03:54
【问题描述】:

当我使用“print_r($_POST)”时,它只是捕获了输入的“名称”。 如何从 html 表单中获取参数输入,例如“类型”或“名称”:

<form method="post">Nama : <input type="text" name="nama" /><br />
Jabatan : <input type="hidden" name="id_jabatan" value="2"><br>
Kontak : <input type="text" name="kontak"><br>
Email : <input type="text" name="email"><br>
UserID : <input type="text" name="userid"><br>
Foto : <input type="file" name="foto"><br>
<br>
<input type="submit" value="Simpan" name="simpan">
</form>

我用以下值填写了该表格: AA 2 W 抄送 dd 辛潘

这是“print_r($_POST)”的结果:

Array
(
    [nama] => AA
    [id_jabatan] => 2
    [kontak] => VV
    [email] => CC
    [userid] => dD
    [foto] => 
    [simpan] => Simpan
)

只是捕获的输入名称 请帮忙:)

【问题讨论】:

标签: php forms


【解决方案1】:

会话数组中的任何信息都可以使用以下函数获得: $this->session->userdata('item');

其中 item 是与您要获取的项目相对应的数组索引。例如,要获取会话 ID,您将执行以下操作: $session_id = $this->session->userdata('session_id');

注意:如果您尝试访问的项目不存在,该函数将返回 FALSE(布尔值)。 试试这样的

$sessiondata = array(
                                  'username' => $username,
                                  'password'=>$password,
                                  'email'=>$email,
                                  'is_login' => TRUE
                             );
                             //$this->login_model->set_session($username);
                             $this->session->set_userdata($sessiondata);
                             print_r($this->session->all_userdata());  //to check

【讨论】:

  • 我不认为你理解他的问题兄弟。
【解决方案2】:

这里有一个简单的例子来演示PHP Simple HTML DOM Parser的用法。

它演示了访问输入字段的“类型”和“值”。

<?php

include __DIR__ .'/vendor/simplehtmldom_1_5/simple_html_dom.php';

// restrict the list of names to use
$inputNames = array("nama", "kontak", "email", "userid", "foto", "id_jabatan", "simpan");

if (empty($_POST)) { // supply default values
    $_POST = array();
    foreach ($inputNames as $name) {
        $_POST[$name] = '';
    }
}


$html = <<<EOF
<!DOCTYPE HTML>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
     <form action="" method="post">
          Nama : <input type="text" name="nama" value="{$_POST['nama']}" ><br />
          Kontak : <input type="text" name="kontak" value="{$_POST['kontak']}"><br />
          Email : <input type="text" name="email" value="{$_POST['email']}"><br />
          UserID : <input type="text" name="userid" value="{$_POST['userid']}"><br />
          Foto : <input type="file" name="foto" value="{$_POST['foto']}"><br />
          <input type="hidden" name="id_jabatan" value="2"><br>
          <br />
          <input type="submit" value="Simpan" name="simpan">
    </form>
  </body>
</html>
EOF;

// parse the HTML and the POST inputs.
if (!empty($_POST)) {
    $htmlDom = str_get_html($html);

    // find all the elements with a name
    $elements = $htmlDom->find('[name]');
    foreach ($elements as $node) {
        if (!in_array($node->attr['name'], $inputNames)) {
            continue; // ignore
        }

        echo 'The Name is: ', $node->attr['name'], ' ',
             'The Tag/Type is: ', $node->tag, ' ', ' / ',  $node->attr['type'], ' ',
             'The Value is: ', $node->attr['value'], '<br />';

    }
}

// show the form with the current values.
echo '<br />';
echo $html;

exit;

输出:

The Name is: nama The Tag/Type is: input / text The Value is:
The Name is: kontak The Tag/Type is: input / text The Value is:
The Name is: email The Tag/Type is: input / text The Value is: xxx@ttt
The Name is: userid The Tag/Type is: input / text The Value is:
The Name is: foto The Tag/Type is: input / file The Value is: Winter.jpg
The Name is: id_jabatan The Tag/Type is: input / hidden The Value is: 2
The Name is: simpan The Tag/Type is: input / submit The Value is: Simpan


Nama :
Kontak :
Email :
UserID :
Foto :

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2017-12-28
    • 2022-11-29
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多