homepage Welcome to WebmasterWorld Guest from 54.161.236.92
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
select tag get value from caption
hozyali




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved