I have to write a simple script for a reservations form.
The idea is to show Number of Adults and Children inputs for each room selected.
But if number of Children is entered, I need to show Age Field, for each Children.
Like this :
Number of Rooms - 2
Room-1
Adults: 2 Children: 2
Children Age[1] : 10 Children Age[2] : 15
--------------------------------------
Room-2
Adults: 2 Children: 1
Children Age[1] : 6
--------------------------------------
That's the idea (like Expedia Seach Box).
Here is my code, is almost working. Bue whe I select Children Ages, the With Adult and Children input disappears.
Here in my code:
enter code here
<lablel>ROOMS</lablel>
<select id="rooms" name="rooms" size="1"/>
<option val="">-?-</option>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
<option val="5">5</option>
</select>
<div id="showRoom1" style="display:<?php echo $selhab1 ;?>">Room [1]
<label>ADULTS</lablel><lablel>CHILDREN</lablel>
<select required id="seldos" name="adlr1" size="1">
<option value="">--?--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select id="child" name="chl" size="1" />
<option val="0">--</option>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
<option val="5">5</option>
</select>
<div id="show1" style="display:<?php echo $seled1 ;?>">Age Child [1]
<?php
echo'<select class="seldos" id=select name="age1" id="age1" >';
if($age1 == ''){
echo'<option value="--">-?-</option>';
}else{
print "<option value='$age1'>$edad1</option>";
}
for ($i = 1; $i <= $maxage; $i++) {
echo "<option value=$i>";
echo "$i</option>";
}
print"</select></div>";
?>
<div id="show2" style="display:<?php echo $seled2 ;?>">Age Child [2]
<?php
echo'<select class="seldos" name="edad2" id="age2">';
if($age2 == ''){
echo'<option value="--">-?-</option>';
}else{
print "<option value='$age2'>$age2 </option>";
}
for ($i = 1; $i <= $maxage; $i++) {
echo "<option value=$i>";
echo "$i</option>";
}
print"</select></div>";
?>
.
.
.
**** UNTIL CHILD AGE 5
</div>
<script>
$("#rooms").bind("change", function () {
var value = parseInt(this.value, 10);
$("div[id^='showRoom']").hide();
for(var i = 1; i <= value; i++) {
$("#showRoom" + i).fadeIn(300);
}
});
$("#child").bind("change", function () {
var value = parseInt(this.value, 10);
$("div[id^='show']").hide();
for(var i = 1; i <= value; i++) {
$("#show" + i).fadeIn(300);
}
});
</script>
I believe your issue is this line:
$("div[id^='show']").hide();
This is hiding the showRoom1 DIV which contains the adult and children inputs.
Related
I am creating this simple project that includes auto calculate of the price and quantity and then display it only after two < select > tags has been triggered,set or selected using AJAX without using submit button. I can achieve this but does not automatically calculates and display it without using submit button.
this is my code in HTML:
<form method="post">
<select name="days">
<option value="2">2 days</option>
<option value="3">3 days</option>
</select>
<select name="persons">
<option value="2">2 persons</option>
<option value="4">4 persons</option>
<option value="6">6 persons</option>
<option value="8">8 persons</option>
</select>
</form>
Assume that in my Database
day 2 == 1000
day 3 == 2000
this is my code in PHP:
if(isset($_POST['submit'])) {
$days = $_POST['days'];
$persons = $_POST['persons'];
// get database of the 'price' from rates tbl
$sql = "SELECT * FROM rates WHERE duration='$days'";
$res = mysqli_query($conn,$sql);
$rates_tbl = mysqli_fetch_assoc($res);
$rates = $rates_tbl['price'];
$total_price = ($rates*$persons);
echo 'total: ' . number_format($total_price) . '.00';
}
Should i use this type of ajax?? I dont even know if this is right.
<script type="text/javascript">
$.ajax({
url:"autocalculate.php",
method:"POST"
})
</script>
If the goal is to display the values on screen you can use this.
<form method="post">
<select name="days">
<option value="2">2 days</option>
<option value="3">3 days</option>
</select>
<select name="persons">
<option value="2">2 persons</option>
<option value="4">4 persons</option>
<option value="6">6 persons</option>
<option value="8">8 persons</option>
</select>
and your jQuery script
var readyToCalculate = false;
var optionChanges = 0;
$('select').on('change', function (e) {
if(readyToCalculate==true){
calculate();
} else {
optionChanges ++;
if (optionChanges==2){
readyToCalculate = true;
calculate();
}
}
});
function calculate(){
var val1 = parseInt($('select[name=days]').val());
var val2 = parseInt($('select[name=persons]').val());
var result = (val1 + val2);
$(".results").html(result);
}
I'm trying to take the value of SELECT option and use it in php variable, but can not figure out how. Would you assist me. Thank you.
Here is my code:
<select name="pcs" id="pcs">
<option class="option" value="8" selected class="selected">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
<option value="24">24</option>
<option value="24+">над 24</option>
</select></br>
My goal is, after taking the value to make some calculation with it and show results. Thank you for the help.
This is one option:
<select name="pcs" id="pcs" onchange="this.form.submit()">
<option class="option" value="8" selected">8</option>
<option value="12" <?php if($pcs =="12") { echo "selected"; } ?>>12</option>
<option value="16" <?php if($pcs =="16") { echo "selected"; } ?>>16</option>
<option value="20" <?php if($pcs =="20") { echo "selected"; } ?>>20</option>
<option value="24" <?php if($pcs =="24") { echo "selected"; } ?>>24</option>
<option value="24+" <?php if($pcs =="24+") { echo "selected"; } ?>>над 24</option>
</select></br>
But I do not want the page to reload.
Using jQuery get the select box value on change or submit, pass the value to a php file via Ajax say calculation.php, Do the necessary calculation and echo the result in calculation.php. Get the value and display inside DIV or desired place.
EXAMPLE BELOW:
main_file.php
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#pcs").on('change', function(){
var pcs_val = $(this).val();
$.ajax({
type: "POST",
url: "calculation.php",
data: { pcs: pcs_val},
success: function(theResponse) {
$('#outputDiv').html(theResponse);
}
});
});
});
</script>
<select name="pcs" id="pcs">
<option class="option" value="8" selected class="selected">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
<option value="24">24</option>
<option value="24+">над 24</option>
</select></br>
<div id="outputDiv">--output here--</div>
calculation.php
<?php
$pcs = isset($_POST['pcs']) ? $_POST['pcs'] : '';
echo 'Calculation Result = '.$pcs;
?>
Try the following without using AJAX:
<?php
$pcs_array = array('8'=>'8','12'=>'12','16'=>'16','20'=>'20','24'=>'24','24+'=>'над 24');
$select_pcs = '';
if(isset($_POST['pcs']) && !empty(isset($_POST['pcs']))){
$select_pcs = $_POST['pcs'];
}
if(!empty($pcs_array)){
?>
<select name="pcs" id="pcs">
<?php
$option_count = 0;
foreach($pcs_array as $k=>$v){
if(!empty($select_pcs) && $select_pcs == $k){
echo '<option class="option" value="'.$k.'" selected class="selected">'.$v.'</option>';
}elseif($option_count == 0 && empty($select_pcs)){
echo '<option class="option" value="'.$k.'" selected class="selected">'.$v.'</option>';
}else{
echo '<option value="'.$k.'">'.$v.'</option>';
}
$option_count++;
}
?>
</select></br>
<?php
}
?>
Good day! I am trying to create a "search by date" option in my website that will retrieve the records in the database whose dates matched the users' input. I have these select boxes:
<!-- FROM -->
<select name="from_month" id="from_month">
<option selected>-- MONTH --</option>
<option value="january">January</option>
...
<option value="december">December</option>
</select>
<select name="from_day" id="from_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="from_year" id="from_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- FROM (END) -->
<!-- TO -->
<select name="to_month" id="to_month">
<option selected>-- MONTH --</option>
<option value="1">January</option>
...
<option value="12">December</option>
</select>
<select name="to_day" id="to_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="to_year" id="to_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- TO (END) -->
What I originally plan to do is get all the "from_" values and store it into a variable '$from'. The same goes for the "to_". Because my database for dates looks like this:
id | title(text) | date_submitted(date) |
1 | Lorem Ipsum | 2015-01-20 |
Any help would be appreciated on how I would accomplish this. Thank you in advance!
Since you know every part of the date, MySQL's BETWEEN would do the trick:
SELECT * FROM db WHERE mydate BETWEEN 'from_date' AND 'to_date'
from_date and to_date are your variables, get through the form.
As #AnoopS stated, use numbers to value your month boxes.
And remember to prevent injections using mysqli or PDO with prepared statements.
I have this form but would like to get the total.
Please how can I calculate this array?
...While(ticket table row)
{
<input type="text" name="ticket[][price]" id="ticket[][price]" value="12">
<input type="text" name="ticket[][fee]" id="ticket[][fee]" value="3462">
<select name="ticket[][qty]">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
</select>
}
Eg. $total = ticket[][price] + ticket[][fee] * ticket[][qty];
Maybe something like this?
$total = 0;
foreach ($_POST['ticket'] as $ticket) {
$total += $ticket['price'] + $ticket['fee'] * $ticket['qty'];
}
print $total;
What I want to do is to create a feature that creates fields based on what the user selects
For example;
<select name="ex">
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
If the user selected 4, I want to be able to display 4 input fields
My question was, how would I construct a query that would do this?
I rattled my brain and all I could come up with was
if($ex='4') {
<input type="text" name="field4">
<input type="text" name="field4">
<input type="text" name="field4">
<input type="text" name="field4">
}
But there has to be an easier way of displaying them. Any ideas?
If would be easier if you use for loop , like as follows :
for($i=1;i<=$ex;$i++){
echo "<input type='text' name='field4[]'>";
}
I hope it helps.
You could even do it on the fly with javascript: http://jsfiddle.net/fudQF/
Javascript:
showFields(t) {
f = document.getElementById('fields');
while (f.childNodes.length >= 1)
f.removeChild(f.firstChild);
for(i = 0; i < t.value; i++) {
e = document.createElement('input');
e.setAttribute('type','text');
f.appendChild(e);
e = document.createElement('br');
f.appendChild(e);
}
}
HTML:
<select name="ex" onClick="showFields(this)">
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="fields">
</div>
for($i = 0; $ < 4; $i++)
{
echo "<input type=\"text\" name=\"field".($i + 1)."\">";
}
for ($i=1; $i<=$ex; $i++)
{
echo "<input type=\"text\" name=\"field".$i."\">";
}
It will give unique name to your fields
for($i=0;$i<$ex;$i++)
{echo '<input type="text" name="field'.$i.'">';}
Try this
You can do this using onchange function in javascript on select
function chang()
{
for(var i=1;i<document.getElementById("select_id").value;i++)
document.body.innerHTML+='<input type="text" name="field"'+i+'>';
}
That if statement seems to be PHP (dollar sign before the variable).. I guess you want to echo or save in a variable that output.
A way to to that would be to use a repetitive statement (for, while) to do that thing the number of times you need.
Another option that is based on the fact that you seem to use the exact same output would be to use the str_repeat() function.
That seems a bit strange.. maybe you want the name to be field4[] and not just field4
Suppose $ex=4
$ex=4;
for($C=1;$C<=$ex;$C++){ echo "<input type=\"text\" name=\"field".$C."\" />"; }
Building on an existing answer - jQuery could further bring down the amount of code needed -
HTML
<select name="ex" onClick="showFields(this)">
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="fields">
</div>
jQuery
$("select[name='ex']").on('change',function(){
var selectedVal = $(this).val();
$("#fields").html(''); // clear existing input fields (possibly from previous selection)
for (var i=1; i<=selectedVal; i++){
$("#fields").append('<input type="text" name="field'+i+'">');
}
});