【问题标题】:Why does $_POST convert string to boolean为什么 $_POST 将字符串转换为布尔值
【发布时间】:2016-02-27 07:56:13
【问题描述】:

我有一个非常奇怪的问题,我想要第二双眼睛。我正在尝试一些应该是非常基本的功能。使用 POST 发送表单并检索值。

<form action="index.php?a=2" method="post">
                    <input type="hidden" name="pk" value="'. $_GET["c"] .'">
                    <table id="_person_table">
                <tr>
                    <td id = "_person_create_table"> FIRST NAME: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="afn" id="_person_create_input" value="'.$record[0].'"> </td>
                </tr>                   
                <tr>
                    <td id = "_person_create_table"> LAST NAME: </td>
                    <td id = "_table_nr_plate">  <input type="text" name="overwrite_lastname" id="_person_create_input" value="'.$record[1].'"> </td>

                </tr>   
                <tr>
                    <td id = "_person_create_table"> PHONE NUMBER: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="overwrite_phone_number" id="_person_create_input" value="'.$record[2].'"> </td>
                </tr>   
                <tr>
                    <td id = "_person_create_table"> E MAIL: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="aem" id="_person_create_input" value="'.$record[3].'"> </td>
                </tr>
                <tr>
                    <td id = "_person_create_table"> STREET ADRESS: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="asa" id="_person_create_input" value="'.$record[4].'"> </td>
                </tr>
                <tr>
                    <td id = "_person_create_table"> FULL HOUSE NUMBER: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="ahn" id="_person_create_input" value="'.$record[5].'"> </td>
                </tr>
                <tr>
                    <td id = "_person_create_table"> POSTAL CODE:</td>
                    <td id = "_table_nr_plate"> <input type="text" name="apc" id="_person_create_input" value="'.$record[6].'"> </td>
                </tr>
                </table>
                    <center> <input type="image" src="img/alterUser.png" name="submit" alt="Submit" style="width:100px;height:100px; padding-top:1%;"> </center>
                </form>

上面是表格,当我 var_dump 每个值分开时,它给出了字符串表示。但是,它在 index.php?a=2 上发送和检索的那些它会给出以下转储:

array(10) { 
   ["pk"]=> string(8) "10000007"
   ["afn"]=> string(5) "Lisa" 
   ["overwrite_lastname"]=> bool(true) 
   ["overwrite_phone_number"]=> bool(true)
   ["aem"]=> string(26) "lisa@example.com" 
   ["asa"]=> string(19) "not applicable" 
   ["ahn"]=> string(1) "0" 
   ["apc"]=> string(6)

为什么 POST 中的 10 个值中有 8 个符合预期,但 $_POST["overwrite_lastname"] 和 $_POST["overwrite_phone_number"] 被转换为布尔值?没看到。

更新 #1:var_dump($record);

array(8) { [0]=> string(4) "Lisa" [1]=> string(8) "Dijkstra" [2]=> 字符串(10)“0643482515”[3]=>字符串(17)“lisa@example.com” [4]=> 字符串(19) "不适用" [5]=> 字符串(1) "0" [6]=> 字符串(6)“9743LD”[7]=>字符串(19)“待实现”}

更新 #2:睡了一夜之后,我找到了原因。 我正在检查 POST 值是否设置为:

if($_POST["afn"] != null && $_POST["overwrite_lastname"] =! null && $_POST["overwrite_phone_number"] =!空 && $_POST["aem"] != 空 && $_POST["asa"] != null && $_POST["ahn"] !=null && $_POST["apc"]!= null)

可以看出我将 != 切换为 =!对于作为布尔值返回的值。至少我学会了!可以用来引用true。

【问题讨论】:

  • 您能var_dump($record) 并将其添加到帖子中吗?

标签: php string forms post boolean


【解决方案1】:

你使用PHP的方式不对,试试这个

<form action="index.php?a=2" method="post">
                    <input type="hidden" name="pk" value="<?php echo $_GET["c"]; ?>">
                    <table id="_person_table">
                <tr>
                    <td id = "_person_create_table"> FIRST NAME: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="afn" id="_person_create_input" value="<?php echo $_GET["c"]; ?>"> </td>
                </tr>                   
                <tr>
                    <td id = "_person_create_table"> LAST NAME: </td>
                    <td id = "_table_nr_plate">  <input type="text" name="overwrite_lastname" id="_person_create_input" value="<?php echo $record[0]; ?>"> </td>

                </tr>   
                <tr>
                    <td id = "_person_create_table"> PHONE NUMBER: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="overwrite_phone_number" id="_person_create_input" value="<?php echo $record[1]; ?>"> </td>
                </tr>   
                <tr>
                    <td id = "_person_create_table"> E MAIL: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="aem" id="_person_create_input" value="<?php echo $record[2]; ?>"> </td>
                </tr>
                <tr>
                    <td id = "_person_create_table"> STREET ADRESS: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="asa" id="_person_create_input" value="<?php echo $record[3]; ?>"> </td>
                </tr>
                <tr>
                    <td id = "_person_create_table"> FULL HOUSE NUMBER: </td>
                    <td id = "_table_nr_plate"> <input type="text" name="ahn" id="_person_create_input" value="<?php echo $record[4]; ?>"> </td>
                </tr>
                <tr>
                    <td id = "_person_create_table"> POSTAL CODE:</td>
                    <td id = "_table_nr_plate"> <input type="text" name="apc" id="_person_create_input" value="<?php echo $record[5]; ?>"> </td>
                </tr>
                </table>
                    <center> <input type="image" src="img/alterUser.png" name="submit" alt="Submit" style="width:100px;height:100px; padding-top:1%;"> </center>
                </form>

【讨论】:

  • 我猜他们实际上并没有发布整个文件,而该表单实际上是在一个字符串中,如$form = '&lt;form action="index.php?a=2" method="post"&gt;...'
  • @samlev .. 嗯,也许你是对的,我希望提出问题的用户可以 var_dump 脚本,并粘贴错误
  • 它是回显字符串的一部分,其中 '. 。表示:stop.append.$record[n].append.continue
【解决方案2】:

更新 #2:睡了一夜之后,我找到了原因。我正在检查 POST 值是否设置为:

if($_POST["afn"] != null && $_POST["overwrite_lastname"] =! null && $_POST["overwrite_phone_number"] =!空 && $_POST["aem"] != 空 && $_POST["asa"] != null && $_POST["ahn"] !=null && $_POST["apc"]!= null) 可以看出我将 != 切换为 =!对于返回为的值 布尔值。至少我学会了!可以用来引用true。

【讨论】:

    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 2012-03-10
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多