0

i search a long time, but i dont find a solution for my problem. The case is, that i have a few buttons integrated in my view and clicking on them affected updating single input fields. The problem is that i have warnings, that variables are undefined in view. I understand why and how i suppress them, but i`m not sure, if this is a good solution. Is there a better way to solve this? What is best practice?

Here is my code from the view file:

<?php 
echo $this->Form->create('Excel', array('type' => 'file'));
echo $this->Form->file('File');
echo $this->Form->submit('Upload xls File');
echo $this->Form->end();

echo $this->Form->create('Config');
//echo $this->Form->input('Name');
echo $this->Form->input('vlanNumber');
echo $this->Form->input('description', array('value' => $description));

echo $this->Form->input('preSharedKey', array('value' => $preSharedKey));
echo $this->Form->button('generate', array('name'=>'generateButton'));

echo $this->Form->input('customerPeerIp', array('default' => 'id_of_default_val','value' => $cusPeerIp));

The generate button affect a new preSharedKey. And the upload of the csv affected an update of the other fields. The relevant code of the controller is this:

    public function inputData() {


    if ($this->request->is('post')) {
        $post_data = $this->request->data;
        if (isset($this->request->data['show'])) {  //Submit Button was clicked
            $this->Session->write('Configuration',$post_data); //Store the input fields in the session
            return $this->redirect(array('action' => 'showPreview'));
        } else if (isset($this->request->data['cancel'])) { // Cancel button was clicked: Go back to index site 
            return $this->redirect(array('action' => 'index')); 
        } else if (isset($this->request->data['generateButton'])) {
            return $this->set('preSharedKey', $this->getRandomString(20)); //Set a Pre Shared Key with 30 signs
        }
        if (!empty($this->data) && is_uploaded_file($this->data['Excel']['File']['tmp_name'])) {
            $this->importData($this->data['Excel']['File']['tmp_name']);
            $excel=new Excel();
            $values=$excel->getParams($this->data['Excel']['File']['tmp_name']);
            $this->set('description',$values['description']);
            $this->set('cusPeerIp',$values['cust_peer']);
            return;
              //this calls the Excel Class function

        }
        //print_r($post_data);
        //echo $post_data['Config']['Name'];
        //echo $this->request['Config']['task_1'];
    }
    $this->set('description','');
    $this->set('cusPeerIp','');
    $this->set('preSharedKey', '');
}

Can you please help me?

arilia
  • 9,373
  • 2
  • 20
  • 44
  • in what view you get the warnings? And about what variable? Why don't you just set the variables to null at the beginning the action? – arilia May 06 '14 at 09:56
  • Hello, i got the warnings in the inputData view. When i press the generate button, then cake displays a warning, that the variables 'description' and 'cust_peer' are undefined and when i press upload csv file, then i get the warning, that preSharedKey is undefined. where should i set the variables to null? because the problem is that on each post the complete view is updated and not updated fields with a reference to an variable displays an error. – user3607499 May 06 '14 at 10:42
  • instead of setting them at the bottom of the action put them at the beginning. Take the 3 last rows of your code and put them just after `public function inputData() {` so they will never be undefined – arilia May 06 '14 at 10:44
  • I have this before. There is one more Problem. When i upload the CSV file then the fields were updated. Then i press the generate button and the input data method is called an overwrite me the variables. The same thing in the other way. Maybe you have an other idea to solve this? How would you programm that? – user3607499 May 06 '14 at 10:51
  • The aim is too have two possibilities to input data. One way is manual and the other way is uploading and then maybe alter the uploaded data. And i need an function that can create me an key (I named it 'generate') and display it. At next step i take this parameter and work with them in a new view. Do you have better ideas? – user3607499 May 06 '14 at 10:56

0 Answers0