简体   繁体   English

如何从php表单获取变化的属性的值?

[英]How do I get the value of a changing attribute from a php form?

I am pulling information to be displayed from my database and what id like to happen is when the user clicks on a selection, its takes them to a form that would be just for that selection. 我正在从数据库中提取要显示的信息,并且当用户单击某个选择时,ID会发生什么,它将其带到仅用于该选择的形式。 So i have one form and im trying to pull the name attribute whose value is set but $form_id which gets its value depending on the person chosen. 所以我有一种形式,我试图拉名称属性的值设置,但$ form_id取决于选择的人来获取其值。 on the top id get the attribute's value displayed in my url bar which i dont mind, but i cant seem to echo it out. 在顶部ID上,我不介意在我的网址栏中显示该属性的值,但是我似乎无法将其回显。 After I am able to pull the value i will then place it back into the database. 在能够提取该值之后,我会将其放回数据库中。 Please see code below. 请参见下面的代码。 Thank you! 谢谢!

EDIT: For further clarification, i am pulling a list of names and ID's from the database. 编辑:为进一步澄清,我从数据库中提取名称和ID的列表。 I am also pulling just the ID a second time and assigning it to form_id. 我还将第二次仅提取ID并将其分配给form_id。 I display this information on a button because when you click that button it should take you to a form that will then ask you for information that will be added to the row that holds your form_id. 我将这些信息显示在一个按钮上,因为当您单击该按钮时,它应该带您到一个表单,然后该表单将询问您要添加到包含form_id的行中的信息。 My problem is that form_id isnt coming over as the variable but what its assigned to ex. 我的问题是,form_id不会作为变量出现,而是将其赋给ex。 if form id assumes the value of 7, as per the id it pulls from the database, a $_GET(['form_id']) no longer works because 7 is what is being sent over, not form_id 如果表单id假定值为7(根据它从数据库中提取的id),则$ _GET(['form_id'])不再起作用,因为发送的是7,而不是form_id

Before you submit form (click button): 提交表单之前(单击按钮):

<?php
while($pro_showcase = mysqli_fetch_assoc($pro_s))
{
    $form_id = $pro_showcase['performance_id'];
    echo "<div class=\"xs-col-4 col-sm-offset-2 separate\"><form 
        role=\"form\"action=\"judge-form.php\" method=\"get\"><button name=\"$form_id\"  
        type=\"submit\" class=\"btn btn-danger\">        <p>ID: ";
    echo implode("<br> ",$pro_showcase);

    echo "</p></button></form></div>";
}
?>

After submission, where it submits to: 提交后,提交到:

<?php
if (isset($_GET['submit']))
{
    $n = $_GET["$form_id"];
    echo $n;
}
?>

You got the $form_id mixed up. 您混淆了$form_id The value in your HTML code will be replaced with whatever is in $form_id variable, but you won't have a $form_id array key in $_GET after you submit the form. HTML代码中的值将替换为$form_id变量中的任何值,但提交表单后,在$_GET中将没有$form_id数组键。 This is because the names of the HTML form elements map to keys in $_GET . 这是因为HTML表单元素的名称映射到$_GET What you have done in your code is: map the value of $form_id variable (for example 7 ) to a key in the $_GET . 您在代码中所做的是:将$form_id变量的 (例如7 )映射到$_GET

You should instead use constant name for the HTML form elment, but with value of the $form_id variable: 你应该使用常量的HTML表单elment,但与价值 $form_id变量:

<button name="form_id" value=\"$form_id\" type=\"submit\" class=\"btn btn-danger\">

Then you can capture the value like this: 然后您可以像这样捕获值:

$n = $_GET['form_id'];

(without $ sign). (无$符号)。

If you are confused with parsing variables into strings, you can use the curly syntax , which makes the string a lot more readable. 如果您对将变量解析为字符串感到困惑,则可以使用curl语法 ,这会使字符串更具可读性。

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

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