开发者

how to send multiple values to PHP using "onchange="?

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


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;
}


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

Also consider to use jQuery


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


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
    });

}


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 />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜