Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

CakePHP 2.0  Saving form data help

Posted on 2012-04-01
1
1,092 Views
Last Modified: 2012-06-21
Hi,  Just in the beginning stages of learning CakePHP, and would like help with saving data from a form to the database.  Basically i have about twenty forms which all are very similar, so solving this would also help me with the other forms.

What code do i put in my Models Views and Controllers, what files to create.

I have a Name texfield and Notes textarea  and a save button.

Entering a Name, and some Notes and clicking save, i want to save to a database table called Risks.

The Risks table is
-----------------------
id - INT
name - VARCHAR (255)
notes - VARCHAR (255)
created_at - DATETIME
updated_at - DATETIME

The form i have has this markup :
<form class="new-risk">
  <fieldset>
    
    <div class="control-group">
      <label class="control-label" for="input01">Name</label>
      <div class="controls">
        <input type="text" class="input-xlarge" id="input01">
      </div><!-- End Controls -->
    </div><!-- End Control Group -->
  	
  	<div class="control-group">
      <label class="control-label" for="textarea">Notes</label>
        <div class="controls">
          <textarea class="input-xlarge" id="textarea" rows="3"></textarea>
       	</div><!-- End Controls -->
   	</div><!-- End control-group -->
  
  	<div class="form-actions">
    	<button type="submit" class="btn btn-primary">Save</button>
    </div><!-- End form-actions -->
  
  
  </fieldset>
</form>

Open in new window


Solving this, would then help me with all my other forms.
0
Comment
Question by:jonathan1978
1 Comment
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 37795856
Hi,

assuming you have looked at this page: http://book.cakephp.org/2.0/en/models/saving-your-data.html

and this for creating the form:

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

so in your case your form needs to return an array like:

Array
(
    [ModelName] => Array  (your model name here)
    (
        [id]=>'a id'
        [name] => 'value'
        [notes] => 'value'
    )
)

Open in new window


cake will deal with the updated and created fields for you, in 2.x you dont need a model unless you have any special requirements.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question