trying to create a form that has two submit buttons in cakephp.
task_1 submit button when clicked creates this in the address bar '/fields/add' and sends the information added to the database.
task_2 submit button when clicked creates this in the address bar '/fields/add/add' and sends the entered information to the database`.
task_1 is meant to allow users to keep creating new fields, where task 2 is meant to take them to a different page, at the moment we just chose a page at random.
here is the code for the add function in controller
function add(){
$this->set('title_for_layout', 'Please Enter Your Invoice Headings');
$this->set('stylesheet_used', 'style');
$this->set('image_used', 'eBOXLogo.jpg');
$this->Session->setFlash("Please create your required fields.");
if($this->request->is('post')){
$this->Field->create();
if ($this->Field->save($this->request->data))
{
if($this->params['form']['task_1'] == "task_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
}
if($this->params['form']['task_2'] == "task_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Invoices','action' => 'add'));
}
}
else
{
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
}
here is the code for the add view
<form enctype="multipart/form-data" method="post" action="add/">
Name: <input type ="text" name="name" /> <br />
Description: <input type ="text" name="description" /> <br />
Account ID: <input type ="number" name="accounts_id" /> <br />
<br />
<input type="submit" name="task_1" value="Continue adding fields" />
<input type="submit" name="task_2" value="Finish adding fields" />
</form>
echo $this->Form->create('Field');
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('account_id');//this would be the conventional fk fieldname
echo $this->Form->button('Continue adding fields', array('name' => 'type_1'));
echo $this->Form->button('Finish adding fields', array('name' => 'type_2'));
echo $this->Form->end();
Related
I am making a sub form in Zend with the following code:
class Admin_Form_StudentAdmission extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$personalDetailsForm = new Zend_Form_SubForm();
$personalDetailsForm->setIsArray(true);
$student_first_name = $this->CreateElement('text','first_name')
->setAttribs(array('placeholder'=>'First Name', 'mendatory'=>'true'))
->setRequired(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => 'Please enter First Name')),
array('stringLength',true,array(2, 10, 'messages'=> 'First Name should be 2 to 10 characters long.')),
))
->addFilter(new Zend_Filter_StringTrim())
->setDecorators(array( array('ViewHelper')
));
$personalDetailsForm->addElement($student_first_name);
$this->addSubForm($personalDetailsForm, 'student_personal_details');
}
}
And now I am rendering this form with below php code:
$personalDetailsForm = $this->form->getSubForm('student_personal_details');
echo $personalDetailsForm->first_name;
But this renders the element as
<input type="text" mendatory="true" placeholder="First Name" value="" id="first_name" name="first_name">
While I want this as below
<input type="text" mendatory="true" placeholder="First Name" value="" id="student_personal_details-first_name" name="student_personal_details[first_name]">
What I'm doing wrong here?
Just use zend MVC pattern, it will work like a charm,
result:
<input type="text" name="student_personal_details[first_name]" id="student_personal_details-first_name" value="" placeholder="First Name" mendatory="true">
Pass the the form as $personalDetailsForm from namecontroller.php action viewfromAction() to viewfrom.phtml like:
$this->view->form = $personalDetailsForm;
Then echo your from in the viewfrom.phtml simply like:
<?php echo $this->form ?>
Zend 1.12 MVC:
[http://framework.zend.com/manual/1.12/en/learning.quickstart.intro.html]
The problem looks like, you used a direct echo $personalDetailsForm->first_name; thats didn't generate the proper html form element tags.
Here is my form.
<form method="post" name="frm">
<label>Name*<input type="text" name="Name" value="<?php echo $r['Name'] ?>" onblur="if(this.value.length<3) alert('Name too short');" /></label>
<label>Username*<input type="text" name="UN" value="<?php echo $r['UN'] ?>" onblur="if(this.value.length<5) alert('Username too short');" /></label>
<label>Password*<input type="password" name="PW" onblur="validation()" /></label>
<label>Confirm Password*<input type="password" name="CM" onblur="validation()" /></label>
<?php } ?>
<input type="submit" name="Submit" value="<?php if($new) echo 'Register'; else echo 'Update'; ?>" />
</form>
Without writing separate onBlur events I am trying to get all these events into a function called validation() as I have done for password and confirm-password fields. Here is my validation function:
<script language="javascript">
function validation() {
var password = document.frm.PW.value;
var password2 = document.frm.CM.value;
if (password.length < 5) {
alert ("Password too short");
}
else if (password != password2) {
alert("password mismatch");
}
}
</script>
But with this code once I have filled password, and when I am about to start inputting for confirm-password field, it alerts the message "password mismatch". How to get rid of this? And for all the form tags if I am validating and using validation() function, then in each tag do I have to call onblur=validation()?
Don't have the password validation fired on the main password change - have it fired when you alter password2 or when you click submit. :)
If you insist on checking when you update password AND password 2, I would just add a check to see if password2 had been left blank:
else if(password !== password2 && password2 !== ""){
But doing ALL your checks on submit is a much cleaner solution (and as Rake says, it would be neater still to have a span that is updated instead of an alert).
I just started using CodeIgniter today, and was following the tutorial at the CI website.
When I got to the final step for creating a new news item, I ran into an issue with the form submit. I would expect the URL to be jonhopkins.net/news/create when I press the form's submit button, but it is going to http://jonhopkins.net/news/jonhopkins.net/news/create instead, which is clearly very wrong, and I have no idea why this is happening. Does anyone know how to fix this?
Everything else on my site (the Pages part of the tutorial) works perfectly. This is the only thing that is breaking. All of my code is exactly as it appears in the tutorial, except for a change to what happens when an item is successfully submitted to the database, which currently is never happening.
News Controller
public function create() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
} else {
//$success will contain the slug of the created item, or FALSE if database insertion failed
$success = $this->news_model->set_news();
if ($success) {
$this->_doView($success);
} else {
echo "Something went wrong while trying to submit the news item.";
}
}
}
public function view($slug) {
$this->_doView($slug);
}
private function _doView($slug) {
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item'])) {
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
Create View
<h2>Create a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
And the resulting HTML...
<form action="jonhopkins.net/news/create" method="post" accept-charset="utf-8">
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
News Model I don't think it's ever getting here, but just in case
public function set_news() {
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'),
'date' => date("Y-m-d H:i:s")
);
if ($this->db->insert('news', $data)) {
return $slug;
} else {
return FALSE;
}
}
Routes
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
The form_open('news/create') method uses the $CI->Config->site_url() method which, in turn, uses the base_url from your config file.
Make sure your $config['base_url'] is set to your domain (with trailing slash) in the application/config/config.php file.
$config['base_url'] = 'http://jonhopkins.net/';
I want to submit a form that have many checkboxes with different values, using firebug I am sure that the checkboxes values are send to the server, but when I print_r the values of the post variable, only the last value is printed and igoring the other values.
<input type="checkbox" name="fruits" value="apple" />
<input type="checkbox" name="fruits" value="orange" />
<input type="checkbox" name="fruits" value="banana" />
and in the server when I print the request parameters
$formData = $this->getRequest()->getPost();
print_r($formData);
Only the last option is printed even I choose all of them and firebug shows them all!
I solve it by making [] in the name of the checkboxex
<input type="checkbox" name="fruits[]" value="apple" />
<input type="checkbox" name="fruits[]" value="orange" />
<input type="checkbox" name="fruits[]" value="banana" />
This is ZF way of doing the same
$fruits = new Zend_Form_Element_MultiCheckbox('fruits', array(
'multiOptions' => array(
'apple' => 'Label for apple',
'orange' => 'I am good orange',
'banana' => 'I am banana'
);
));
echo $fruits;
I'm getting to grips with Zend_Form and can create a form to add/edit a single database record, but now I'm trying to create a form to edit multiple records on a single page. For example, if I had a table of sports teams, and another table of players assigned to them teams I would want to be able to click on a team on my site and have all the players listed as rows with inputs to edit their names, date of births etc., with one submit button at the bottom to save any changes.
One thing to note is that there are a variable number of records that could be edited on a page; it is not a set amount.
Any pointers/help would be very much appreciated.
Thanks.
I use the code below to delete multiple items from the database.
On the index page (list of my database records):
<form method="post" action="<?php echo $this->baseUrl().'/admin/pages/deleteMultiple'; ?>">
<td class="checkboxTable"><input name="remove[<?php echo $data[$row]->id; ?>]" type="checkbox" id="checkbox[]" value="<?php echo $data[$row]->id; ?>"/></td>
<input class="deleteMultipleButtonBottom" name="deleteMultiple" type="submit" id="deleteMultiple" value="<?php echo $this->translate('Delete selected'); ?>">
</form>
The user sees a confirmation page:
<form method="post">
<input type="hidden" name="removeId" value="<?php echo implode($_POST['remove'], ','); ?>" />
<input class="deleteYes" type="submit" name="deleteMultiple" value="Yes" />
<input class="deleteNo" type="submit" name="deleteMultiple" value="No" />
</form>
And my action looks like this:
if($this->getRequest()->isPost())
{
if($this->getRequest()->isPost('remove'))
{
$this->view->pages = $this->pagesService->GetPages($this->getRequest()->getPost('remove'));
if($this->getRequest()->getPost('deleteMultiple') == 'Yes')
{
$this->pagesService->DeleteMultiplePages($this->getRequest()->getPost('removeId'), $this->view->user->username, $this->getRequest()->getPost('countedItems'));
}
elseif($this->getRequest()->getPost('deleteMultiple') == 'No')
{
$this->_helper->flashMessenger(array('message' => $this->view->translate('The pages were <u>not</u> deleted'), 'status' => 'notice'));
$this->_helper->redirectToIndex();
}
}
}
And in my service page:
public function DeleteMultiplePages($id)
{
$this->pages->delete('id IN (' . $id . ')');
}
This approach should work for updating values.