TechnoDiver Posted September 18, 2021 Share Posted September 18, 2021 (edited) Maybe not 2 values in one element but something similar. I have the following method -> <?php public function selectCat($type) { $table = "categories"; $field = "type"; $rule = "ORDER BY id ASC"; $query = $this->_db->get($table, array($field, "=", $type), $rule ); $this->_data = $query->all(); $str = ""; foreach($this->_data as $obj) { $cat_id = $obj->id; $category = $obj->category; $cat_title = ucwords($category); $str .= " <option value='$category'>$cat_title</option> "; } return $str; } It's obviously to construct a dynamic select menu. It's called in the html of a page and then $category is passed through $_POST. But I need to pass both $category and $cat_id to $_POST. How would I go about passing both $category and $cat_id through post when the corresponding $cat_title is selected? I feel like an answer is right on the tip of my brain but I just haven't arrived there yet. Any suggestions are really welcome. Thanks EDIT: I've been thinking about having the method return an array of the data and then looping through it in the html to construct the select dropdown but looking ahead, I'm still not sure how a $cat_id would be assigned when the corresponding $cat_title is selected Edited September 18, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/ Share on other sites More sharing options...
Barand Posted September 18, 2021 Share Posted September 18, 2021 20 minutes ago, TechnoDiver said: But I need to pass both $category and $cat_id to $_POST. Why? What's the difference between your category and cat_title? Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590100 Share on other sites More sharing options...
TechnoDiver Posted September 18, 2021 Author Share Posted September 18, 2021 11 minutes ago, Barand said: What's the difference between your category and cat_title? There's no difference it's the $category and $cat_id I need to pass together Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590101 Share on other sites More sharing options...
gizmola Posted September 18, 2021 Share Posted September 18, 2021 Just going forward, while these type of all in one functions with data & presentation are enticing (and I certainly wrote many of them back in my early days of web development) it's really an anti-pattern that you should avoid. Have a function that returns the data in a format, ideally json, but at very least in a clean php data like an array. Why json? Because it will be easier for you to wire it into a javascript based UI. In the meantime for server side coding, have a separate view script or template. You might organize these by putting them into a subdir named /views or /templates. You could write a simple render($view, $data) function that takes the parameters and require_once() your $view. At that point, your view templates can just concentrate on html, and using alternative php syntax, handle whatever looping you need. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590102 Share on other sites More sharing options...
Barand Posted September 18, 2021 Share Posted September 18, 2021 If you know the cat_id, can't you then determine the category? All you should need to pass is the cat_id, which should be the value in your options. Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590103 Share on other sites More sharing options...
gizmola Posted September 18, 2021 Share Posted September 18, 2021 Also to answer your question, a common technique is to use a hidden form element, if you need to pass an extra attribute that isn't part of the 2 you have in the drop down. You would need a bit of javascript to set it onChange. <input id= "catId" type="hidden"> With that said, you should probably use the cat_id rather than the category to index the select. *edit* See Barands comment ^^^^^^^^ Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590104 Share on other sites More sharing options...
TechnoDiver Posted September 19, 2021 Author Share Posted September 19, 2021 12 hours ago, Barand said: If you know the cat_id, can't you then determine the category? All you should need to pass is the cat_id, which should be the value in your options. Yea, sure, it just takes another simple method. But the method I posted here already has both pieces of information - $category and $id. Is there not a way to pass them both on when they're called? For example, the method that I post in my OP is obviously part of a form to be processed. Processing it requires both the category name and its id. It seems like I would have to make another quick method to determine the category name from the id if the <select> element only holds the id; but my posted method already has both pieces of information, it seems redundant to me to make another. Or am I being a complete muggle and missing something? Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590117 Share on other sites More sharing options...
requinix Posted September 19, 2021 Share Posted September 19, 2021 (edited) 10 hours ago, TechnoDiver said: Or am I being a complete muggle and missing something? So you'd be cool with me going into the page (which is in my browser so I can do whatever the hell I want) and change the value of the name only to be anything I wanted? Let's try another example. You're making a checkout page for some online shopping thing. You put into the form the product ID and quantity, because obviously you need to know that. You also tell me the product price, because obviously I need to know that. Do you put the product price into the form as well? Edited September 19, 2021 by requinix browse*r* Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590118 Share on other sites More sharing options...
TechnoDiver Posted September 19, 2021 Author Share Posted September 19, 2021 18 minutes ago, requinix said: So you'd be cool with me going into the page (which is in my browse so I can do whatever the hell I want) and change the value of the name only to be anything I wanted? I'm not sure what you mean here. In my example the user can't change any information. There's a table in my DB of only the categories. Since a <select> dropdown menu with the categories appears more than once in the app I made the selectCat method to create it. This method already collects the cat. id along with it's corresponding category. I'm trying to keep each id:category connection so when an option is chosen I can $_POST both to another method that parses and displays it with other information. It's not that big of a deal to write a quick method that matches items (categories, posts, users etc) with their id's but it seems redundant to do so when each is already paired with it's respective id in the selectCat() method. I'm trying to keep the id:category connection with the <select> element to pass both to $_POST. I've been going through some mental fatigue lately so I realize I could be being a bit slow but I don't quite understand your analogy Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590125 Share on other sites More sharing options...
TechnoDiver Posted September 19, 2021 Author Share Posted September 19, 2021 30 minutes ago, requinix said: Let's try another example. You're making a checkout page for some online shopping thing. You put into the form the product ID and quantity, because obviously you need to know that. You also tell me the product price, because obviously I need to know that. Do you put the product price into the form as well? It seems you'd get the price via the id in the form and not actually type the price in. Which I feel is what I'm trying to do - I've already got both pieces of information that I need, I'm trying to avoid another DB query method. Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590126 Share on other sites More sharing options...
requinix Posted September 19, 2021 Share Posted September 19, 2021 9 hours ago, TechnoDiver said: I'm not sure what you mean here. In my example the user can't change any information. There's a table in my DB of only the categories. Since a <select> dropdown menu with the categories appears more than once in the app I made the selectCat method to create it. This method already collects the cat. id along with it's corresponding category. I'm trying to keep each id:category connection so when an option is chosen I can $_POST both to another method that parses and displays it with other information. Once you send that information to my browser, I can screw around with it however I want. It may have started off as data from your database, but it's not in your database anymore. It's in an HTML form. 9 hours ago, TechnoDiver said: It's not that big of a deal to write a quick method that matches items (categories, posts, users etc) with their id's but it seems redundant to do so when each is already paired with it's respective id in the selectCat() method. I'm trying to keep the id:category connection with the <select> element to pass both to $_POST. What's redundant is the fact that your receiving page is going to have to look up the ID in the database anyways, which is the same place the category name is being stored. You are looking up the ID in the database, right? How else would you know that the ID you're receiving in the form is valid? It sounds like you might not be doing that. 9 hours ago, TechnoDiver said: It seems you'd get the price via the id in the form and not actually type the price in. Exactly: the price is not something that should be in the form, and when you want it, you look up the price in your database according to the ID. Because putting it into the form means someone could go change the value however they see fit. 9 hours ago, TechnoDiver said: Which I feel is what I'm trying to do - I've already got both pieces of information that I need, I'm trying to avoid another DB query method. Actually you're trying to do the opposite: put something in the form and then not look it up in the database later. Quote Link to comment https://forums.phpfreaks.com/topic/313770-how-to-pass-2-values-in-an-html-element/#findComment-1590149 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.