如何使用“onchange=”向 PHP 发送多个值?

发布于 2024-11-16 16:49:02 字数 1054 浏览 1 评论 0原文

我有两个单选按钮类别: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

临走之时 2024-11-23 16:49:02

您需要在执行请求时发送这两个变量(正如 yes123 的回答所示)。

或者,您可以在 PHP 中使用 $_SESSION 来保留已选择和未选择内容的状态。

您可以在此处阅读会议内容
http://www.tizag.com/phpT/phpsessions.php

会话只是一个数组(我保持非常简单)。您可以在该数组中放入一些值,每次重新加载页面时,这些值都会保留 - 除非您关闭浏览器窗口。

在你的情况下,

session_start(); // Remember to have this on top of your php file before any echo or print or any other html is printed out.

if(isset($_GET['color1']){
  $_SESSION['color1'] = $_GET['color1'];
}

if(isset($_GET['color2']){
  $_SESSION['color2'] = $_GET['color2'];
}

echo $_SESSION['color1']; 
echo $_SESSION['color2'];

我还建议不要使用“红色”,“蓝色”等。

给每种颜色一个 id - 红色 = 1,蓝色 = 5 等。

然后你做 $_SESSION['color2'] = intval($ _GET['color2']); 这意味着您将颜色存储为数字,很容易使用 intval() 进行清理。当您需要显示已选择的颜色时,您只需编写一个方法,该方法接受一个数字并返回一个带有颜色的字符串。 (简单的 switch 语句)。这将阻止人们向你的 html 注入一些垃圾。

function get_colour($i){
  switch(intval($i)){
    case 1:
      $colour = "blue";
      break;
    case 2:
      $colour = "red";
      break;
    default:
      $colour = "blue"; // default colour when people try to fiddle with injection
      break;
  }
  return $colour;
}

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

session_start(); // Remember to have this on top of your php file before any echo or print or any other html is printed out.

if(isset($_GET['color1']){
  $_SESSION['color1'] = $_GET['color1'];
}

if(isset($_GET['color2']){
  $_SESSION['color2'] = $_GET['color2'];
}

echo $_SESSION['color1']; 
echo $_SESSION['color2'];

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 with intval(). 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.

function get_colour($i){
  switch(intval($i)){
    case 1:
      $colour = "blue";
      break;
    case 2:
      $colour = "red";
      break;
    default:
      $colour = "blue"; // default colour when people try to fiddle with injection
      break;
  }
  return $colour;
}
狼性发作 2024-11-23 16:49:02
onchange=" window.location.href='recom.php?color1='+this.value+'
           &color2='+document.getElement[..]
         "

还可以考虑使用 jQuery

onchange=" window.location.href='recom.php?color1='+this.value+'
           &color2='+document.getElement[..]
         "

Also consider to use jQuery

高跟鞋的旋律 2024-11-23 16:49:02

您可以将两个单选按钮放入表单中,并在两个 onchange 事件中,只需提交表单即可。

you can put both radio buttons in a form, and in both onchange events, just submit the form.

如歌彻婉言 2024-11-23 16:49:02

只需使用一个类...

<input type="radio" name="color1" value="black"  class="myElementClass"> Black <br />
<input type="radio" name="color2" value="white"  class="myElementClass"> White <br />


Jquery {


    $(.myElementClass).change(function() {

    //do whatever i want
    });

}

Just use a class ...

<input type="radio" name="color1" value="black"  class="myElementClass"> Black <br />
<input type="radio" name="color2" value="white"  class="myElementClass"> White <br />


Jquery {


    $(.myElementClass).change(function() {

    //do whatever i want
    });

}
想你的星星会说话 2024-11-23 16:49:02

尝试这样的事情:

select first color:<br/>
<input type="radio" name="color1" value="black" onchange="window.location.href='<?php if(isset($_GET['color2'] echo"\'recom.php?color1=\'+this.value+\'&color2=".$_GET['color2'];else echo"\'recom.php\'+this.value";?>> black<br />

try something like this:

select first color:<br/>
<input type="radio" name="color1" value="black" onchange="window.location.href='<?php if(isset($_GET['color2'] echo"\'recom.php?color1=\'+this.value+\'&color2=".$_GET['color2'];else echo"\'recom.php\'+this.value";?>> black<br />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文