Get Array Value - php

I have one form that have a select box inside while-loop. I try to get the data or value from that select box. Below is my form code.
<form role="form" method="post" action="test2.php" >
<?php
$endDate = '2014-01-28';
$startDate = '2014-01-27';
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startDate ) ) / 86400 );
$x=0;
while($x<=$daydiff)
{
?>
<select class="form-control" name="day_<?php echo $x; ?>">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
</select>
<select class="form-control" name="time_<?php echo $x; ?>">
<option value="10am">10am</option>
<option value="11am">11am</option>
<option value="12pm">12pm</option>
<option value="1pm">1pm</option>
</select>
<?php
$x++;
}
?>
<input type="hidden" name="count" value="<?php echo $x ?>"/>
<button type="submit" name="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
</form>
Below is my "test2.php".
<?php
for ($i = 0; $i < $_POST['count']; $i++){
$day_var = 'day_' . $i;
$days_list[] = $_POST[$day_var];
$time_var = 'time_' . $i;
$time_list[] = $_POST[$time_var];
echo ($_POST[$day_var]);
echo ($_POST[$time_var]);
}
?>
When I submit it will echo "monday10tuesday11am" how can I extract the value separately i.e. monday10 and tuesday11am?

You can replace your numbering scheme and variable variables with a simple array in HTML.
<select class="form-control" name="day[<?php echo $x; ?>]">
<select class="form-control" name="time[<?php echo $x; ?>]">
$days_list[] = $_POST[$day];
$time_list[] = $_POST[$time];
This would greatly simplify your form2.php. Otherwise, please try to explain what results you are expecting with the code you have.

just add some space to your echo.
echo ($_POST[$day_var]).' '.($_POST[$time_var]);

Related

How to get the second value in the echo in PHP?

I have some options to select and would like to set some textinputs to visible or hidden depending on what user selects. I have the functionality already but in this case I need to take the second value of the echo and check if its equal to something, I can currently only check the first value of the echo:
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
So, I need to get ONLY the value of $row['serial_or_imei'] and compare onchange="if (this.value=='') , how can I do that?
EDIT: further explanation
I need to read the value of $row['serial_or_imei'] which could be empty, 'serial' OR 'imei', so, one of these three.
P.S: I must keep the $row['id'] in the value attribute because I am passing that value later to server so dont suggest removing that.
You can add one attribute to option element and test with it. something like
<select id="type_id" name="device_type_id" onchange="if (this.getAttribute('data')==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option data="<?php echo $row['serial_or_imei'] ?>" value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You can compare the value of an array in the onchange by assign the value to a variable which will be a quite neat format as
<?php $value = echo $row['serial_or_imei']; ?>
<select id="type_id" name="device_type_id" onchange="if (this.value=='<?php empty($value)?>'){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You add a new custom attribute serial and supply the value of serial_or_imei to it and then compare in javascript with onchange="if (this.value=='')
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-serial="<?php echo $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
EDIT: Attribute “serial” is not allowed on element “option”. Changed this to data-seial.
You say you need the values back to server because they change dynamically. A clean way would be to have a dedicated hidden field:
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
Here's a full self-contained script to illustrate it:
<?php
$random_serial_or_imei = range(1000, 9999);
shuffle($random_serial_or_imei);
$result = [];
for ($i = 0; $i < 5; $i++) {
$result[] = [
'id' => $i,
'serial_or_imei' => $random_serial_or_imei[$i],
'name' => "Name #$i",
];
}
?>
<select name="device_type_id">
<option value="">Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id']?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
This prints something like:
<select name="device_type_id">
<option value="">Select</option>
<option value="0">Name #0</option>
<option value="1">Name #1</option>
<option value="2">Name #2</option>
<option value="3">Name #3</option>
<option value="4">Name #4</option>
</select>
<input type="hidden" name="map[0]" value="9513">
<input type="hidden" name="map[1]" value="3931">
<input type="hidden" name="map[2]" value="3971">
<input type="hidden" name="map[3]" value="4476">
<input type="hidden" name="map[4]" value="6429">
Selected type can be found at $_POST['device_type_id'] and the corresponding IMEI at $_POST['map'][ $_POST['device_type_id'] ].
Based on the information from the comments, I think this solution is what you are looking for. Note I move the onchange to a function for clarity.
You can see the "three" option hides the text input as it doesn't have a imei.
Also notice how I use the data- attribute inside your option so you can retrieve that information as well, independent of the option id. Also note the this.form['device_imei_textinput'] didn't worked. The elements reference is required.
function check_imei(obj, form) {
var imei = obj.options[obj.selectedIndex].getAttribute('data-imei');
if (imei == '') {
form.elements.device_imei_textinput.style.visibility='hidden';
} else {
form.elements.device_imei_textinput.style.visibility='visible';
}
}
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<option value="1" data-imei="foo">One</option>
<option value="2" data-imei="bar">Two</option>
<option value="3" data-imei="">Three</option>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
Mirror of example : https://jsfiddle.net/asp1hewx/
EDIT : To address the PHP side of things, the form above could be generated with :
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-imei="<?php $row['serial_or_imei'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>

Eliminate the Selected option if already selected in database...?

I have a select box with options up to 20, for example, if option 5 or any other is selected and saved in a database in the specific column it won't show next time...
please guide me
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
Controller:
function index(){
//below code should be in Model this is only for example
$this->db->select('code');
$query = $this->db->get_where('hr_levels', array('created_by' => $_SESSION['username']));
if ($query->num_rows() > 0) {
$code = $query->row('code');
} else {
$code = 0;
}
$option = array();
for($i=1;$i<=20;$i++){
if ($code != $i) {//no need to add if in view, just pass the data to view
$option[$i] = $i;
}
}
$data['options'] = $option;
$this->load->view('option_view',$data);
}
view:
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($options as $key => $value){
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
You can approach it like this
$availableOption = range(1,20);
$selectedOrInDb = [5,6,8,9]; // Selected OR from DB
$remaining = array_diff($availableOption, $selectedOrInDb);
Now you can use $remaining to loop through
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($remaining as $key => $value){ ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
Controller
public function get_selected(){
$this->load->model('model_name');
$data['selected'] = $this->model_name->function_name($_SESSION['username']);
//Already saved values will be get on the basis of any condition
return $this->load->view('file.php');
}
Model
public function function_name($username){
return $this->db->get_where('table', ['created_by' => $username])->row()->value;
}
Now you get already selected value in $selected
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){
if($i != $selected)?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } } ?>
</select>

Form that uses array, assign object to array

I need to create a form that allows the user to select a number from 0 to 7 and "show all" and the script will then output the object found in that train car to the user, or if show all is picked then it runs a loop that shows all 8 objects along with the train number. But my code doesn't really run.
(Please ignore my code if it doesn't make any sense, but I don't seem to get arrays :( Please point out the mistakes and thank you so so so much)
<html>
<body>
<form name="train" method="GET" action="test.php">
<select>
<option value="0" name="object">0</option>
<option value="1" name="object">1</option>
<option value="2" name="object">2</option>
<option value="3" name="object">3</option>
<option value="4" name="object">4</option>
<option value="5" name="object">5</option>
<option value="6" name="object">6</option>
<option value="7" name="object">7</option>
<option value="8" name="object">8</option>
<option value="all" name="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] ="pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
$train = $_GET['object'];
echo "<p>I have $train!</p>";
}
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
?>
</body>
</html>
There are some mistakes in the code
Select should have name attribute
Need to get object and display results based on the selected option.
Use the below code
<form name="train" method="GET" >
<select name="object">
<option value="0">0</option>
......
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
PHP code
$train[0] ="pencil";
....
$train[8] = "cup";
if ($_GET['submit']) {
$object = $_GET['object'];
if($object == 'all'){
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
}else{
echo "<p>I have $train[$object]!</p>";
}
}

PHP drop down menu

I want to get values of the selected items from 3 drop down menus ( years , month and day) using PHP ,
I tried this but didn't work
<p><font size="6"> <b>TP3</b></font></p>
<p>
</p>
<table align="center" width="800" border="1" cellspacing="2" cellpadding="2">
<tr>
<td><p><?php echo "Date : " . date("Y/m/d") . "<br>";?></p><br/>
<?php $date = new DateTime();
$day = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y'); ?>
<select name="day">
<option value="day" selected="selected">Day</option>
<?php
for($i=1; $i<=31; $i++)
{
printf('<option value="%d" %s>%d</option>', $i, $i == $day ? 'selected="selected"' : '', $i);
}
?>
</select>
<br>
<br>
<br>
<select name="month">
<option value="month" selected="selected">Month</option>
<?php
for($i=1; $i<=12; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
<br>
<br>
<br>
<select name="years">
<option value="years" selected="selected">Years</option>
<?php
for($i=2000; $i<=2020; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
<input type="submit" name="submit" value="Verify Date">
First, you'll need to add your <form action="action.php" method="post"></form> tags so the browser knows where to post the data to. In your "action.php" file, you can retrieve the values the user set the controls to when the form is submitted using the $_POST array:
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
EDIT: If you wish to use the GET method instead of POST, then simply replace $_POST with $_GET.
Try it
<p><font size="6"> <b>TP3</b></font></p>
<p>
</p>
<table align="center" width="800" border="1" cellspacing="2" cellpadding="2">
<tr>
<td><p><?php echo "Date : " . date("Y/m/d") . "<br>";?></p><br/>
<?php $date = new DateTime();
$day = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y'); ?>
<select name="day">
<option value="day" selected="selected">Day</option>
<?php
$i=1;
for($i=1; $i<=31; $i++)
{
printf('<option value="%d" %s>%d</option>', $i, $i == $day ? 'selected="selected"' : '', $i);
}
?>
</select>
<br>
<br>
<br>
<select name="month">
<option value="month" selected="selected">Month</option>
<?php
$i=1;
for($i=1; $i<=12; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
<br>
<br>
<br>
<select name="years">
<option value="years" selected="selected">Years</option>
<?php
$i=2000;
for($i=2000; $i<=2020; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
<input type="submit" name="submit" value="Verify Date">
Firstly, you're missing <form></form> tags (this is very important).
Then a method (GET or POST), but we'll be using POST for this: method="post".
<form> defaults to GET if the method is omitted, a quick FYI.
Then there's the action (see further down below for an examle). The action determines if you wish to execute and show the data pulled in from your form inside the same file action=""
or using an external file action="handler.php".
Here:
<p><font size="6"> <b>TP3</b></font></p>
<p>
</p>
<table align="center" width="800" border="1" cellspacing="2" cellpadding="2">
<tr>
<td><p><?php echo "Date : " . date("Y/m/d") . "<br>";?></p><br/>
<?php $date = new DateTime();
$day = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y'); ?>
<form action="" method="post">
<select name="day">
<option value="day" selected="selected">Day</option>
<?php
for($i=1; $i<=31; $i++)
{
printf('<option value="%d" %s>%d</option>', $i, $i == $day ? 'selected="selected"' : '', $i);
}
?>
</select>
<br>
<br>
<br>
<select name="month">
<option value="month" selected="selected">Month</option>
<?php
for($i=1; $i<=12; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
<br>
<br>
<br>
<select name="years">
<option value="years" selected="selected">Years</option>
<?php
for($i=2000; $i<=2020; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
<input type="submit" name="submit" value="Verify Date">
</form>
Plus, I don't know what type of action you wish to use, that will be up to you to decide.
So change action="" to the file handler you wish to use.
I.e.: action="handler.php" and do your magic from there.
Example of handler.php: and will produce for example Day: 14 Month: 10 Year: 2014
<?php
if(isset($_POST['submit'])){
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['years'];
echo "Day: " . $day . " ";
echo "Month: " . $month . " ";
echo "Year: " . $year;
}
?>
You can place the above inside its own file, or place it below:
// rest of code from your form/code
<input type="submit" name="submit" value="Verify Date">
</form>
<?php
if(isset($_POST['submit'])){
$day = $_POST['day'];
// rest of code
I believe this has answered your question.
Mark it as accepted to close the question properly:
The full article: https://meta.stackexchange.com/q/5234/

All zero values passed in the table

When I execute this code, I only get zeroes inserted into my database. Why? What can I do to solve this problem?
<select class="date" name="year"> <option value="">Year</option>
<?php $i=1; while($i<=31) { ?>
<option value="<?php $i ?>"> <? echo $i; ?> </option>
<?php $i++; } ?> </select>
And this is my mysql code:
$sql="INSERT INTO information ( Year )
VALUES ('$_POST[year]')";
here how it will be your code
<select class="date" name="year">
<option value="">Year</option>
<?php for($i=1;$i<=31 ;$i++ ) { ?>
<option value="<?php $i ?>"> <? echo $i; ?> </option>
<?php } ?>
</select>
EDIT :
<input type ="submit" value =" submit me"/>
<?php if (isset($_POST['year'])) // check if isset the year
{
$year = $_POST['year'] ;
} else {}
$sql="INSERT INTO information ( Year ) VALUES ('".$year."')";
?>
edit 2 :
your code is right but you should escape value to prevent sql injection
do like that
insert into ....
VALUES
('mysql_real_escape_string($_POST[user])','mysql_real_escape_string($_POST[email])'.........
<select class="date" name="day"> <option value="">Day</option>
<?php $i=1; while($i<=31) { ?>
<option value="<?php echo $i; ?>"> <?php echo $i; ?> </option>
<?php $i++; } ?> </select>
Always use <?php and ?> so your browser recognizes PHP
also, with your first <?php $i ?>
I changed this to echo $i; and on the same line I changed <? echo $i; ?> </option> To <?php echo $i; ?> </option>
and it worked for me.
With Your Insert:
$sql="INSERT INTO information (COL_FOR_DAY)
VALUES ('{$_POST['day']}')";

Categories