I want choose month and year from the same column. How can I separate month and year from the column?
<label>Bulan :</label>
<select name="month">
<option value=""></option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mac</option>
<!-- ... -->
</select>
<label>Tahun :</label>
<select name="year">
<option value=""></option>
<option value="2017">2017</option>
<option value="2016">2016</option>
</select>
<input type="submit" value="Hantar" >
<?php
if(isset($_POST['year']) && ($_POST['month']))
{
$tarikh = mysql_real_escape_string($_POST['month']);
$tarikh = mysql_real_escape_string($_POST['year']);
I am running this query
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikh')
and (YEAR(tarikh) = '$tarikh')";
Why doesn't the output display? What is wrong with the above query?
in query string you use same variable for month and year
add different variable names in
$tarikhMonth = mysql_real_escape_string($_POST['month']);
$tarikhYear = mysql_real_escape_string($_POST['year']);
and
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikhMonth')
and (YEAR(tarikh) = '$tarikhYear')";
You are overwriting your $tarikh variable. Naming them $month and $year should work.
I would use PDO instead of trying to escape strings manually as well.
You need to have different variable names for the month and year. Try this
<label>Bulan :</label> <select name="month">
<option value=""></option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mac</option>...
</select><label>Tahun :</label> <select name="year">
<option value=""></option>
<option value="2017">2017</option>
<option value="2016">2016</option>
</select><input type="submit" value="Hantar" ></td>
<?php
if(isset($_POST['year']) && ($_POST['month']))
{
$tarikh_month = mysql_real_escape_string($_POST['month']);
$tarikh_year = mysql_real_escape_string($_POST['year']);
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikh_month')
and (YEAR(tarikh) = '$tarikh_year')";
For training purposes i need to make a function which tells me the 'travel cost' between 2 cities. The book tells me to type this function:
<?php
function travelcost($start, $destination)
{
$travelcost = array();
$travelcost[1] = array();
$travelcost[2] = array();
$travelcost[3] = array();
$travelcost[4] = array();
$travelcost[1][1] = 0;
$travelcost[1][2] = 30;
$travelcost[1][3] = 60;
$travelcost[1][4] = 90;
echo($travelcost[$start][$destination] . " Euro's");
}
?>
In addition i've created this form to ask for a start and a destination:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Start: <select name="start" value="true">
<option value="start[]">Amsterdam</option>
<option value="start[]">Utrecht</option>
<option value="start[]">Den Haag</option>
<option value="start[]">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="destination[]">Amsterdam</option>
<option value="destination[]">Utrecht</option>
<option value="destination[]">Den Haag</option>
<option value="destination[]">Rotterdam</option>
</select>
<p><input type="submit" name="calculate" value="Calculate"</p>
</form>
Followed by:
<?php
if(isset($_POST["start"])&& isset($_POST["destination"]))
{
travelcost($_POST['start'], $_POST['destination']);
}
?>
This gives me Undefined index: start[]
I know im doing it wrong, but i just can't see the logic in the function and the array. I assume the function is correct because it's right out of the book but i'm also not sure about that.
Can someone help me out?
This is wrong,
<option value="start[]">Amsterdam</option>
^ ^
It should be
<option value="start">Amsterdam</option>
or
<option value="Amsterdam">Amsterdam</option>
Same for all options in start and destination.
According to your function `travelcost(), your select should be
Start: <select name="start" value="true">
<option value="1">Amsterdam</option>
<option value="1">Utrecht</option>
<option value="1">Den Haag</option>
<option value="1">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="1">Amsterdam</option>
<option value="2">Utrecht</option>
<option value="3">Den Haag</option>
<option value="4">Rotterdam</option>
</select>
I have a HTML select list where one can chose an option.
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' selected>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
When this list is submitted via a HTML submit button, this list shows again in another page. Now, I want the option the user selected to be new selected value. I am doing this:
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
But values are appearing twice. The option the user selected is shown as the selected and still appears in the list. For example, we now have something like this:
0-50,000 (this is the selected value)
0-50,000
50,000-100,000
100,000-150,000
150,000-200,000
200,000 and above
How do I solve this?
In other page, make sure the selected option has selected="selected"
<select name='price' id='price'>\
<option value='' >Select....</option>
<option selected="selected" value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try this
/**
* Takes To values (First for option value Second for value to be compare)
* #param Srting $option stores Option Value String
* #param String $value Stores to be compare
* #return String
* #access public
*/
function selectBoxSelection($option, $value) {
if ($option == $value) {
return "selected";
}
}
<select name='price' id='price'>
<option value='0-50,000' <?php echo selectBoxSelection('0-50,000', $_POST['price']);?> >0-50,000</option>
<option value='50,000-100,000' <?php echo selectBoxSelection('50,000-100,000', $_POST['price']);?> >50,000-100,000</option>
<option value='100,000-150,000' <?php echo selectBoxSelection('100,000-150,000', $_POST['price']);?> >100,000-150,000</option>
<option value='150,000-200,000' <?php echo selectBoxSelection('150,000-200,000', $_POST['price']);?> >150,000-200,000</option>
<option value='200,000 and above' <?php echo selectBoxSelection('200,000 and above', $_POST['price']);?> >200,000 and above</option>
<option value='see all' <?php echo selectBoxSelection('see all', $_POST['price']);?> >See All</option>
</select>
instead of your code,
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try like this,
<select name='price' id='price'>
<option value='' >Select....</option>
<?php foreach ($arr as $key=>$value)
{
if($key == $_POST['price'])
{
echo "<option selected value=$key>$value</option>";
unset($arr[$key]);
}
else
echo "<option value=$key>$value</option>";
}?>
</select>
This will do it :
<form name="myform" method="post" action="testsel.php">
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="0-50,000") print "selected"; ?>>0-50,000</option>
<option value='50,000-100,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="50,000-100,000") print "selected"; ?>>50,000-100,000</option>
<option value='100,000-150,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="100,000-150,000") print "selected"; ?>>100,000-150,000</option>
<option value='150,000-200,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="150,000-200,000") print "selected"; ?>>150,000-200,000</option>
<option value='200,000 and above' <?php if(isset($_POST["price"]) && $_POST["price"]=="200,000 and above") print "selected"; ?>>200,000 and above</option>
<option value='see all' <?php if(isset($_POST["price"]) && $_POST["price"]=="see all") print "selected"; ?>>See All</option>
</select>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST["price"]))
print $_POST["price"];
?>
Save this file as "testsel.php" and view. It will serve the purpose.
<select name='price' id='price'>
<option value='' >Selec</option>
<option value='0-50,000'<?php if($_POST['price']=='0-50,000'){echo "selected==selected";}?>>0-50,000</option>
<option value='50,000-100,000'<?php if($_POST['price']=='50,000-100,000'){echo "selected==selected";}?>>50,000-100,000</option>
<option value='100,000-150,000'<?php if($_POST['price']=='100,000-150,000'){echo "selected==selected";}?>>100,000-150,000</option>
<option value='150,000-200,000'<?php if($_POST['price']=='150,000-200,000'){echo "selected==selected";}?>>150,000-200,000</option>
<option value='200,000 and above'<?php if($_POST['price']=='200,000 and above'){echo "selected==selected";}?>>200,000 and above</option>
<option value='see all'<?php if($_POST['price']=='see all'){echo "selected==selected";}?>>See All</option>
</select>