如何使用“onchange=”向 PHP 发送多个值?
我有两个单选按钮类别:color1 和 color2。颜色 1 可以是黑色或白色,颜色 2 可以是红色或蓝色。我只是希望用户能够从一个类别或两个类别中进行选择,然后使用 PHP 回显这些选择。
我的问题是它不会同时回显两个类别的选择。例如,如果我单击“白色”单选按钮,然后单击“蓝色”单选按钮,则它只会回显“蓝色”,而不是同时回显“白色”和“蓝色”。
更新:
<?php
session_start();
if(isset($_GET['color1'])){
$_SESSION['color1'] = $_GET['color1'];
}
if(isset($_GET['color2'])){
$_SESSION['color2'] = $_GET['color2'];
}
echo $_SESSION['color1'];
echo $_SESSION['color2'];
?>
select first color:<br/>
<input type="radio" name="color1" value="black" onchange="window.location.href='recom.php?color1='+this.value"> black<br />
<input type="radio" name="color1" value="white" onchange="window.location.href='recom.php?color1='+this.value"> white<br />
select second color:<br/>
<input type="radio" name="color2" value="red" onchange="window.location.href='recom.php?color2='+this.value"> red<br />
<input type="radio" name="color2" value="blue" onchange="window.location.href='recom.php?color2='+this.value"> blue
I have two radio button categories: color1 and color2. Color 1 can be either black or white and color 2 can be either red or blue. I just want the user to be able to pick from just one category or both categories and then echo out the choices with PHP.
My problem is that it will not echo out choices from both categories at the same time. For example, if I click the "white" radio button and then click the "blue" radio button, it will only echo out "blue", not both "white" and "blue".
UPDATE:
<?php
session_start();
if(isset($_GET['color1'])){
$_SESSION['color1'] = $_GET['color1'];
}
if(isset($_GET['color2'])){
$_SESSION['color2'] = $_GET['color2'];
}
echo $_SESSION['color1'];
echo $_SESSION['color2'];
?>
select first color:<br/>
<input type="radio" name="color1" value="black" onchange="window.location.href='recom.php?color1='+this.value"> black<br />
<input type="radio" name="color1" value="white" onchange="window.location.href='recom.php?color1='+this.value"> white<br />
select second color:<br/>
<input type="radio" name="color2" value="red" onchange="window.location.href='recom.php?color2='+this.value"> red<br />
<input type="radio" name="color2" value="blue" onchange="window.location.href='recom.php?color2='+this.value"> blue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在执行请求时发送这两个变量(正如 yes123 的回答所示)。
或者,您可以在 PHP 中使用
$_SESSION
来保留已选择和未选择内容的状态。您可以在此处阅读会议内容
http://www.tizag.com/phpT/phpsessions.php
会话只是一个数组(我保持非常简单)。您可以在该数组中放入一些值,每次重新加载页面时,这些值都会保留 - 除非您关闭浏览器窗口。
在你的情况下,
我还建议不要使用“红色”,“蓝色”等。
给每种颜色一个 id - 红色 = 1,蓝色 = 5 等。
然后你做
$_SESSION['color2'] = intval($ _GET['color2']);
这意味着您将颜色存储为数字,很容易使用intval()
进行清理。当您需要显示已选择的颜色时,您只需编写一个方法,该方法接受一个数字并返回一个带有颜色的字符串。 (简单的 switch 语句)。这将阻止人们向你的 html 注入一些垃圾。You need to send both variables to when you are doing request (just as shown in answer by yes123).
Or you can use
$_SESSION
in your PHP to keep a state of what has been selected and hasn't.You can read on sessions here
http://www.tizag.com/phpT/phpsessions.php
Session is just an array (i'm keeping it very simple). You can put some values in that array and each time page is reloaded those values will stick around - unless you close your browser window.
In your case
I would also suggest not using "red", "blue" etc.
Give each colour an id - red = 1, blue = 5 etc.
Then you do
$_SESSION['color2'] = intval($_GET['color2']);
This means that you store your colours as numbers which are easy to sanitize withintval()
. When you need to display which colour has been selected you can just write a method that takes a number and returns a string with colour. (simple switch statement). That will stop people from injecting your html with some garbage.还可以考虑使用 jQuery
Also consider to use jQuery
您可以将两个单选按钮放入表单中,并在两个 onchange 事件中,只需提交表单即可。
you can put both radio buttons in a form, and in both onchange events, just submit the form.
只需使用一个类...
Just use a class ...
尝试这样的事情:
try something like this: