简体   繁体   English

PHP 如何:多个提交按钮持有不同的数据但相同的值

[英]PHP How to: multiple submit buttons holding different data but the same value

I have a numeric array (eg 0,1,2,3...) displayed in a table.我在表格中显示了一个数字数组(例如 0,1,2,3...)。 Each row contains an <input type="submit" name="button" value="Click me"> next to the number to do something with the data from this specific object of the array (which is the number).每行在数字旁边包含一个<input type="submit" name="button" value="Click me">以对来自数组的这个特定 object 的数据(即数字)执行某些操作。

Using $_POST I can get the information about the button by its defined name, but they obviously all hold the same value (Click Me) and it wouldn't matter which button you clicked.使用 $_POST 我可以通过其定义的名称获取有关按钮的信息,但它们显然都具有相同的值(单击我),并且单击哪个按钮并不重要。

To define a new scenario (eg with if-functions) for each name of every button is impossible, because I do not now how long the array could be.为每个按钮的每个名称定义一个新场景(例如使用 if 函数)是不可能的,因为我现在不知道数组可以有多长。

Is there a way to use something like an additional ID for the button, which would then be a variable?有没有办法使用按钮的附加 ID 之类的东西,这将是一个变量?

Alternatively I could use the array as a name of the input, but then I would need some PHP to do something like:或者,我可以使用数组作为输入的名称,但是我需要一些 PHP 来执行以下操作:

$x = $_POST[array[]];
if ($x == 0) { do something with array[0];}

but for every number of the array.但是对于数组的每个数字。

No matter which number of the array, I always want to do the same thing with it.无论是哪个数组,我总是想用它做同样的事情。

Is there either an approach with PHP functions and arrays or in the HTML form?是否有 PHP 函数和 arrays 或 HTML 形式的方法?

If you are looping your array, then you could add a number after the word button that would get higher after every input.如果您正在循环您的数组,那么您可以在单词按钮之后添加一个数字,该数字在每次输入后会变高。 Sorry if this is kind of on the rough side, as I'm trying to imagining how your coding may already look.抱歉,如果这有点粗糙,因为我正在尝试想象您的编码可能已经看起来如何。

    foreach ($array as $value) {
        $a++;
        echo "<input type=\"submit\" name=\"button$a\" value=\"Click me\">$value";
    }

then just loop though the post data然后只是循环发布数据

    do {
        $x++;
        echo "".$_POST["button$x"]."";

    } while (!empty($_POST["button$x"]));

Got something similar i'm just using arrays, i also don't know if you can modify your buttons names but i still propose something, first i suppose your code could be something like:有类似的东西我只是使用 arrays,我也不知道你是否可以修改你的按钮名称,但我仍然提出一些建议,首先我想你的代码可能是这样的:

$things=[1,2,3,4,5,6,7,8,9,10];//.....
?>
<form method="post">
<?php
foreach ($things as $value) 
{
    ?>
    <input type="submit" name="button[<?php echo $value?>]" value="Click me"><br>
    <?php
}
?>
</form>
<?php

Then after it's send:然后发送后:

echo array_search('Click me',$_POST['button']);

I think it costs less computing seachring我认为它的计算成本更低

Give this a try.试试这个。 I used href to send the value to next page and $_GET to get the value我使用 href 将值发送到下一页并使用 $_GET 来获取值

 <?php
 $numlist=[1,2,3,4,5,6,7,8,9,10];
 for($i=0;$i=count($numlist);$i++)
{
//abc.php being the page you want to sent the data to

echo "<a href='abc.php?value=$numlist[$i]'><button>Click me!</button></a>";
}
?>

<?php
//to get your valueof button clicked inside abc.php

$value=$_GET['value'];
?>

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

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