0

How can I get the parameter in the URL and save it to a varialble so I can save it to my database?

example: www.mydomain.com/item/products/3 <-

This is for my upload image, so I can specify what product ID will I use for that image.

function add() {
        if ($this->request->is('post')) {
            $this->Upload->create();

                if(empty($this->data['Upload']['image']['name'])) {
                    unset($this->request->data['Upload']['image']);
                }

                if(!empty($this->data['Upload']['image']['name'])) {
                    $filename = $this->request->data['Upload']['image']['name'];
                    $new_filename = STring::uuid().'-'.$filename;
                    $file_tmp_name = $this->request->data['Upload']['image']['tmp_name'];
                    $dir = WWW_ROOT.'img'.DS.'uploads';

                    move_uploaded_file($file_tmp_name,$dir.DS.$new_filename);

                    $this->request->data['Upload']['image'] = $new_filename;
                    if($this->Upload->save($this->request->data)) {
                        $this->Session->setFlash(__('Uploaded.'));
                        $this->redirect(array('action' => 'index'));
                    }
                }
        }
    }

How will I add it here. Thank you in advance :) Im using cakePHP

2 Answers 2

1

Just add the product id as a hidden input in the Form. Then it will be included in the $this->data variable when you get the POST request.

Sign up to request clarification or add additional context in comments.

7 Comments

how can I put the product id to the hidden field? Sorry newbie :|
What if I want the id goes to other view? example: www.mydomain.com/item/products/view/3 -> www.mydomain.com/item/products/add/3.. That is the flow of my desire website..
Ok, If you are in the product page. You have a form to upload the image right? Then in that form just insert a hidden field. That field will be present in the data of the post. URL parameters are received via GET requests, forms do a POST request.
My upload image is in the "add" then before you can go there, you go first in the "view" so I can specify where the image will be putted. How will I pass the "id" to the hidden fields in the "add" ?
|
0

For example in a controller method like the following

public function product($id) {
  .....
}

You access it by the url (for example): www.mydomain.com/item/products/3 where Item is the controller, product is the method you are calling in that controller and 3 represent a parameter that is required to the function to work in this case $id. (assuming you don't have any routing configuration)

Is treated as a normal php variable, just do whatever you wanna do with it. Just make sure you pass the correct value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.