When I click on buttons I generate some inputs field with custom attributes like that :
<input type='text' name='field["+ i++ +"]' value='' data-kind='title' />
<input type='text' name='field["+ i++ +"]' value='' data-kind='video' />
<input type='text' name='field["+ i++ +"]' value='' data-kind='text' />
I retrieve the 'name' value with a foreach loop in PHP :
$result = array_combine($num, $records);
foreach ($result as $rank => $content)
{
$data = array(
'content' => $content,
'post_id' => $post_id,
'rank' => $rank,
'type' => $this->input->post('field_type') // HERE
);
echo '<pre>';print_r($data);echo '</pre>';
}
To get the 'type' I do a $this->input->post('field_type'); which is given by this :
var field_type = $(":input[data-kind]").attr('data-kind');
$("#field_type").val(field_type' ');
and :
echo '<input type="hidden" id="field_type" name="field_type" value="" />';
But it only returns me the last 'data-kind' value not each one :/
Now i just need to loop the 'data-kind' value for each input fields and retrieve them in my foreach loop
Any help would be very very appreciated !!
Many thanks for your answers, it helped me a lot! But now how can I add the result in my current foreach at 'type' data :
$result = array_combine($num, $records);
foreach ($result as $rank => $content)
{
$data = array(
'content' => $content,
'post_id' => $post_id,
'rank' => $rank,
'type' => // HERE I NEED EACH ATTRIBUTE VALUE
);
echo '<pre>';print_r($data);echo '</pre>';
}
If you want to place all of the data-kind values in the #field_type field, you need something like this:
var fieldTypes = [];
$("input[data-kind]").each(function()
{
fieldTypes.push( $(this).attr('data-kind') );
});
$("#field_type").val(fieldTypes.join(','));
Maybe you've missed the plus sign?
$("#field_type").val(field_type' ');
should be $("#field_type").val(field_type+' ');
This code:
http://jsfiddle.net/PKgkU/17/
Does what you want!
$('input').each(function(el) {
switch ($(this).data('kind')) {
case "video":
kind = 'video';
break;
case "image":
kind = 'image';
break;
case "title":
kind = 'title';
break;
default:
break;
}
$(this).after('<input type="hidden" id="field_type" name="field_type" value="' + kind + '" />');
});
Related
everything are right but when i put two tag same together second tag are create but not first one
<?php
require 'header/security-class.php';
class SecureForm{
function form($action ,$method ,$InputArray ,$button=false){
global $security;
$form = '';
$form .= "<form action='$action' method='$method'>";
$form .= "<input type='hidden' name='token' value= '". $security->GenerateTokens(3, 20) ."' />";
foreach ($InputArray as $input=>$key){
// for label elemnt
//requier contect in attribute
if($input == 'label' ){
$form .= "<label ";
foreach($key as $attribute=>$value){
$form .= $attribute."='".$value."' ";
}
$form .= ">";
$form .= $key['content'];
$form .="</label>";
}else{
//for input element
//esay to make type by input:type
//else you can make it in attrbuits
$type = explode(':',$input);
if($type[0] == "input")$elemnt = $type[0];
if($type[0] == "input" and count($type) > 1 ){
$form .= "<". $type[0] ." type ='". $type[1] ."' ";
}elseif($type[0] == "input" and count($type) > 1 ){
$form .= "<input ";
}else{
$form .= "<$input ";
}
foreach($key as $attribute=>$value)
$form .= $attribute."='".$value."' ";
if($type[0] == "input"){
$form .= '/>';
}else{
$form .= " ></$input>";
}
}
}
if(is_array($button)){
$form .= '<button name="'.$button['name'].'" class="'.$button['class'].'">'.
$button['content'].'</button>';
}
$form .= "</form>";
return $form;
}
}
$form = new SecureForm;
$InputArray = array(
'input:text' => array(
'id'=>'input',
'class'=>'input',
'max' =>'250'
),
'input:text' => array(
'name'=>'button',
'value'=>'name',
'id' =>'button'
)
);
echo $form->form('#','post',$InputArray );
//result
<input type="text" name="button" value="name" id="button">
//must be
<input type="text" id="input" class="input" max="250">
<input type="text" name="button" value="name" id="button">
what are wrong is here ?
because i test it in a a lot of code like
two textarea , hidden or input
but i can't solve it
sorry but i must write anything because this issue
You are overwritting your array indexes.
$InputArray = array(
// v----------v--------------- first index name
'input:text' => array(
'id'=>'input',
'class'=>'input',
'max' =>'250'
),
// v----------v--------------- Same index name, the last value will be kept
'input:text' => array(
'name'=>'button',
'value'=>'name',
'id' =>'button'
)
);
that provides this var_dump($InputArray); :
array(1) {
["input:text"]=>
array(3) {
["name"]=>
string(6) "button"
["value"]=>
string(4) "name"
["id"]=>
string(6) "button"
}
}
You might want to use a proper unique index and use the input type in a value separately such as :
$InputArray = array(
array(
'input_type' => 'input:text',
'id'=>'input',
'class'=>'input',
'max' =>'250'
),
array(
'input_type' => 'input:text',
'name'=>'button',
'value'=>'name',
'id' =>'button'
)
);
And then, in your loop, instead of using $input, use the proper index :
foreach ($InputArray as $value) {
...
if($value['input_type'] == 'label' ) {
...
i'm newbie for codeigniter. i trying to passing data attendance into database.
this my view code
<?php $no=1; foreach($employee AS $list_emp) { ?>
<tr>
<td><?= $no ?></td>
<td><input type="hidden" name="employee[]" value="<?php echo $list_emp->sn; ?>"><?= $list_emp->name; ?></td>
<td><?= $list_emp->position; ?></td>
<?php foreach ($attend_detail AS $list) {?>
<td><input type="checkbox" name="detail[]" value="<?php echo $list['details']"></td>
<?php } ?>
<td><input type="text" name="note[]"></td>
<input type="hidden" name="location[]" value="<?php echo $list_emp->branch; ?>">
</tr>
<?php $no++;} ?>
when i checked for 1 employee attendance (example 4630 is work), data can pass to database, but result like this (see image 2)
all data view input to database, not data when 1 checked before and remark WORK insert into row 1.
this my controller
function add_attend()
{
$employee = $this->input->post('employee');
$location = $this->input->post('location');
$detail = $this->input->post('detail');
$note = $this->input->post('note');
$total = count($employee);
if (empty($detail) === true) { $errors['detail'] = 'please select one';}
if (!empty($errors)){
$info['success'] = false;
$info['errors'] = $errors;
}
else {
for ($x=0; $x<$total; $x++){
$data = array(
'sn' => $employee[$x],
'lab' => $location[$x],
'stat' => $detail[$x],
'note' => $note[$x]
);
$this->m_human_capital->insert_attend($data);
}
$info['success'] = true;
}
$this->output->set_content_type('application/json')->set_output(json_encode($info));
}
and this my model
function insert_attend($data)
{
$this->db->insert('tb_attend_tes',$data);
}
i just want insert employee attendance who i checked. please help
thanks for anyone help.
sorry my bad english
Add an identifier on your employee attendance input name, so each employee have their own unique attendance dataset.
view :
...
<td><input type="checkbox" name="detail[<?php echo $no-1 ?>][]" value="<?php echo $list['details']"></td>
...
Since the attendance input is a multidimensional array, for the empty validation, you could use array_filter to check the whole attendance array.
And because of you are inserting an array data type into a single column, you need to concatenate it, you could use implode() function.
controller :
function add_attend()
{
$employee = $this->input->post('employee');
$location = $this->input->post('location');
$detail = $this->input->post('detail');
$note = $this->input->post('note');
$total = count($employee);
$filtered_detail = array_filter($detail);
if (empty($filtered_detail) === true) {
$errors['detail'] = 'please select one';
}
if (!empty($errors)){
$info['success'] = false;
$info['errors'] = $errors;
}
else {
for ($x=0; $x<$total; $x++){
$data = array(
'sn' => $employee[$x],
'lab' => $location[$x],
'stat' => (isset($detail[$x]) && !empty($detail[$x])) ? implode(",",$detail[$x]) : '',
'note' => $note[$x]
);
$this->m_human_capital->insert_attend($data);
}
$info['success'] = true;
}
$this->output->set_content_type('application/json')->set_output(json_encode($info));
}
How would I pass an array of multiple checkboxes and values through php, saving to a sql database, as well as being able to pull up the saved data back on the client side?
For example, I have an array that saves checkbox values in the way of '"checked"""checked""""', but not only do I want to save the checked value, I also want to save the form data, as follows :
Apples : Checked
Oranges : Not Checked
Bananas : Checked
Tomatoes : Checked
Any help would be GREATLY appreciated, please answer with context of how to do, not just code - still learning!
Here is the code THAT WORKS for this question
processing code :
$salesman = $data['data-invoice-salesman']; // this is an array
$salesman_array = array(); // create new array
$salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User");
for($i = 1; $i <= 5; $i++){ // loop from 1 to 5
if(in_array($i, $salesman)){ // if value exists (has been selected), stack 'checked', if not, stack ''.
$salesman_array[$salesman_names[$i]] = "checked";
} else {
$salesman_array[$salesman_names[$i]] = "";
}
}
$salesman_json = mysqli_real_escape_string($this->_con, json_encode($salesman_array)); // encode the array into JSON and then escape it.
form code THAT WORKS :
<?php
$salesmand = json_decode($invoice['Invoice']['salesman'], true);
$salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User5");
foreach ($salesman_names AS $i => $name) {
if ($salesman[$name] == "checked") {
echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/> '.$name.'<br>';
} else {
echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" /> '.$name.'<br>';
}
}
?>
</div>
Add an array that maps the indexes to the name:
$salesman_names = array(
1 => "Joe Smith",
2 => "Fred Flintston",
3 => "George Jetson",
4 => "John Hamm",
5 => "Alan Smithee"
);
Then in your loop, you can do:
$salesman_array[$salesan_names[$i]] = "checked";
The JSON that's saved in the database might then look like:
{"Joe Smith":"checked", "Fred Flintson": "", "George Jetson","checked", "John Hamm":"", "Alan Smithee":""}
To display the checkboxes, you would refer to the $salesman_array when displaying it:
$salesman = json_decode($invoice['Invoice']['salesman'], true);
foreach ($salesman_names AS $i => $name) {
if ($salesman[$name] == "checked") {
echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/> '.$name.'<br>';
} else {
echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" /> '.$name.'<br>';
}
}
I am trying to make a function so that if the user clicks the Update button AND the text in the textbox is a valid text(only the text from the array should be valid ie. text1, text2, text3). Then, it echo's the number assosiated to the text in the array so if text1 is entered, it should echo 10. I made a function to do it but it says Invalid argument supplied for foreach() on the foreach loop line.
HTML:
<input type='text' id='usertext' name='usertext' size='15' />
<input type='submit' name='update' id='update' value='Update' />
PHP:
public currenttext = 0;
$config['text'] = array(
10 => 'text1',
25 => 'text2',
50 => 'text3'
);
public function set_text($validtext) {
foreach($this->config['text'] as $key => $value) { // <-- foreach loop
if($key == $validtext){
$this->currenttext = $value;
}
}
}
if ($_POST['update') {
$this->set_text($_POST['usertext'));
}
You may try this:
if ( isset($_POST['update']) ) {
$this->set_text( $_POST['usertext'] );
}
Also flip the array;
$config['text'] = array(
'text1' => 10,
'text2' => 25,
'text3' => 50
);
Finally change the set_text method to something like this:
// $validtext could be text1 or text2 or text3 from user
public function set_text($validtext) {
if(array_key_exists($validtext, $this->config['text'])) {
$this->currenttext = $this->config['text'][$validtext];
}
}
Integrate these in your class properly, it'll work. Also check array_key_exists on PHP manual.
Actually, you dont need a foreach loop, alternatively, you could also use array_search() to get the same goal. Consider this example:
<?php
if(isset($_POST['update'])) {
$usertext = $_POST['usertext'];
$config['text'] = array(10 => 'text1', 25 => 'text2', 50 => 'text3');
$key = array_search($usertext, $config['text']);
if($key !== false) {
echo $key;
} else {
// not found
{
exit;
}
?>
<form method="POST">
<input type='text' id='usertext' name='usertext' size='15' /><br/>
<input type='submit' name='update' id='update' value='Update' />
</form>
I am trying to add a search module for my data listing .
my function that i post the request to, renders a view.
how can i load the list grid after pushing search button?
should i echo a html code, or render a view and write that view in that position?
I am totally confused ...
public function actionSell() {
$cond="";
if($_POST) {
foreach ($_POST as $key => $value) {
$_POST[$key] = str_replace(',', '', stripslashes(mysql_real_escape_string($value)));
}
$melk_id = $_POST['melk_id'];
$city_id = $_POST['city_id'];
$cost_from = $_POST['cost_from'];
$cost_to = $_POST['cost_to'];
$metraj_from = $_POST['metraj_from'];
$metraj_to = $_POST['metraj_to'];
if($melk_id) $cond .= ' and melk_id='.$melk_id;
if($city_id) $cond .= ' and city_id='.$city_id;
if($cost_from) $cond .= ' and cost >='.$cost_from;
if($cost_to) $cond .= ' and cost <='.$cost_to;
if($metraj_from) $cond .= ' and metraj >='.$metraj_from;
if($metraj_to) $cond .= ' and metraj <='.$metraj_to;
}
$dataProvider = new CActiveDataProvider('Data', array('criteria' => array(
'condition' => 'type = 0 '.$cond,
'order' => 'id DESC',
),
'pagination' => array('pageSize' => 15)
));
$this->render('sell', array(
'dataProvider' => $dataProvider,
));
}
I'm not a php developer but I'll try yo answer in a jQuery manner.
Suppose you hold your list grid in a container: <div id="container"> //the grid </div>
You fill the container from a View that holds the grid
You have Seach TextBox
<input id="searchBox" type="text" name="SeachTxt" Value="Some text"/>
And a Button :
<input id="SearchBtn" type="button" value="Search Now!">
Next you must have a jQuery ajax post function, like this one:
jQuery("#SearchBtn").click(function(e) {
e.preventDefault();
jQuery.ajax({
url: 'index.php?searchString=' + jQuery("#searchBox").val(),
success: function(result) {
jQuery("#container").html(result);
}
});
});
you could use renderPartial, like
$this->renderPartial('_ajaxContent', array(
'dataProvider' => $dataProvider
), false, true
);