简体   繁体   English

PHP代码中的HTML选项标签

[英]HTML Option Tags inside PHP Code

I'm rather new to PHP (just picked it up for a course on CS) and I'm right now trying to display some values from a Database, namely stocks. 我对PHP相当陌生(只是在CS课程中学习了它),现在我正尝试显示数据库中的一些值,即股票。 Right now I'm running into some issue though as I'm trying to display the stocks symbol from the database in a nice option menu. 现在,我正在尝试在一个不错的选项菜单中显示数据库中的股票符号,但现在遇到了一些问题。 But when I try to retrieve the value via $_POST["stock"] ("stock" is the name of the option) it displays me an error of "Undefinex index: stock". 但是,当我尝试通过$ _POST [“ stock”](“股票”是选项的名称)来检索值时,向我显示了“ Undefinex index:stock”的错误。

Now if I choose the above option (the option before the php code) it actually works perfectly and "stock" is retrievable (and displays nothing, as anticipated). 现在,如果我选择上述选项(php代码之前的选项),则它实际上可以正常工作,并且可以检索“ stock”(并且不显示任何内容,正如预期的那样)。

Now my question is: What did I do wrong and how can I make the name "stock" show the value of $_POST["stock"] 现在我的问题是:我做错了什么,如何使名称“ stock”显示$ _POST [“ stock”]的值

    <select class="form-group">
    <option class='form-control' type='text' name='stock'></option>
    <?php
        $rows = query("SELECT * FROM stocks WHERE id = ?", $_SESSION["id"]);

        foreach ($rows as $row)
        {
            print("<option class='form-control' name='stock'>{$row["symbol"]}</option>");

        }
    ?> 
    </select>   

You should generally always check that your key in $_POST exists to avoid the undefined index issue, and you can also check that it's not empty if defined as examples shows below. 通常,应始终检查$_POST中的键是否存在,以避免出现undefined index问题,如果定义如下所示,还可以检查它是否不为空。

Eg 例如

$stock = isset( $_POST["stock"] ) && !empty( $_POST["stock"] ) ? $_POST["stock"] : '';

Not shorthand: 不速记:

$stock = '';
if ( isset( $_POST["stock"] ) && !empty( $_POST["stock"] ) ) {
    $stock = $_POST["stock"];
}

EDIT 编辑

The reason why $_POST['stock'] is undefined is because your <select> element is missing a defined name, you have the name defined on the childs instead. $_POST['stock']之所以未定义,是因为您的<select>元素缺少定义的名称,而是在子元素上定义了该名称。

Change: 更改:

<select class="form-group">

To: 至:

<select class="form-group" name="stock">

Also, make sure to remove the name attribute from all your <option> elements. 另外,请确保从所有<option>元素中删除name属性。

Best of luck, 祝你好运
Fredrik 弗雷德里克

Bring dynamic parameteres out of double quote and spearate them by dot while printing. 将动态参数带出双引号,并在打印时通过点将其插入。 Also use php isset before consuming posted data (specially while first option has null value): 在使用发布的数据之前,也要使用php isset (特别是当第一个选项具有null值时):

     <select class="form-group">
       <option class='form-control' type='text' name='stock'></option>
        <?php
            $rows = query("SELECT * FROM stocks WHERE id = ?", $_SESSION["id"]);

            foreach ($rows as $row)
            {
                print "<option class='form-control' name='stock'>{".$row["symbol"]."}</option>";

            }
        ?> 

        </select> 

You need to add attribute name to select not in options.use like this 您需要添加属性名称以选择不在选项中。

<select class="form-group" name="stock">
    <option class='form-control' type='text'></option>
    <?php
        $rows = query("SELECT * FROM stocks WHERE id = ?", $_SESSION["id"]);

        foreach ($rows as $row)
        {
print("<option value='".$row["symbol"]."' class='form-control'>".$row["symbol"]."</option>");

        }
    ?> 
    </select>

then with php when you will try to get value with $_POST['stock'] it will return you the option value which is selected . 然后使用php时,当您尝试使用$_POST['stock']来获取价值时,它将返回选择的选项值。 ie $row["symbol"] value in that option. 即该选项中的$row["symbol"]值。

You have several issues, the serious ones are mostly HTML based. 您有几个问题,严重的问题大多基于HTML。

1) Inorder to use the POST variable you need to set the FORM to send the data via post, this is done with the method tag you may or may not already have but : 1)为了使用POST变量,您需要将FORM设置为通过post发送数据,这可以通过您可能已经或可能尚未拥有但method标签来完成:

<form method="post"> 

2) With HTML, the VALUE of the option selected needs to be defined, for example you have 2)对于HTML,需要定义所选选项的VALUE,例如

print("<option class='form-control' name='stock'>{$row["symbol"]}</option>");

But what this does is send a value of "" (if anything) because no value has been defined. 但这是发送""值(如果有的话)的原因,因为尚未定义任何值。 instead write it as: 而是这样写:

 print("<option class='form-control' name='stock' value='stockvalue'>{$row["symbol"]}</option>");

As an additional - the "name" attribute needs to appear in the <select> element, and only the value attribute is needed in the <option> element. 另外,“名称”属性需要出现在<select>元素中,而只有value属性需要出现在<option>元素中。

so: 所以:

print("<option class='form-control' value='stockvalue'>{$row["symbol"]}</option>");

3) query is not a recognised function, unless you made your own function? 3) query不是公认的函数,除非您自己创建函数? You may want mysqli_query() instead. 您可能需要mysqli_query()

4) minor reformatting: Do not use double quotes for array keys, your array should be: $rows['symbol'] . 4)小格式:不对数组键使用双引号,您的数组应为: $rows['symbol'] Also when printing out variables to a print command, get into the habit of shaping it thus: 同样,在将变量输出到print命令时,请养成如下习惯:

  print "<option class='form-control' name='stock' value='stockvalue'>".$row['symbol']."</option>";

This stops printing with the . 这将停止使用打印. character, and appends the raw PHP variable value $row and then appends onto this with another . 字符,并附加原始PHP变量值$row ,然后附加另一个. the rest of the string to print. 其余字符串要打印。 Print also does not need to be in brackets. 打印也不需要放在方括号中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM