Welcome to WebmasterWorld Guest from 54.145.39.186

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

select tag get value from caption

     

hozyali

7:31 pm on May 12, 2012 (gmt 0)

5+ Year Member



Hi,

I need some help with something unusual.

The select tag I have has different value under value attribute and a caption for each option.

something like this
<option value"123">USA</option>

Now, this value "123" is being used for another purpose in my code and it is required to be there. But at the same time, I need to send the value of USA also to my submitted php code.

Is there a way to do that?

please help.

thanks

rainborick

3:40 pm on May 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only way I can think of to do this in pure PHP would be to encode the two pieces of data into a single value attribute with something like:

<option value="123:USA">USA</option>

and then explode() the two values in the script that processes the form data.

The other option would be to use JavaScript to populate a hidden <input> tag using onsubmit().

hozyali

3:58 pm on May 13, 2012 (gmt 0)

5+ Year Member



Okay. but in this case, I have another problem.

the value 123 is being supplied to a javascript function which actually uses that value to calculate the total cost of the cart.

so the question is if we do the way you suggest, how can we explode the value in javascript and use the only first part to make the calculations and send the 2nd part in a hidden field so we can use that value on next php page.

any idea?

thanks

cffrost2

12:14 am on May 14, 2012 (gmt 0)

5+ Year Member



I'm thinking Javascript split function. I believe this would be something like what you want. Split uses a separator and a limit value. So string.split(":") would split the string by the ":".

<script type="text/javascript">
function something()
{
var string = document.getElementById('select').value;
var explode = string.split(":");
var firstChunk = explode[0];//this is the "123" chunk
var secondChunk = explode[1];//this is the "USA" chunk
}
</script>


Hope this helps.

rocknbil

4:12 pm on May 14, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Now, this value "123" is being used for another purpose in my code and it is required to be there. But at the same time, I need to send the value of USA also to my submitted php code.


Instead of wrangling with a bunch of strings and changing all your validation methods, I'd just grab the value "from wherever it came." That is, if you have a country table that generated this list (which you should) just get the country input and look it up in that table. You should have a function somewhere that does this as it's one of those things you'll use a lot.

list($country_abbrev,$country) = get_country_name($_POST['country']);
if (! $country_abbrev) { error_function('Invalid country name'); } // you'd write this error function of course

function get_country_name($id) {
if (! is_numeric($id) or (is_numeric($id) and ! ($id > 0))) { return null; }

$r=array(null,null);

$q = "select short_title,title from country where id=$id";
$res = mysql_query($q) or die("cannot query country list for name");
$r = mysql_fetch_array($res);
return array($r[0],$r[1]);
}

hozyali

5:32 pm on May 14, 2012 (gmt 0)

5+ Year Member



hi, thanks for the help,

actually it is not a countries table. as I mentioned. the real value is being calculated in total amount for shipping. and the caption selected should go to the database so we know in order details that what shipping method client selected.

thanks

rocknbil

4:26 pm on May 15, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



So where does the country list come from then? An array? Hard coded?

If you have a hard coded list, you're going to make life a lot easier if you use and array or a table (better!) You'll have use for this list in multiple places.

If it's an array, instead of the get country name function you could do this, assuming this array structure:

$country_array = (
'123' => 'USA'
// etc.
);

$c = (isset($_POST['country']) and (is_numeric($_POST['country']) and ($_POST['country']>0)))?$_POST['country']:null;

if (! $c) { die("Invalid country selection"); }

$countryname = $country_array[$c];


Or you could just work around it and concatenate the option values as suggested, make your forms Javascript dependent, and a year from now wish you'd compartmentalized it. :-)

hozyali

4:48 pm on May 15, 2012 (gmt 0)

5+ Year Member



thanks for the reply. as I mentioned earlier, it is not a countries list, it is used for shipping values.

basically I have a delivery methods drop down in my cart.
it has values=2.99 and the label is Fedex. so if I select Fedex, it will send 2.99 value via jscript to the cart total. so I just need that Fedex label also in a hidden field so I can save it in the database for future use.

any idea?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month