galvin Posted March 28, 2010 Share Posted March 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/196773-question-about-values-within-input-tags/ Share on other sites More sharing options...
haku Posted March 29, 2010 Share Posted March 29, 2010 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'. Quote Link to comment https://forums.phpfreaks.com/topic/196773-question-about-values-within-input-tags/#findComment-1033293 Share on other sites More sharing options...
galvin Posted March 29, 2010 Author Share Posted March 29, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/196773-question-about-values-within-input-tags/#findComment-1033433 Share on other sites More sharing options...
zeodragonzord Posted March 29, 2010 Share Posted March 29, 2010 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 All input fields can be changed, regardless if you set it to read-only or not, with enough magic... Quote Link to comment https://forums.phpfreaks.com/topic/196773-question-about-values-within-input-tags/#findComment-1033453 Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 You can also use buttons <button type="submit" name="state" value="1">Kansas</button> Quote Link to comment https://forums.phpfreaks.com/topic/196773-question-about-values-within-input-tags/#findComment-1033521 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.