Jump to content

How to pass 2 values in an HTML element


TechnoDiver

Recommended Posts

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 by TechnoDiver
Link to comment
Share on other sites

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.   

  • Like 1
Link to comment
Share on other sites

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 ^^^^^^^^

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by requinix
browse*r*
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.