【问题标题】:Insert array values into specific field of HTML table将数组值插入 HTML 表的特定字段
【发布时间】:2018-06-23 15:25:09
【问题描述】:

我需要将 pg_query 选择并组织成数组的值插入到输入类型 = 文本的表的特定字段中:

<tr>
  <td>
    <label for="street">Street:</label>
  </td>
  <td>
    <input type="text" placeholder="Street name" required="required" id="street" name="street" onchange="sendForm(this.form)">
  </td>
</tr>

<tr>
  <td>
    <label for="building_no">Building No:</label>
  </td>
  <td>
    <input type="text" placeholder="Number of building (XXXX)" name="bld_no">
  </td>
</tr>

<tr>
  <td>
    <label for="ID Sector">ID Sector:</label>
  </td>
  <td>
    <input type="text" placeholder="IotaNet Sector (5)" name="id_sector">
  </td>
</tr>

所有值都是字符串,数组包含所有选定的值:

$qexist = "SELECT 
       test.tb.street, 
       test.tb.bld_no, 
       test.tb.id_sector, 
  FROM test.tb
  WHERE test.tb.street = '$street'
  AND test.tb.bld_no = '$bld_no'
  "; 
  $ress = pg_query($qexist);
  while ($row = pg_fetch_array($ress)) { 

//   values of $row[] have to be inserted into exact fields
    echo $row[1].'street';
    echo $row[2].'bld_no';
    echo $row[3].'id_sector';
  ? ? ? ? ? ?
    var_dump($row);
      /* result of var_dump
array(30) { [0]=> string(20) "90315464612890004 " ["id_bld"]=> string(20) "90315464612890004 " 
        [1]=> string(50) "Street" ["street"]=> string(50) "Street" 
        [2]=> string(4) "0004" ["bld_no"]=> string(4) "0004" 
        [3]=> string(1) "5" ["id_sector"]=> string(1) "5"................ */
  }

请告知如何解决此问题。我尝试了不同的方法,但没有成功,尽管该站点显示了 echo 和 var_damp 结果,但这些字段仍然为空。谢谢。

【问题讨论】:

  • &lt;input type="text" value="&lt;?php echo $row[1]; ?&gt;" id="street" name="street" onchange="sendForm(this.form)"&gt;。 (php 而必须包装 html &lt;tr&gt;s)。 “onchange”不是一个好主意。这意味着,如果用户键入表单会立即发送
  • @Jeff...这种方式行不通我昨天用不同的方式尝试过,尤其是这样:&lt;input type="text" value="&lt;?= $row[2] ?&gt;" placeholder="Number of building (XXXX)" name="bld_no"&gt;

标签: javascript php jquery arrays postgresql


【解决方案1】:
<input type="text" placeholder="Street name" required="required" id="street" name="street" value="<?php echo $row[1] ?>">

<input type="text" placeholder="Number of building (XXXX)" name="bld_no" value="<?php echo $row[2] ?>">

<input type="text" placeholder="IotaNet Sector (5)" name="id_sector" value="<?php echo $row[3]?>">

【讨论】:

  • 不幸的是,在&lt;input .... &gt; 参数中更改value="&lt;?php echo $row[3] ?&gt;" 的位置也没有效果
【解决方案2】:

我已经通过实验方式找到了解决方案,但它确实有效。我创建了新的变量数组 $value[] ,为它们分配 $row[] 的值,代码开始工作

<?php>
$qexist = "SELECT 
       test.tb.id_bld, 
       test.tb.street, 
       test.tb.bld_no, 
  FROM test.tb
  WHERE test.tb.street = '$street'
  AND test.tb.bld_no = '$bld_no'
  "; 
  $ress = pg_query($qexist);
  while ($row = pg_fetch_array($ress)) { 
        $value[0] = $row[0];
        $value[1] = $row[1];
        $value[2] = $row[2];
  }
 ?>
 

<table>
  <tbody>
    <form method="get" action="t54646.php">
      <tr>
        <td>
          <label for="id_bld">Building ID:</label>
        </td>
        <td>
          <input type="text" placeholder="Not for fill - system ordered" name="id_bld" value="<?php echo $value[0] ?>">
        </td>
      </tr>

      <tr>
        <td>
          <label for="street">Street:</label>
        </td>
        <td>
          <input type="text" placeholder="Street name" required="required" id="street" name="street" onchange="sendForm(this.form)" value="<?php echo $value[1] ?>">
        </td>
      </tr>

      <tr>
        <td>
          <label for="building_no">Building No:</label>
        </td>
        <td>
          <input type="text" placeholder="Number of building (XXXX)" name="bld_no" value="<?php echo $value[2] ?>">
        </td>
      </tr>

如果有人可以解释为什么在变量重命名后数组开始工作,这对很多人来说都会有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多