user3733831
user3733831

Reputation: 2926

Submit more than one value from a single selected option

I have a dropdown to display price ranges for searching.

HTML for dropdown:

<select name="price">
    <option>All (-)</option>
    <option>Rs.400 to Rs.1,000 (3)</option>
    <option>Rs.1,000 to Rs.2,000 (6)</option>
    <option>Rs.2,000 to Rs.4,000 (1)</option>
    <option>Rs.4,000+ (1)</option>
</select>

Using this option values, now I need to separate first and second price from user selection. Eg. If someone select Rs.400 to Rs.1,000 (3), then I need to get 400 and 1,000.

Can anybody tell me how can I do this in php. I tired it with php explode. but I could not figure this out correctly.

Upvotes: 0

Views: 114

Answers (2)

Jacob Tollette
Jacob Tollette

Reputation: 101

What about something like this:

HTML

<select name="price">
    <option value="all">All (-)</option>
    <option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
    <option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
    <option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
    <option value="Over">Rs.4,000+ (1)</option>
</select>

PHP

<?php
  //Get the data from the form
  $price = $_POST['price'];
  $prices = explode("|", $price);
?>

For example, if the user selects "Rs.400 to Rs.1,000", the value of $prices will be:

$price[0] = "400";
$price[1] = "1000";

I hope that helps. If it doesn't, I suppose I wasn't clear on what you were asking.

Upvotes: 4

davcs86
davcs86

Reputation: 3935

Inspired by a previous answer. I'd suggest.

HTML

<select name="price">
    <option value="|">All (-)</option>
    <option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
    <option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
    <option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
    <option value="4000|">Rs.4,000+ (1)</option>
</select> 

PHP

<?php

function get_numeric($val) { 
  if (is_numeric($val)) { 
    return $val + 0; 
  } 
  return false; 
} 

$price_selected = isset($_REQUEST['price']) && trim($_REQUEST['price'])!="" ? $_REQUEST['price'] : "|"; // sets a default value

list($lower, $upper) = array_map('get_numeric',explode('|', $price_selected, 2));

var_dump($lower); // false when there's no lower limit

var_dump($upper); // false when there's no upper limit

?>

Upvotes: 1

Related Questions