Jump to content

question about values within INPUT tags?


galvin

Recommended Posts

I am building an NCAA tourney bracket application where people click a team and it submits their pick into a database.  In my mysql database that stores all team names, there is a teamname (like "Kansas") and a teamid (like "1").

 

I have a form with an "input" tag like below....

 

<input type=text readonly class="team" name="pick0_1" onclick="win(this)" value="Kansas">

 

Since the value is "Kansas", then "Kansas" is what will be inserted into MySQL. I would much prefer to insert the teamid (i.e. "1") instead.

 

So, in that "input" tag, is there any way to store the teamid as the true value so to speak (since that is what I want to insert into MqSQL via Post) but also have the teamname be what is displayed to the usre?

 

In other words, I want the user to see "Kansas" but if they submit this, I want "1" to go into the database as $_POST['pick0-1'], NOT "Kansas".

 

Is there a way to do this with "input" tags, or will I have to use an anchor tag (since that stores a value behind the scenes and can display to the user as whatever I want)?

 

-Greg

Link to comment
Share on other sites

Before getting to how to deal with your problem, you have a fairly large logic issue going on with your method of taking input and storing values. If you want to store values as IDs, for example, kansas = 1, Washington = 2, Colorado = 3 etc, that's entirely fine (and actually will streamline the actual database a bit. But if you are going to store values by ID, then you should be storing ALL the values by ID, not just some of them. In this case, you can use a select menu:

<select name="state">
  <option value="1">Kansas</option>
  <option value="2">Washington</option>
  <option value="3">Colorado</option>
</select>

That way the value is always selected properly, and you don't have to worry about spelling mistakes or whatnot.

 

The way you are doing it now, you are accepting text values and wanting to convert that to an ID. That just leaves open the possibility of spelling mistakes and unintended values.

 

But, if you really must do it the way you want, you can simply convert the value in the form processing:

 

if($_GET['pick0_1'] == 'Kansas')

{

  $value = 1;

}

else

{

$value = $_GET['pick0_1'];

}

 

You now have a variable $value with a value of either 1, or whatever was entered into the input that didn't say 'Kansas'.

Link to comment
Share on other sites

Thanks Haku.  Just so you know, all the input fields are all read-only so users aren't able to type or change the text that is showing.  They are simply clicking a team name and that causes the exact same team name to appear in the field in the next "round".

 

With that said, I'm pretty sure your suggestion will allow me to do what I want (i.e. store IDs in the database rather than team names). 

 

I'll try it out, thanks again!  :)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.