0

I have data in a select tag that is POSTed back to my script. I have the key in the POST array but need the corresponding value. How do I get this without querying the database for it?

<option value="1">My Value</option>
Jim
  • 3
  • 2

5 Answers5

2

Don't you have it on the server side already? In the database or whatever storage? Sure you do.
So, get it from there.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I have it in the database but when the user submits the form, I'll only have the key. You're saying that I need to query the database a second time for its value? – Jim Feb 14 '11 at 13:31
  • Sure. You will do that anyway, don't you? – Your Common Sense Feb 14 '11 at 13:35
  • @Jim: yep, the DB has probably cached that record anyway, so it's not going to be a big overhead. – Spudley Feb 14 '11 at 13:38
  • Yes, but I've done it once already to populate the select. So I have to query the database a second tome for its value? – Jim Feb 14 '11 at 13:38
  • @Jim it's just an atomic primary key lookup. Trust me, not this kind of query would make your application slow. – Your Common Sense Feb 14 '11 at 13:40
  • @ Col. Shrapnel. OK, it just seems really strange to have to so it like this is all. I'm not that worried about resources. – Jim Feb 14 '11 at 13:45
  • @Jim nothing strange if you grasp the idea how PHP works. It's always another program to run, knows nothing of what happened in the previous instances. Remember no states, variables, resources etc. So, if you need some data back, you have to read it again. Not a big deal though – Your Common Sense Feb 14 '11 at 13:50
  • @Col, It just that it make me crazy knowing that I just queried the database and got both the key/value pairs to build the option tag with and then have to re-query the database a second time for something I already had... It's almost unbelievable that will all of the built in functions that there wouldn't be one to intersect the keys so I can get the value back out. – Jim Feb 14 '11 at 13:54
  • 1
    @Jim you're thinking of the database as of something separate, something where you have to "go". But it isn't like that. It's just a part of your application, like any other. Say, a web-server have to serve the same page second time. And PHP have to parse and run the same code. It's just the way these things works. It's like a session suggested in another answer: it doesn't really matter where to go for your data. A database is no worse than anything else – Your Common Sense Feb 14 '11 at 13:59
  • @Col. I know, you're right... Sometimes, I get to splittin' hairs and wind up like a cat chasing his tail looking for the "best" way. You're right though and I've credited you for the answer. – Jim Feb 14 '11 at 14:02
1

When you post the form, the receiving program will receive the contents of the value attribute for each field. Therefore, in the example you quoted, you will receive 1.

If you actually want to receive My Value (or at least have it readily to hand), then there's a number of options open to you:

  1. Include it in the value attribute, so it gets posted. eg <option value='1:My Value'>. Then you can split the numeric value from the text value in the receiving program. This would work quite well, though the down side of this is that you're now adding extra overhead to the form post (admittedly not a huge amount, though).

  2. Store the options array in the session. Then the whole array is accessible at any time without re-loading from the DB. The down side of this is that you'll end up cluttering up your session with loads of arbitrary data that doesn't need to be there.

  3. Just get it from the DB and stop worrying; if you're using a decent DB, it'll have cached the value anyway from your previous page load when you populated the drop-box, so it isn't exactly a big overhead. Realistically, unless you've got a really big performance issue, I'd suggest this is the way to go.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

I would suggest you go about this in a reverse manner. That is create the list in the back end and call it to its HTML counterpart. That way you would have both the values and its corresponding key.

Alternatively, you could, if your list is rather small (otherwise it will get a bit messy) simply check what the value is and assign its corresponding key in your logic, manually.


Edit:

I noticed you mentioned that you already have both the values. Whats stopping you from matching the key with its value?

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
  • This is what I want to do. What function can I use for this? – Jim Feb 14 '11 at 13:35
  • @Jim: Try retrieving it from the database. I doubt it will be as resource intensive as you make it out to be. Please do not try and calculate how long you *assume* it might take. It does not hurt to try it :) – Russell Dias Feb 14 '11 at 13:38
0

One way to do this, first one an array, 2nd an object:

<select name="">
    <option value="[0,1,2,3]">Option one</option>
    <option value="{foo:'bar',one:'two'}">Option two</option>
</select>

Can an Option in a Select tag carry multiple values?

Community
  • 1
  • 1
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
-1

You can't - an alternative, if you wan't to avoid contacting the database, would be something like:

<option value="1|My Value">My Value</option>

And in your recieving script:

$values = explode("|",  $_POST["yourselect"]);
var_dump($values);
Repox
  • 15,015
  • 8
  • 54
  • 79
  • Isn't there a built in function that I can use to map the key to its value? I'm having a hard time believing that I'd have to query the db again – Jim Feb 14 '11 at 13:35
  • @Jim: Would be really be that resource intensive? If so, you might want to rethink the design of your search functionality... – Russell Dias Feb 14 '11 at 13:36
  • No Russell, it wouldn't but it seems really hackish to me – Jim Feb 14 '11 at 13:41