Reputation: 131
<div class="control-group invoice-properties">
<label class="control-label"><?php echo lang('quote_type'); ?></label>
<div class="controls">
<input type="text" id="quote_type" class="input-small" value="<?php echo $quote->quote_type; ?>" style="margin: 0px;">
</div>
</div>
The above code is located in a form. When I load the form it retrieves the value from the database. So when I enter directly in the database a number it retrieves this. But when I save it always saves a zero. When I input 8 it makes a 0(zero).
Anyone any idea why this is. Thanks in advance.
Below is the code that does the saving/retrieving at least I think it does.
public function save() {
$this->load->model('quotes/mdl_quote_items');
$this->load->model('quotes/mdl_quotes');
$this->load->model('item_lookups/mdl_item_lookups');
$quote_id = $this->input->post('quote_id');
$this->mdl_quotes->set_id($quote_id);
if ($this->mdl_quotes->run_validation('validation_rules_save_quote')) {
$items = json_decode($this->input->post('items'));
foreach ($items as $item) {
if ($item->item_name) {
$item->item_quantity = standardize_amount($item->item_quantity);
$item->item_price = standardize_amount($item->item_price);
$item_id = ($item->item_id) ? : NULL;
$save_item_as_lookup = (isset($item->save_item_as_lookup)) ? $item->save_item_as_lookup : 0;
unset($item->item_id, $item->save_item_as_lookup);
$this->mdl_quote_items->save($quote_id, $item_id, $item);
if ($save_item_as_lookup) {
$db_array = array(
'item_name' => $item->item_name,
'item_description' => $item->item_description,
'item_price' => $item->item_price
);
$this->mdl_item_lookups->save(NULL, $db_array);
}
}
}
$db_array = array(
'quote_number' => $this->input->post('quote_number'),
'quote_type' => $this->input->post('quote_type'),
'quote_date_created' => date_to_mysql($this->input->post('quote_date_created')),
'quote_date_expires' => date_to_mysql($this->input->post('quote_date_expires')),
'quote_status_id' => $this->input->post('quote_status_id')
);
$this->mdl_quotes->save($quote_id, $db_array);
$response = array(
'success' => 1
);
} else {
$this->load->helper('json_error');
$response = array(
'success' => 0,
'validation_errors' => json_errors()
);
}
if ($this->input->post('custom')) {
$db_array = array();
foreach ($this->input->post('custom') as $custom) {
// I hate myself for this...
$db_array[str_replace(']', '', str_replace('custom[', '', $custom['name']))] = $custom['value'];
}
$this->load->model('custom_fields/mdl_quote_custom');
$this->mdl_quote_custom->save_custom($quote_id, $db_array);
}
echo json_encode($response);
}
Edit 1
The last comments and answers pointed out the name field is not visible. I have uploaded all files that correspond with this to http://websiterheine.eu/quotes.zip
When you look at views/view.php then there are no name fields on any input. But all inputs are correctly working except the one I asked the question about.
Please take a look at the .zip.
Edit 2
As pointed out by Patrick a .zip ain't save. So no one will download. Therefore I will post the files on JSFIDDLE. The fiddles wont work because I use it only as a placeholder for the files.
**views/view.php**
**models/mdl_quotes.php**
http://jsfiddle.net/4vngkegm/1/
**controllers/ajax.php**
http://jsfiddle.net/forv452m/1/
**controllers/quotes.php**
Edit 3
I just checked again. There is nowhere in any files $_POST. So it is very strange it is saving all form values.
I have never came across a form like this with no name attributes and no $_POST values. How is this form saving and loading and where is that exactly???
Upvotes: 0
Views: 298
Reputation: 131
I found it with help from someone else wtf8_decode thank you for helping. Also eveyone else thank you.
So the solution was to add in views/view.php the line
quote_type: $('#quote_type').val(),
to
$.post("<?php echo site_url('quotes/ajax/save'); ?>", {
quote_id: <?php echo $quote_id; ?>,
quote_number: $('#quote_number').val(),
quote_date_created: $('#quote_date_created').val(),
quote_date_expires: $('#quote_date_expires').val(),
quote_status_id: $('#quote_status_id').val(),
items: JSON.stringify(items),
custom: $('input[name^=custom]').serializeArray()
},
So it would look like this:
$.post("<?php echo site_url('quotes/ajax/save'); ?>", {
quote_id: <?php echo $quote_id; ?>,
quote_number: $('#quote_number').val(),
quote_type: $('#quote_type').val(),
quote_date_created: $('#quote_date_created').val(),
quote_date_expires: $('#quote_date_expires').val(),
quote_status_id: $('#quote_status_id').val(),
items: JSON.stringify(items),
custom: $('input[name^=custom]').serializeArray()
},
Now it is working as it should!
Upvotes: 0
Reputation: 28906
Each form field must have a name to allow you to reference it in your PHP code. In general, you can add a name like this:
<input name="your_field_name" type="...
Then reference that name in the PHP code that handles your form.
$input = $_POST['your_field_name'];
Your value will now be saved in $input
and can be inserted into the database as needed.
In your specific case, your code appears to expect an array of input items, here:
$items = json_decode($this->input->post('items'));
To make your existing code work with the new change, simply name your field using array syntax:
<input name="items[]" type="...
Upvotes: 1
Reputation: 2708
Change
<input type="text" id="quote_type" class="input-small" value="<?php echo $quote->quote_type; ?>" style="margin: 0px;">
To
<input type="text" id="quote_type" name="quote_number" class="input-small" value="<?php echo $quote->quote_type; ?>" style="margin: 0px;">
Add the name attribute of input. If your field name is quote_number
then add name="quote_number"
Hope it helps.
Upvotes: 1
Reputation: 3698
It is because you are missing name
attribute in your input tag
<input type="text" id="quote_type" class="input-small" value="<?php echo $quote->quote_type; ?>" style="margin: 0px;">
change this to
<input type="text" id="quote_type" name="quote_id" class="input-small" value="<?php echo $quote->quote_type; ?>" style="margin: 0px;">
P.S. use of inline CSS in not that good
Upvotes: 1