Welcome Guest, Not a member yet? Register   Sign In
Clone form field, how to enter data of multiple clone fields in mysql database.
#1

[eluser]Imman Kant[/eluser]
I'm new to codeigniter. I need help with insertion of data in the db from multiple clone form fields, this from clone form fields(If I click add, it should display another clone of the same form field again is done). Pushing a single value is fine, but when I try pushing in multiple clone form fields at the same time, there is a problem. Or give me a hint as to how. I have attached an image of the dummy form I have used to try it out. I used SheepCloneit for jquery validation purpose.
Following are the two functions codes:
Code:
public function cascadehr()
{
$data['house']=$this->db->get('house')->result_array();
$data['rooms']=$this->db->get('rooms')->result_array();
//print_r($data['rooms']);
//print_r($data['house']);
$this->load->view('cascadehomeroom',$data);
}

public function savehr(){
$config['upload_path']   =   "./uploadshouseroom/";
$config['allowed_types'] =   "gif|jpg|jpeg|png";
$config['max_size']      =   "10000";
$config['max_width']     =   "2500";
$config['max_height']    =   "1280";
$config['encrypt_type']= true;
$this->load->library('upload',$config);

if(!$this->upload->do_upload('Interior_house_room_image'))
{
echo $this->upload->display_errors();
}
else
{
$finfo=$this->upload->data();
}

$data['Interior_house_id']=$_POST['interior_house_desc2'];
$data['Interior_room_id']=$_POST['interior_room_desc2'];
$data['Interior_house_room_image']= $finfo['file_name'];
$data['Interior_house_room_status']=$_POST['Interior_house_room_status'];
$this->load->model('Think_model');

/*this part I'm experimenting as to insert the data from clone fields
foreach($this->input->post('interior_house_desc2') as $key => $value)
{
$data=array(
"interior_house_desc2"=>$value,
"interior_room_desc2"=>$interior_room_desc2[$key],
//"Interior_house_room_image"=>$Interior_house_room_image[$key],
"Interior_house_room_status"=>$Interior_house_room_status[$key]);
print_r($data); exit;
$this->db->insert('house_rooom', $data);
} */

//print_r($data); exit;
$result=$this->Think_model->houseroom($data);
if($result){
redirect('cascadehr');
}
else
echo "failure";
}
This is the form in views folder
Code:
<link rel="stylesheet" href="<?php echo base_url();?>css/customMessages.css" />
<link rel="stylesheet" href="<?php echo base_url();?>css/validationEngine.jquery.css" />
[removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]

[removed]
jQuery(document).ready(function(){
// binds form submission and fields to the validation engine
jQuery("#formID").validationEngine();
});
[removed]

[removed]
jQuery(document).ready(function(){
// binds form submission and fields to the validation engine
jQuery("#formID1").validationEngine();
});
[removed]

[removed]
jQuery(document).ready(function(){
// binds form submission and fields to the validation engine
jQuery("#formID2").validationEngine();
});
[removed]

[removed]
//sheepit plugin used for cloning third form content
$(document).ready(function() {
var sheepItForm = $('#sheepItForm').sheepIt({
separator: '',
allowRemoveLast: true,
allowRemoveCurrent: true,
allowRemoveAll: true,
allowAdd: true,
allowAddN: true,
maxFormsCount: 10,
minFormsCount: 0,
iniFormsCount: 1
});
});

[removed]

<style>
a {
text-decoration:underline;
color:#00F;
cursor:pointer;
}

#sheepItForm_controls div, #sheepItForm_controls div input {
float:left;    
margin-right: 10px;
}

</style>
</head>
<form id="formID2" class="formular" method="post" acti echo base_url(); ?>savehr" enctype="multipart/form-data">

<!-- sheepIt Form -->
<div id="sheepItForm">

&lt;!-- Form template--&gt;
<div id="sheepItForm_template">

<h3>Home type</h3>
<select name="interior_house_desc2" id="interior_house_desc" class="validate[required]">
<option value="">Please select</option>
&lt;?php foreach($house as $ho){ ?&gt;
<option value="&lt;?php echo $ho['interior_house_id'];?&gt;">&lt;?php echo $ho['interior_house_desc'];?&gt;</option>
&lt;?php } ?&gt;
</select>

<h3>Room type</h3>
<select name="interior_room_desc2" id="interior_room_desc" class="validate[required]">
<option value="">Please select</option>
&lt;?php foreach($rooms as $ro){ ?&gt;
<option value="&lt;?php echo $ro['interior_room_id'];?&gt;">&lt;?php echo $ro['interior_room_desc'];?&gt;</option>
&lt;?php } ?&gt;
</select>

<h4>House-Image</h4>
&lt;input type="file" id="imagehr" name="Interior_house_room_image" value="upload"&gt;

<h4>Status </h4>
<td><select name="Interior_house_room_status" id="Interior_house_room_status" class="validate[required]">
<option value="">Please select</option>
<option value="1">Active</option>
<option value="0">Inactive</option>
</select></td>

<a id="sheepItForm_remove_current">
<img class="delete" src="images/remove.png" width="16" height="16" border="0">
</a>
</div>
&lt;!-- /Form template--&gt;

&lt;!-- No forms template --&gt;
<div id="sheepItForm_noforms_template"><a href="&lt;?php echo base_url();?&gt;cascadehr">click to go add homes</a></div>
&lt;!-- /No forms template--&gt;

&lt;!-- Controls --&gt;
<div id="sheepItForm_controls">
<div id="sheepItForm_add"><a><span>Add home</span></a></div>
<div id="sheepItForm_remove_last"><a><span>Remove</span></a></div>
<div id="sheepItForm_remove_all"><a><span>Remove all</span></a></div>
<div id="sheepItForm_add_n">
&lt;input id="sheepItForm_add_n_input" type="text" size="4" /&gt;
<div id="sheepItForm_add_n_button"><a><span>Add</span></a></div></div>
</div>
&lt;!-- /Controls --&gt;

</div>
&lt;!-- /sheepIt Form --&gt;
&lt;input name="Submit" type="submit" value="Submit"&gt;
&lt;input name="Submit" type="reset" value="Cancel"&gt;
&lt;/form&gt;

Please help me with regards to this, it's just bit of my display file as I have attached..
#2

[eluser]TheFuzzy0ne[/eluser]
SheepClone? Isn't that a reference to Dolly the sheep? If so, your plugin is illegal. Tongue

You've got two choices:
1) Make sure the name attribute of each cloned field adheres to a sequence. i.e "Interior_house_room_status_1", "Interior_house_room_status_2", "Interior_house_room_status_3" and so on.

This can be a pain in the arse when it comes to validation, since you'll need to add your validation rules in a loop, which means you'll also need to know how many fields are being submitted. You can use a hidden form field for that, but be sure to set a soft limit so that your loop doesn't execute infinitely if someone tries to exploit your form.

2) Use an array to send the data to the server. This can take time and patience to implement correctly, and whenever I've tried it, I've had problems with the validation class (which I've managed to overcome). Each form field will be "named Interior_house_room_status[]" (note the square brackets), so when the data is submitted to the server, it's sent as an array.

The problem I mentioned occurs because when you use set_value() in your view, and also if someone somehow deletes an entry from the database after the form is loaded, but before the form has been submitted. With set_value(), CodeIgniter pops the first entry of the array off each time set_value() is called, which is fine, but if any entries have been deleted on the page, then you'll find the array indices won't be what you might expect. For example, you might have 5 entries in the database to be updated, but only 3 have been submitted, because 2 were deleted.

The easiest way to get around that, is to just delete all entries in the database pertaining to that field, and then looping through each submitted value and re-adding them. Sorry if this doesn't make much sense. It's quite difficult to explain.

For what it's worth, I hope this helps.
#3

[eluser]Imman Kant[/eluser]
It's not my plugin, I have used someone else's plugin. That's not illegal, is it? Tongue

I must request you, if it is possible at all, to send me an example of some simple sort on the same. I'm sorry, but I'm still pretty new to php and codeigniter. Sad
#4

[eluser]Unknown[/eluser]
try to use input arrays in naming your filelds and use foreach loop to insert the data in you database
see http://jsfiddle.net/leonardeveloper/8xDhw/:lol:
#5

[eluser]Imman Kant[/eluser]
I have managed to get something by using implode.

@leonard: That page doesn't seem to work, can you give me another link? I get an error.

"Error 404

We're truly sorry, but there is no such page." is what it shows. And, I have used implode to manage to push multiple data into a single record. But, what I'm looking at is the same reference id, doesn't matter if it repeats, changing one field, ie the chosen field. I hope I am able to explain properly.




Theme © iAndrew 2016 - Forum software by © MyBB