i am working on textbox in which value is display after selecting value from the combo box .i need to get that value that appear in textbox for further computation in the same page.
here is the coding
<?php
$options = array(
'0' => array(
'title' => ' ',
'value1' => '',
'value2' => '',
),
'1' => array(
'title' => 'Islamabad',
'value1' => '31.41',
'value2' => '73.11',
),
'2' => array(
'title' => 'Lahore',
'value1' => '31.56',
'value2' => '74.35',
),
'3' => array(
'title' => 'Kharachi',
'value1' => '24.86',
'value2' => '67.01',
),
'4' => array(
'title' => 'Faisalâbâd',
'value1' => '31.41',
'value2' => '73.11',
),
'5' => array(
'title' => 'Gujrânwâla',
'value1' => '32.16',
'value2' => '74.18',
),
);
if (isset($_GET['option']) && isset($options[$_GET['option']])) {
echo json_encode($options[$_GET['option']]);
exit;
}
?>
<form>
<?php echo "SELECT " ?>
<select name="combo" id="combo">
<?php
foreach($options as $key_value => $option)
{
printf('<option value="%s">%s</option>', $key_value, $option['title']);
}
?>
</select>
<input type="text" name="hidden" id="textboxB" value="" />
<input type="text" name="hidden" id="textboxC" value="" />
<script type="text/javascript" src="jquery.min1.js"></script>
<script type="text/javascript">
$(function(){
$('#combo').change(function(){
$.getJSON("?", {
option : $(this).val()
}, function (data) {
$('#textboxB').val(data.value1);
$('#textboxD').val(data.value1);
$('#textboxE').val(data.value1);
$('#textboxC').val(data.value2);
});
});
});
</script>
i need to get the value of textboxB and textboxC in separate variable so that i can use theses value for further computation on the same page.
i want to use theses value in if -else statement like
if(textboxD==3.15)
{
$a=$b......
}
my requirement is to get these value in separate variable.
people plz help me in ths
You need to set your input names to arrays, such as:
<input type="text" name="hidden[]" id="textboxA" value="123" />
<input type="text" name="hidden[]" id="textboxB" value="654" />
<input type="text" name="hidden[]" id="textboxC" value="abc" />
<input type="text" name="hidden[]" id="textboxD" value="zyg" />
Remember, ID's are only useful for client-side. ID's never get posted, only the NAME and VALUE.
Now, in PHP, you can access these values via:
<?php
$hidden = !empty($_POST['hidden']) ? $_POST['hidden'] : false;
if($hidden) {
foreach($hidden as $key => $value) {
echo "$key => $value\n"; // 0 => 123, 1 => 654, etc.
}
// Directly access single item:
echo $hidden[3]; // zyg
}
?>
Related
I am Inserting a multiple form input data in to database using Codeigniter.
I have this post input array:
Array
(
[subject_id] => Array
(
[0] => 1
[1] => 1
)
[question] => Array
(
[0] => test
[1] => test2
)
[option1] => Array
(
[0] => test
[1] => test2
) )
I don't get that how do i convert this array to insert How to insert this array using Insert batch.
$this->db->insert_batch('mytable', $data);
This is the form code which i use for posting the data:
<form method="post">
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
// Down side Part is appended when user want to add more question
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
<input type="submit" name="submit" >
</form>
Below is the Array format which i want.
$data = array(
array(
'subject_id' => 'My title' ,
'question' => 'My Name' ,
'option1' => 'My date'
),
array(
'subject_id' => 'Another title' ,
'question' => 'Another Name' ,
'option1' => 'Another date'
)
);
<?php
$i = 0;
foreach($subject_id as $key=>$val)
{
$data[$i]['subject_id'] = $val;
$data[$i]['question'] = $question[$key];
$data[$i]['option1'] = $record[$key];
$i++;
}
$this->db->insert_batch('mytable', $data);
?>
Try like below:Assume $records is an array that you want to insert.
foreach ($records as $record)
{
for ($i=0; $i < count($record); $i++)
{
$data[$i]['subject_id'] = $record['subject_id'][$i];
$data[$i]['question'] = $record['question'][$i]
$data[$i]['option1'] = $record['option1'][$i];
}
}
Then
$this->db->insert_batch('mytable', $data);
I am Inserting a multiple form input data in to database using Codeigniter.
I have this post input array:
Array
(
[subject_id] => Array
(
[0] => 1
[1] => 1
)
[question] => Array
(
[0] => test
[1] => test2
)
[option1] => Array
(
[0] => test
[1] => test2
) )
I don't get that how do i convert this array to insert How to insert this array using Insert batch.
$this->db->insert_batch('mytable', $data);
This is the form code which i use for posting the data:
<form method="post">
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
// Down side Part is appended when user want to add more question
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
<input type="submit" name="submit" >
</form>
Below is the Array format which i want.
$data = array(
array(
'subject_id' => 'My title' ,
'question' => 'My Name' ,
'option1' => 'My date'
),
array(
'subject_id' => 'Another title' ,
'question' => 'Another Name' ,
'option1' => 'Another date'
)
);
<?php
$i = 0;
foreach($subject_id as $key=>$val)
{
$data[$i]['subject_id'] = $val;
$data[$i]['question'] = $question[$key];
$data[$i]['option1'] = $record[$key];
$i++;
}
$this->db->insert_batch('mytable', $data);
?>
Try like below:Assume $records is an array that you want to insert.
foreach ($records as $record)
{
for ($i=0; $i < count($record); $i++)
{
$data[$i]['subject_id'] = $record['subject_id'][$i];
$data[$i]['question'] = $record['question'][$i]
$data[$i]['option1'] = $record['option1'][$i];
}
}
Then
$this->db->insert_batch('mytable', $data);
I am trying to create a filter system basically, that gets data from input in a HTML form and then uses that data in PHP to filter existing 'profiles' from $profileArray , outputting whatever profiles include the user entered data/variable(s). I can not figure out how to send the user input data through my filter_array functions which, as far as I know, work from testing it using var_dump.
As a beginner, I can't understand $foo and $bar explanations in php.net so clear explanations are very much appreciated.
HTML
<html>
<body>
<form action="profileFilter.php" method="POST">
<p>Age: <input type="text" name="ageChoice"></p>
<p>Colour: <input type="text" name="colourChoice"></p>
<input type="submit" name="catQualitiesBtn">
</form>
</body>
</html>
PHP
<?php
$profileArray = array(
array( 'Name' => "Toby",
'Age' => 3,
'Colour' => "Ginger",
),
array( 'Name' => "Cassie",
'Age' => 3,
'Colour' => "Tabby",
),
array( 'Name' => "Lucy",
'Age' => 1,
'Colour' => "Black",
)
);
function get_profile_by_age ($profile, $age){
return array_filter ($profile, function($ageInput) use ($age){
return $ageInput['Age'] === $age;
});
}
function get_profile_by_age ($profile, $colour){
return array_filter ($profile, function($colourInput) use ($colour){
return $colourInput['Colour'] === $colour;
});
}
$ageInput = intval($_POST['ageChoice']);
$colourInput = strval($_POST['colourChoice']);
echo function get_profile_by_age ($ageInput , $age);
echo function get_profile_by_colour ($colourInput , $colour);
?>
I didn't understand what your question was but i think you are not able to loop through you'r array. This loop will help you too loop through the multidimensional array. And in you array the age isnt in string, so you don't have to change the value of the user input from string to int.
<html>
<body>
<form action="profileFilter.php" method="POST">
<p>Age: <input type="text" name="ageChoice"></p>
<p>Colour: <input type="text" name="colourChoice"></p>
<input type="submit" name="catQualitiesBtn">
</form>
</body>
</html>
PHP
<?php
$profileArray = array(
array( 'Name' => "Toby",
'Age' => 3,
'Colour' => "Ginger",
),
array( 'Name' => "Cassie",
'Age' => 3,
'Colour' => "Tabby",
),
array( 'Name' => "Lucy",
'Age' => 1,
'Colour' => "Black",
)
);
if (isset($_POST['ageChoice']) && isset($_POST['colourChoice'])) {
$ageInput = $_POST['ageChoice'];
$colourInput = $_POST['colourChoice'];
for ($i=0; $i >=0 ; $i++) {
if ($profileArray[i]['Age'] == $_POST['ageChoice'] && $profileArray[i]['Colour'] == $_POST['colourChoice']) {
echo "name -".$profileArray[i]['Name'];
break;
}
}
}
}
I have two arrays, the first one is
$practice_areas = array(
array(
"id" => 1,
"name" => "Administrative",
"form_id" => 1
),
array(
"id" => 2,
"name" => "Admiralty & Maritime",
"form_id" => 1
),
array(
"id" => 3,
"name" => "Agricultural",
"form_id" => 1
),
array(
"id" => 4,
"name" => "Alternative Dispute Resulution",
"form_id" => 1
),
array(
"id" => 5,
"name" => "Antitrust & Trade Regulation",
"form_id" => 1
),
array(
"id" => 6,
"name" => "Appellate Practice",
"form_id" => 1
)
);
and the second one is $selected_id
$selected_id = array(
array(
"id" => 1,
"lawyer_id" => 2,
"practice_area_id" => 3
),
array(
"id" => 2,
"lawyer_id" => 2,
"practice_area_id" => 4
)
);
And this is my current code:
<label>Practice area(s) *</label>
<select id="practice_areas" multiple="multiple" name="practice_areas[]">
<option value="" selected="selected"></option>
<?php foreach($practice_areas as $practice_area) {?>
<option value="<?php $practice_area['id'] ?>" <?php ($selected_id->practice_area_id == $practice_area['id']) ? 'selected="selected"' : ''; ?>><?php $practice_area['name'] ?></option>
<?php } ?>
</select>
I want to select all the options that exist in the $selected_id array. I'm having a bad time on writing this loop correctly.
EDIT1
when I try to use the
foreach ($selected_id as $sel) {
$is_selected[$sel["practice_area_id"]] = true;
}
I've got this result.
$selected_id = array(
array(
"id" => 1,
"lawyer_id" => 2,
"practice_area_id" => 3
),
array(
"id" => 2,
"lawyer_id" => 2,
"practice_area_id" => 4
),
true,
true
);
You could do it like this:
<label>Practice area(s) *</label>
<select id="practice_areas" multiple name="practice_areas[]">
<option value="" selected></option>
<?php
// First create an array that is keyed by practice_area_id for selected items
foreach ($selected_id as $sel) {
$is_selected[$sel["practice_area_id"]] = true;
}
foreach($practice_areas as $practice_area) {
?>
<option value="<?= $practice_area['id'] ?>"<?=
(isset($is_selected[$practice_area['id']]) ? ' selected>' : '>') .
$practice_area['name'] ?></option>
</option>
<?php } ?>
</select>
Note that you had several <?php tags that did not echo anything inside the option tags. You can use instead the <?= tag for that.
Also the selected attribute in an option does not require a value. Just its presence is enough. The same goes for the multiple attribute of the select tag.
Finally, the -> operator only works on objects, but you have arrays, so you should use the bracket notation throughout.
See the above code run on eval.in
Hey guys I'm trying to see where my pairs of keys stop, I have arrays built like this
EDIT People are getting really confused so I'm using a real array instead of an example
array (
'key' => '',
'po' => '',
'label' => '',
'report_key' => '',
'shipper' => '',
'status' => '',
'location' => '',
'inspector' => '',
'commodity' => '',
'brand' => '',
'case_count' => '',
'variety' => '',
'style' => '',
'grower_lot' => '',
'pack_date' => '',
// grouping 4 items
'berry_size1' => '',
'berry_size2' => '',
'berry_size3' => '',
'berry_size4' => '',
// grouping 3 items
'bunch_color1' => '',
'bunch_color2' => '',
'bunch_color3' => '',
// grouping 2 items
'color1' => '',
'color2' => '',
// grouping 3 items
'stem1' => '',
'stem2' => '',
'stem3' => '',
// grouping 2 items
'shatter1' => '',
'shatter2' => '',
// grouping 2 items
'splits1' => '',
'splits2' => '',
// grouping 2 items
'wet_sticky1' => '',
'wet_sticky2' => '',
'overall_quality' => '',
// grouping 2 items
'sugar_brix1' => '',
'sugar_brix2' => '',
'rating' => '',
'comments' => '',
)
I came up with some stupid way that really doesn't work to try and sort things out, its extremely backwards, honestly I'm pretty embarrassed by my attempt.
foreach($obj as $key=>$val) {
if(strpos( preg_replace('/[^a-z]/i', '', $key),
preg_replace('/[^a-z]/i', '', $all_keys[$key+$b+1])
) !== false) { echo "<p>$key</p>"; // items 1-3 will show
} elseif(strpos(preg_replace('/[^a-z]/i', '', $key),
preg_replace('/[^a-z]/i', '', $all_keys[$key+$b-1])
) !== false) { echo "<p>$key</p>"; // show last item
} else {
$in.='<aside class="left">';
$in .= "<label for='$key'>". ucwords(strtolower(str_replace('_',' ',$key))) ."</label><br/>";
$in .= ($key=='key') ? "<input type='text' value='". $objLastId ."' id='$key' class='disabled' disabled='disabled'>" : "<input type='text' value='' name='$key' id='$key'>";
$in.='</aside>';
$b++;
}
}
Anyway what I'm really trying to achieve is something like this, could someone steer me in the right direction please?
<style>
.row2 input {width: 50px !important;}
.row3 input {width: 27px !important;}
.row4 input {width: 15px !important;}
</style>
// stem was a 2 item group, so should have the row4 class
// and should have the second item appended by a
// all be inside the same grouping, like below ...
<aside class="left row2">
<label for="color1">Color</label>
<br/><input type="text" value="" name="color1" id="color1">
<input type="text" value="" name="color2" id="color2">
</aside>
// stem was a 3 item group, so should have the row4 class
// and should have items 2-3 appended by a all be inside
// the same grouping, like below ...
<aside class="left row3">
<label for="stem1">Stem</label>
<br><input type="text" id="stem1" name="stem1" value="">
<input type="text" id="stem2" name="stem2" value="">
<input type="text" id="stem3" name="stem3" value="">
</aside>
// berry_size was a 4 item group, so should have the row4 class
// and should have items 2-4 appended by a all be inside
// the same grouping, like below ...
<aside class="left row4">
<label for="berry_size1">Berry Size</label>
<br/><input type="text" id="berry_size1" name="berry_size1" value="">
<input type="text" id="berry_size2" name="berry_size2" value="">
<input type="text" id="berry_size3" name="berry_size3" value="">
<input type="text" id="berry_size4" name="berry_size4" value="">
</aside>
... or ...
// this is a single, so no extra class and ....
<aside class="left">
<label for="other_item">Other Item</label>
<br/><input type="text" id="other_item" name="other_item" value="">
</aside>
What I see this really boiling down to is reading the next array keys name (I stripped the name and used the integer in my version), atleast I think that's the right way to do it?
$arr = array(
'other_item' => 'value',
// this one ranges 1-3
'first_name1' => 'value',
'first_name2' => 'value',
'first_name3' => 'value',
// this one ranges 1-4
'next_name1' => 'value',
'next_name2' => 'value',
'next_name3' => 'value',
'next_name4' => 'value',
'other_item' => 'value',
// this one ranges 1-4
'last_name1' => 'value',
'last_name2' => 'value',
'last_name3' => 'value',
'last_name4' => 'value',
'other_item' => 'value'
);
$newarr = array();
foreach($arr as $key=>$value)
{
if (preg_match('#^([^\d]+)#', $key, $matches)===1)
$newarr[$matches[1]][] = $value;
}
print_r($newarr);
Output:
Array
(
[other_item] => Array
(
[0] => value
)
[first_name] => Array
(
[0] => value
[1] => value
[2] => value
)
[next_name] => Array
(
[0] => value
[1] => value
[2] => value
[3] => value
)
[last_name] => Array
(
[0] => value
[1] => value
[2] => value
[3] => value
)
)
And do whatever you want to do with it. Like that code (just an example, not a very nice one)
foreach($newarr as $name => $block)
{
$cnt = count($block);
echo '<aside class="left'.($cnt>1?' row' . $cnt:'').'">
<label for="' . $name . ($cnt>1?'1':''). '">' .
ucwords(str_replace('_', ' ', $name)) . '</label>
<br/>';
foreach($block as $key=>$element)
{
echo ($key>0?' ':'') . '<input type="text" value="" name="' . $name .
($cnt>1?($key+1):'') . '" id="' . $name .
($cnt>1?($key+1):'') . '">' . "\n";
}
echo '</aside>' . "\n";
}
It gives:
<aside class="left">
<label for="other_item">Other Item</label>
<br/><input type="text" value="" name="other_item" id="other_item">
</aside>
<aside class="left row3">
<label for="first_name1">First Name</label>
<br/><input type="text" value="" name="first_name1" id="first_name1">
<input type="text" value="" name="first_name2" id="first_name2">
<input type="text" value="" name="first_name3" id="first_name3">
</aside>
<aside class="left row4">
<label for="next_name1">Next Name</label>
<br/><input type="text" value="" name="next_name1" id="next_name1">
<input type="text" value="" name="next_name2" id="next_name2">
<input type="text" value="" name="next_name3" id="next_name3">
<input type="text" value="" name="next_name4" id="next_name4">
</aside>
<aside class="left row4">
<label for="last_name1">Last Name</label>
<br/><input type="text" value="" name="last_name1" id="last_name1">
<input type="text" value="" name="last_name2" id="last_name2">
<input type="text" value="" name="last_name3" id="last_name3">
<input type="text" value="" name="last_name4" id="last_name4">
</aside>
As far as I can tell you just want to modify your output based on your 'key type'. I am thinking something like this:
foreach($obj as $key => $val)
{
$parts = explode('_', $key);
switch($parts[0])
{
case 'first':
// Do something here
break;
case 'other':
// Do something here
break;
case 'next':
// Do something here
break;
case 'last':
// Do something here
break;
}
}
I think I might be missing something however, as you appear to be checking 'next' elements...