Not get value of select in form - php

I have a proplem with form php. When i submit form it not get value of select option. I try many time and many way but it not work. Help me,please. Thanks
Code:
<form method="post" action="" id="form_search" >
<article class="module width_full">
<header><h3>Thống kê CCU</h3></header>
<div class="module_content">
<fieldset>
<label>Month</label>
<select name="month">
<?php
$stt=0;
$query=mysql_query("select DATE_FORMAT(date,'%m') as month from skycity_log.ccu_log GROUP BY month ORDER BY month ASC") or die (mysql_error());
while($row = mysql_fetch_array($query)){
echo"<option value='$row[month]'>$row[month]</option>";}?>
</select>
</fieldset>
<fieldset>
<label>Year</label>
<select name="year">
<?php
$query=mysql_query("select DATE_FORMAT(date,'%Y') as year from skycity_log.ccu_log GROUP BY year ORDER BY year ASC") or die (mysql_error());
while($row = mysql_fetch_array($query)){
echo"<option value='$row[year]'>$row[year]</option>";}
?>
</select>
</fieldset>
<div class="clear"></div>
</div>
<div class="submit_link">
<input type="submit" name="s_t" value="Search" class="alt_btn">
</div>
</article>
</form>
<?php
if(isset($_POST['s_t'])){
$month=$_POST['month'];
$year=$_POST['year'];
$date = $month."-".$year;
$d=strtotime($date);
echo date("Y-m",$d);
}
?>
Exam: when i choice value = 2 in select 'month' and value = '2015' in select 'year' and submit form then result not = '2015-02' that result ='1970-01'. Why?

You are missing date part in your date parameter.
Try this:
$month=$_POST['month'];
$year=$_POST['year'];
$date = "1-" . $month."-".$year;
$d=strtotime($date);
echo date("Y-m",$d);

Related

Display unlimited row by random ID selected by user (created by php)

I need to display row from different table with random id that what i make it php generate it when i add new car or new driver
For example :
In this image table record's for same driver and different Car
and in this image the records for random id for the car
I need to show every records by date for same driver and Which car he use
Note: In this case its show like this
and I need it show like this
and sorry for arabic database records tell me if there anything not understood
my code:
<?php
require 'config.php';
$query = "SELECT * FROM `driversuser`";
$result1 = mysqli_query($con, $query);
?>
<div class="container">
<div class="addingcar">
<form dir="rtl" action="#" method="post">
<div class="">
<b>عرض وردية سائق</b>
</div>
<br/>
<label>
<b>اختر السائق</b>
<select name="insdname" id="framework" class="align-right selectpicker" data-live-search="true">
<option style="text-align: right" value="">اختر السائق ...</option>
<?php while($row1 = mysqli_fetch_array($result1)):; ?>
<option style="text-align: right" value="<?php echo $row1[10]; ?>"><?php echo $row1[1]; ?></option>
<?php endwhile; ?>
</select>
</label>
<br />
<label>
<b>من</b>
<input type="date" name="datefrom">
</label>
<br />
<label>
<b>الي</b>
<input type="date" name="dateto">
</label>
<br /><br />
<input type="hidden" name="hidden_framework" id="hidden_framework" />
<input type="submit" name="submit" class="btn btn-info" value="عــــرض" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['insdname'];
$date_val = $_POST['datefrom'];
$dateto_val = $_POST['dateto'];
$report = mysqli_query($con, "SELECT * FROM `reports` WHERE Datefrom BETWEEN '$date_val' AND '$dateto_val' AND DriverCode = '$selected_val' ORDER BY Datefrom");
while ($datareport = mysqli_fetch_array($report)) {
echo $datareport['Datefrom']." ".$datareport['DateTo']." ".$datareport['Price']." ".$datareport['PriceTaken']." ";
$report2 = mysqli_query($con, "SELECT * FROM `reports` WHERE DriverCode = '$selected_val' GROUP BY CarCode");
while ($datareport2 = mysqli_fetch_array($report2)) {
$carcode = $datareport2['CarCode'];
}
$report3 = mysqli_query($con, "SELECT * FROM `cars` WHERE Car_ID = '$carcode'");
while ($datareport3 = mysqli_fetch_array($report3)) {
echo $datareport3['CarName']." ".$datareport3['CarModel']." ".$datareport3['CarNumber']." ".$datareport3['CarColor']." ";
echo '<br/>';
}};
}
?>
</div>
</div>
I don't have your database schema, but I tried to create one for testing. So, try this:
SELECT * FROM reports r1 INNER JOIN cars c1 ON r1.carcode = c1.car_id ORDER BY r1.date
r1 is a shortcut for reports table ( change it to whatever you like ).
c1 is a shortcut for cars table ( also, you can change it ).
You can read more about INNER JOIN.

why Isn't my form working

I'm trying to use a listbox form to query the database but it's not showing anything. The idea is that I've queried the database to fill the form with the names of suburbs and then, selecting a suburb will query the database again to return the names of parks in that suburb. When I use the search form it doesn't return anything.
this is the form:
<p>Select Suburb to search</p>
<form method="post" action="suburb_search.php" id="search">
<select>
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
<input type="submit" name="search" value="Search" />
</form>
</div>
This is where it should use the results of the form to query the database but its not working:
<?php
$searchRequest = False;
if (isset($_GET['suburb'])){
$search = $_GET['suburb'];
$sql2 = "SELECT * FROM park_list WHERE suburb=$search";
$result2 = $db->query($sql2);
if($message){
echo "<p>$message</>";
} else {
?>
<div class="form">
<?php
while ($row2 = $result2->fetch_assoc()){
?>
<div class="results">
<h2><?php echo $row2['park_name'];?></h2>
<?php
}
}
} ?>
give a name for your select element as
<select name="suburb">
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
and you are giving form method as POST but accepting data in GET in your php code, change this
if (isset($_GET['suburb'])){
to
if (isset($_POST['suburb'])){

Dynamically displaying option values using PHP

I'm new to PHP and have created a very basic HTML form. As you can see in my form, the option values are all done by hand (there are more, I just simplified this example). What I want is for these to be generated dynamically using just PHP, so that I would physically have to add every single year etc.
I've done some searching but I can't seem to find exactly what I'm after so thought I'd ask here. From what I gather I need to create a query and echo out the option value somehow, although I'm not sure how to do this.
SELECT gameYear from games
I'd guess the above would be the correct query as all the form would need is the bookYear from the table?
<form id = "gameYear" method="get" action="result.php">
<label>
Game Year
<select name="gameYear">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>
Thanks, any help/guidance is appreciated.
<form id = "gameYear" method="get" action="result.php">
<label>
Game Year
<select name="gameYear">
<option value=''>--Select Year--</option>
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$SqlResult = mysqli_query($link, "SELECT gameYear from games");
while($Row = mysqli_fetch_array($SqlResult))
{
?>
<option value="<?php echo $Row['gameYear'] ?>"><?php echo $Row['gameYear'] ?></option>
}
?>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>
<?php $sql = "SELECT gameYear from games order by gameYear ASC";
$result = mysql_query($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); ?>
<form id="gameYear" method="get" action="result.php">
<label>Game Year
<select name="gameYear">
<?php while($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['gameYear'] ?>"><?php echo $row['gameYear']?></option>
<?php } ?>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>

Search form with php

I get the following form my database which contain for year 2011 week 1 to week 20 and for year 2012 currently week 1. I want my user to choose 2011 first and then call the week to choose for example so that they dont choose for example week 10 and year 2012( which are not yet available).Any help most welcome.
<form name="myform" action="http://www.website.com/displaybook.php" method="get">
<select size="1" name="d">
<?
$sql=mysql_query("SELECT DISTINCT (Week) FROM data ORDER BY Week ASC");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Week']."'>Week - ". $row['Week']."</option>";
}
?>
</select>
<select size="1" name="y">
<?
$sql=mysql_query("SELECT DISTINCT (Year) FROM data ORDER BY Year Desc");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Year']."'>Season - ". $row['Year']."</option>";
}
?>
</select>
<input type="submit" value="Get data">
</form>
You need to put the year select before the day select... It will ajust the GET string automaticaly... Then, you create a little javascript to enable the second select when the first is selected, like this:
<script type="text/javascript">
function getWeek()
{
if (document.getElementById('y').value != '')
{
document.getElementById('d').disabled = ''; // this will enable the select
}
}
</script>
And in your HTML:
<form name="myform" action="http://www.website.com/displaybook.php" method="get">
<select size="1" name="y" onchange="getWeek()">
<?
$sql=mysql_query("SELECT DISTINCT (Year) FROM data ORDER BY Year Desc");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Year']."'>Season - ". $row['Year']."</option>";
}
?>
</select>
<select size="1" name="d" id="d" disabled="disabled">
<?
$sql=mysql_query("SELECT DISTINCT (Week) FROM data ORDER BY Week ASC");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Week']."'>Week - ". $row['Week']."</option>";
}
?>
</select>
<input type="submit" value="Get data">
</form>
Something like this should work great!

handle info from select box

I have the following code:
<form name="votos" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php $categorias=mysql_query("SELECT * FROM categoria") or die (mysql_error()); ?>
<?php while($cat=mysql_fetch_array($categorias)){
echo "<h5>".$cat[0]." - ".$cat[1]."</h5>";
$nomeados=mysql_query("SELECT * FROM nomeados WHERE cod_categoria='$cat[0]'") or die(mysql_error());
?>
<div class="styled-select">
<select name="voto">
<option value=""></option>
<?php
while($nom=mysql_fetch_array($nomeados)){
$nomes=mysql_fetch_array(mysql_query("SELECT nome FROM logins WHERE cod_login='$nom[0]'")) or die (mysql_error());
?>
<option value="<?php echo $nom[0]; ?>"><?php echo $nomes[0]; ?></option>
<?php
}
?></select></div>
<?php }
?>
<input type="submit" class="botao" value="" name="submit" />
</form>
The code shows all the categories with the nomenies in a select box.
The thing is that the select box goes to post with the same name. I don't know how to get the information from all of the select boxes.
If you have more elements with the same name you can post them as an array
<select name="voto[]">
and than you get it in php
$votos = $_POST['voto']
foreach($votos as $voto){
//do what you need to do
}
and it's an array with all the values

Categories