tags:

views:

606

answers:

1

Hi,

I had created a input fields in the View Page like

    <div class="input text">
      <label for="2">First Name (required) </label>
      <input type="text" value="" style="width: 300px;" id="2" class="required" name="First Name"/>
     </div>

  <div class="input text">
   <label for="5">Emailid</label>
   <input type="text" value="" style="width: 300px;" id="5" name="Emailid"/><
 /div>
<div class="input text">
<label for="6">Mobile Number</label>
<input type="text" value="" style="width: 30px;" id="6" name="Mobile Number"/>
<input type="text" value="" style="width: 30px;" id="6-1" name="Mobile Number"/>
<input type="text" value="" style="width: 70px;" id="6-2" name="Mobile Number"/>
</div>

these are all generated in the Page .I am retrieving the content values form this page in my CakePHp controller using

 foreach ($_POST as $key => $value):
    echo $key;echo $value;
 endforeach;

For all the above fields i am getting the correct answer like First_Name=A&[email protected]&Mobile_Number=3

But for my Mobile Number field alone i am getting the value of the last id like 6-2.Why so .? HOw to retrieve the full value of the mobile number which is given in the three fields (6,6-1,6-2) ??Please suggest me.

Edit :

     foreach ($_POST as $key => $value):
     $mobile_number = implode('', $_POST['number']); 
   echo $mobile_number;
    $this->data['Result']['form_id']=$formid; 
    $this->data['Result']['label']=Inflector::humanize($key); 
     $this->data['Result']['value']=$value; 
   $this->Form->submitForm($this->data);
    endforeach;

i am saving the key and its values using like above (ie.., The controller doesn't know what are all fields are in the Fill Page). The fillpage may/maynot contain the field Phone Number.How do i assure that the page has Phonenumber field and to save its value with its Key.

Note: $key is the Fieldname and $value is my value of the field . Only for Phonenumber Field the values are array and in all other cases it is only a single value.

+2  A: 

If you're using Cake, you really ought to be learning and using Cake.

Just a quick run-down of the flow in Cake:

Form page -> Submit
    ^           |
    |           v
    |        Controller // Form data is in $this->data
    |           |
    |           v
    |        $this->Model->save($this->data); -> Model
    |                                              |
    |                                              v
    -------- Controller <----------------------- Data validation
                             validation no good    |
                                                   | validation okay
                                                   v
             Controller <----------------------- Actually save data
                 |
                 v
          Go somewhere else

What this means for you:

$this->data is central for Forms to work correctly. DO NOT TOUCH $_POST! Use the Cake methods for creating Forms and use the same names for fields as are used in the database. That facilitates a lot of the Cake "automagic". You don't hand-assemble an array to pass to the database, you use the same names for everything from start to finish and just pass the array around. $this->data will automatically be passed back from the Controller to the View (i.e. the Form), if you use the Cake FormHelper the fields will automatically be filled back in from it.

You should minimize the work you have to do between submitting the form and saving the data. Ideally, your controller only looks like this:

if (!empty($this->data)) {
    if ($this->Model->save($this->data)) {
        $this->redirect(array('action' => 'next_step'));
    }
}

That's all the code you need, and it will

  • display the page
  • handle form submissions
  • validate the input
  • fill the form fields back in if validation failed
  • save the data if validation was successful
  • redirect to the next step if the data was saved successfully

If your form fields don't correspond 1:1 with your database fields, tweak only the bits you need to between if(!empty($this->data)) and $this->Model->save($this->data):

if (!empty($this->data)) {
    $this->data['Model']['mobile_number'] = implode('-', $this->data['Model']['mobile_number']);
    if ($this->Model->save($this->data)) {
        $this->redirect(array('action' => 'next_step'));
    }
}

If you want to validate the submitted form data for completeness, you only fill in the appropriate rules in the Model. Whenever you issue a $this->Model->save(), the model will automatically check the rules before saving and return false if they're not met.

deceze
Actually i am doing the same thing in my other functions .But in this submit action , the controller wont know what may be the fieldname like what u said that mobile_number.Only for that i am using $_POST like foreach ($_POST as $key => $value):if(is_array($value)){$value = implode('', $_POST[$key]);$this->data['Result']['value']=$value;}else{$this->data['Result']['value']=$value;}$this->data['Result']['form_id']=$formid;$this->data['Result']['label']=Inflector::humanize($key);$this->Form->submitForm($this->data);endforeach;
Mercy
**DON'T USE $_POST!** Yes, you can, since there's still PHP in CakePHP, but then you're really not using Cake. Either do it the Cake way or don't use Cake at all. If you'd do one _or_ the other you probably wouldn't have this problem.Also, what does `$this->Form->submitForm($this->data)` do?
deceze
$this->Form->submitForm($this->data) sending all my controllers settings to the Model which saves the entries in the database.
Mercy
I'd check if that could be replaced with a simple `$this->Model->save($this->data)`. If absolutely necessary, create a `beforeSave()` hook in the Model. Either way, I'd definitely rename that function. The form is already submitted, this function name is awkwardly misleading.
deceze