(12-30-2014, 01:44 PM)jonathantkids Wrote: So right now the button submits to the database. So if I am still confused on how to implement a second button allow the user to submit another product with similar properties. Or in other words after submitting the first product be brought back to the form with the fields repopulated from the first entry.
You don't need a second button. You use the same form with the same button. Let me see if I can walk you through the code I posted.
PHP Code:
public function add_product()
{
$message = 'Enter product and click Submit button';
$product = array(
'name' => '',
'price' => 0,
'description' => ''
);
// $this->form_validation->set_rules goes here
if ($this->form_validation->run())
{
$product = $this->input->post('product');
$data = array (
'name' => $product['name'],
'price' => $product['price'],
'description' => $product['description']
);
$this->products_model->add($data);
$message = 'Success! Enter another if you wish';
}
else // form validation failed OR first time through
{
if (validation_errors())
{
$message = validation_errors();
$product = $this->input->post('product');
}
}
$data = array (
'message' => $message,
'product' => $product
);
$this->load->view('add_product_view', $data);
}
Say it's the first time through the function. No products entered so far. The code sets $message to "Enter product and click Submit." The code also sets up a $product array with blanks/zeroes. Then the code sets up the form validation rules.
But the first time through, form validation will fail because nothing has even been submitted, so the code falls down to the else clause. Now, the code doesn't know if validation failed because nothing was submitted (first time through) or because there really was a validation error, so the code checkes the validation_errors function. If there is even one validation error, the if statement will be true, and the code will then load $message with the error messages and load $product with whatever the user entered in the form. It does this so it can show the user what he or she entered.
Then, the $data array gets loaded with whatever is in $message and $product, and the code then loads the view, sending it the $data array. The view then shows whatever is in $message as well as using the values in the $product array to populate the form fields with set_values().
After the users submits the form, it's a similar process, except this time, either validation will succeed or it will fail because of errors (either way, something was posted). If validation succeeded, the code loads the $data array with the values to save in the database and calls the add function in the model file.
Notice that it does not redirect anywhere! Instead, having added the row to the database, the code falls through to the last few lines, where the $data array is loaded with $message and $product and
the same view is called.
As long as you keep pressing the Submit button, the code will keep adding another row to the database. In fact, you'll need some code to prevent entering two identical products. You can restrict that at the database, or you can write code that holds the last entry and compares it to the next.
Now, I hope I understood your question correctly. You want your user to fill out a form, press Submit, and after the code adds the row to the database, you want your user to once again see the same form, but this time with the information he or she entered before, so that if the next product is basically the same, except size medium instead of size small, and $3.49 instead of $2.25, your user won't have to fill out the entire form again. Did I get that right?
It also occurred to me that you may have to use the second argument of set_values:
PHP Code:
value="<?= set_value('product[name]', $product['name']) ?>"
Without the dollar sign, product[name] is just the name of the field. Usually that's all you need because Codeigniter automatically saves the values of the fields (it just doesn't set those values automatically). But I believe Codeigniter clears the field values when form validation succeeds, so to populate the form, you'll need the data in $product['name'].