Multistep selectbox in HTML and Javascript - php

I want to create a html form with 2 nested select box,
And I want to do like that ,
1 selectbox , and user can select
Modern Or Ancient . I did it ;
If User Select ancient , new options are :
Age : (textfield)
Determined By: (text field)
Age By c-14 testing
And,
If C14 testing is performed
Name of the body performing the test
Date of testing
Here's my HTML so far:
<select name="storedinage" id="storedinage" onchange="showfield7(this.options[this.selectedIndex].value)">
<option>Please Select</option>
<option value="Modern" >Modern</option>
<option value="Ancient" >Ancient </option>
and my JavaScript is here:
<script type="text/javascript">
function showfield7(name){
if(name=='Ancient')document.getElementById('div7').innerHTML='Other: <input type="text" name="storedinage" />';
else document.getElementById('div7').innerHTML='';
}
</script>

Check jsfiddle here
I think this is what you wanted. Let me know if otherwise.
I am not sure how your html looks like so I made a bit of my own, please copy what is useful. Don't know either if you want labels on inputs or placeholders, you will have to do those small adjustments.
html
<form action="#" method="post">
<select name="storedinage" id="storedinage" onchange="showfield(this.options[this.selectedIndex].value)">
<option>Please Select</option>
<option value="Modern">Modern</option>
<option value="Ancient">Ancient</option>
</select>
<div id="div7" style="display:none;">
Age:<input type="text" name="storedinage" />
Determined By:<input type="text" name="DeterminedBy" />
Age By c-14 testing?<input type="checkbox" onchange="C14(this)" name="AgeByc-14testing" />
</div>
<div id="C14" style="display:none;">
<input type="text" name="NameBody" placeholder="Name of the body performing the test" />
<input type="text" name="TestingDate" placeholder="Date of testing" />
</div>
</form>
js
function showfield(name) {
console.log('!');
if (name == 'Ancient') {
console.log('passed');
document.getElementById('div7').style.display = 'inline';
} else {
document.getElementById('div7').style.display = 'none';
}
var checkbox = document.getElementById('C14');
if (checkbox.checked == true) {
console.log('true')
}
}
function C14(e) {
if (e.checked == true) {
document.getElementById('C14').style.display = 'inline';
}
}

Related

How to maintain the PHP GET value on the address bar if I use another GET?

I am passing a variable from page1 to page2 using GET and it's already working. However, I also use another get in the <select> <option> on page2 which means when I click an item in the option, the value I get from page1 disappears and becomes undefined since the address bar changes. I want the variable I get from page1 to be compared on SQL where clause. I can't think of a way on how to solve this, I'm not really knowledgeable in PHP, can you help me?
Here is my code(I remove some unnecessary lines): page1
<form method="get" action="home.php">
<div class="form-group">
<input id="codehe" class="form-control" type="codehe" name="codehe" placeholder="Class Code">
</div>
<div class="form-group">
<input class="form-control button" type="submit">
</div>
</form>
page2 (this is how i get the value from page1)
<?php
$codehe = $_POST['codehe'];
echo $codehe;
?>
The other GET (form) on page2
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
</form>
<?php
if(isset($_GET['state'])) {
$str = $_GET["state"];
$sql = "SELECT * FROM classvideo WHERE subjects = '$str' AND linkcode = ''";
?>
The traditional way to do things like this (without client-side code) is to put a hidden input in your form on page 2:
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
<!-- Add this line -->
<input type="hidden" name="codehe" value="<?php echo $_GET['codehe'] %>">
</form>

How can I get the values from the PHP generated forms?

I dont know where or how to use $_POST to get the values of the forms.
This is the whole code, I uploaded it on GitHub if you want to see the whole thing
This is the file I am trying to get the values out of.
new.php
<?php
if (!empty($_POST) and isset($_POST['actiune']))
{
$actiune = isset($_POST['actiune']) ? $_POST['actiune'] : NULL;
switch($actiune){
//CASE 1
case 'addElev': echo '<div class=dbFormText>Adauga elev</div><div
class="databaseform"><form action="" method="post">
<input type="text" name="numeElev" placeholder="Nume elev"/>
<input type="text" name="orasElev" placeholder="Localitate"/>
<select name="sexElev">
<option value="Baiat">Baiat</option>
<option value="Fata">Fata</option></select>
<input type="text" name="tlfElev" placeholder="Nr. de telefon"/>
<input type="text" name="birthdayElev" placeholder="Nastere(DD-MM-YYYY)"/>
<input type="submit" value="Submit">
</form></div>';
break;
}
}
?>
This is the page of the first dropdown, which gives me the value of the switch
adminpanel.html
<div class="dropdowns">
<form action="" method="post">
<select name="actiune" onchange="this.form.submit()">
<option>Selecteaza actiunea</option>
<option value="addElev">Adauga elev</option>
<option value="addNota">Adauga nota</option>
<option value="addAbsenta">Adauga absenta</option>
<option value="addTeza">Adauga teza</option>
<option value="deleteElev">Sterge elev</option>
<option value="deleteNota">Sterge nota</option>
<option value="deleteAbsenta">Sterge absenta</option>
<option value="deleteTeza">Sterge teza</option>
<option value="modifyElev">Modifica elev</option>
<option value="modifyTeza">Modifica teza</option>
<option value="modifyPurtare">Modifica nota purtare</option>
</select>
<?php include('new.php') ?>
</div>
When you submit a form that has a method of post, e.g.
<form method="post">
(note: action="" is redundant and can be left out)
the target page (in this case, itself) will be provided with the $_POST array.
You can dump the entire array to look at the values by using:
if (!empty($_POST) and isset($_POST['actiune']))
{
var_dump($_POST);
}
This array should contain all the values from within the <form> tag, provided a name has been set (which will be used as the 'key' in the array).
Not sure what is the switch for, added the fetch of postvars at the top of code. I am sure you can do some php to make sure vars are set. Also actiune is not a postvar that is its not the name of anyof the input fields so dont do $_POST['actiune']. it wont work. Else you have to add a hidden field, I did it here for you.
<?php
if (!empty($_POST) {
$numeElev = $_POST['numeElev'];
$orasElev = $_POST['orasElev'];
$sexElev = $_POST['sexElev'];
}
if (!empty($_POST) and isset($_POST['actiune']))
{
$actiune = isset($_POST['actiune']) ? $_POST['actiune'] : NULL;
switch($actiune){
//CASE 1
case 'addElev': echo '<div class=dbFormText>Adauga elev</div><div
class="databaseform"><form action="" method="post">
<input type="text" name="numeElev" placeholder="Nume elev"/>
<input type="text" name="orasElev" placeholder="Localitate"/>
<input type="hidden" name="actiune" placeholder="1"/>
<select name="sexElev">
<option value="Baiat">Baiat</option>
<option value="Fata">Fata</option></select>
<input type="text" name="tlfElev" placeholder="Nr. de telefon"/>
<input type="text" name="birthdayElev" placeholder="Nastere(DD-MM-YYYY)"/>
<input type="submit" value="Submit">
</form></div>';
break;
}
}
?>

html dropdown list options

I have the following drop down menu on my website
<select name="Templates" onchange="document.sendform.message.value = 'some text here'; return false;">
<option value="#" selected>----Templates----</option>
<option value="#">Absense</option>
<option value="#">Lates</option>
</select>
This currently sends text to a text area named "message" however I would like the text to be different for each option selected. Is there an easy way to do this at all?
Thanks!
Here it is:
http://jsfiddle.net/sinisake/3NQH7/
<form name="sendform">
<select name="Templates" onchange="document.sendform.message.value = this.value; return false;">
<option value="#" selected>----Templates----</option>
<option value="absense">Absense</option>
<option value="lates">Lates</option>
<input type="text" value="" name="message" />
</select>
</form>
the onchange event handler is what sends the text; if you want to send text based on the value of the selected option you should use a more advanced event handler:
document.TemplateForm.Templates.onchange = function () {
var value = this.value;
if (value === 'absence') {
setMessage('Absence message');
} else if (value === 'laters') {
setMessage('Laters message');
} else {
setMessage('Default message');
}
return false;
};
function setMessage(message) {
document.sendform.message.value = message;
}
I wrapped your select element in a form to be able to adress it simply in the JavaScript above:
<form name="TemplateForm">
<select name="Templates">
<option value="#" selected>----Templates----</option>
<option value="absence">Absense</option>
<option value="laters">Lates</option>
</select>
</form>
In order to see what value is selected I also added unique values for the options elements.
Live example: http://jsfiddle.net/fRB3s/1/

how to populate the textbox value on the selection of a combo box

I have a form which has one combo box and three textboxes. The three textboxes are meant for the calculation purpose. The first textbox is the amount, second one is received and the last one is read only which shows the balance and the combo has the values - full, part and none.
If the user selects full then the the received textbox should become read only and the amount entered in the amount textbox should come in the the received textbox. If the user selects part then the textbox should be available for the user to enter the amount. If the user selects none then the balance textbox should get populated with the value entered in the amount textbox.
<select id="pay" name="pay" tabindex="12">
<option value=""></option>
<option value="1">Full</option>
<option value="2">Part</option>
<option value="3">None</option>
</select>
<input type="text" id="amt" name="amt" value=""
size ="8" onblur="calculateText()"
style="background-color:transparent; color:blue; text-align:right"
tabindex="17"/>
<input type="text" id="resc" name="resc" value=""
disabled="disabled" size ="8" onblur="calculateText()"
tabindex="18"/>
<input type="text" id="bal" name="bal"
readonly="readonly" value="" size ="8"
onblur="calculateText()" tabindex="19"/>
Added jsFiddle: http://jsfiddle.net/MackieeE/fjXzY/1/
Here's something to get you started. It does more or less what you've asked, there are some usability issues because you haven't said what to do in every case, only a limited set of cases. I'll leave it up to you to get it working as you want.
<script>
function calculateText(el) {
var form = el.form;
var idx = form.pay.selectedIndex;
if (idx <= 0) {
form.reset();
return;
}
if (form.pay.value == 1) {
form.resc.disabled = true;
form.resc.value = form.amt.value;
form.bal.value = 0;
} else if (form.pay.value == 2) {
form.resc.disabled = false;
form.bal.value = form.amt.value - form.resc.value;
} else if (form.pay.value == 3) {
form.resc.disabled = true;
form.resc.value = 0;
form.bal.value = form.amt.value;
}
}
</script>
<form>
<select name="pay" onchange="calculateText(this);">
<option selected>Please select an option</option>
<option value="1">Full
<option value="2">Part
<option value="3">None
</select><br>
Amount: <input name="amt" onblur="calculateText(this)"><br>
Received: <input name="resc" disabled onblur="calculateText(this)"><br>
Balance: <input name="bal" readonly><br>
<input type="reset">
</form>
please write following code in javascript frame in your jsfiddle and check if that is you want ..
function UpdatePay( n ) {
if(n.options[n.selectedIndex].value==2){
document.getElementById('amt').style.display='none';
document.getElementById('resc').disabled = false;
}
if(document.getElementById('pay').value==3){
document.getElementById('bal').value=document.getElementById('amt').value
document.getElementById('resc').value=document.getElementById('amt').value
}
if(document.getElementById('pay').value==2){
document.getElementById('bal').value=document.getElementById('resc').value
}
} document.getElementById('resc').value=document.getElementById('amt').value
}
if(document.getElementById('pay').value==2){
document.getElementById('bal').value=document.getElementById('resc').value
}
}

How to change a text box to visible depending on what item is selected in a drop down menu?

I have a drop-down menu with a list of school subjects in it, the last option is "Add New Subject" so when the user selects that option, I need a text box to appear.
My HTML form looks like this: (I have only included the relevant bits, there is a bit more stuff on it.
<form method="post" action="createQuestion.php" name="cq">
<select name="subject" id="subject">
<option value="biology">Biology</option>
<option value="maths">Maths</option>
<option value="economics">Economics</option>
<option value="newSub" <?php if($subject == "newSub1") { echo "SELECTED"; } ?>>
Add New Subject
</option>
</select>
<input type="text" name="newSubtxt" ID="newSubtxt" style="visibility:hidden">
</form>
I have spent ages trying to get this to work with javascript, and I just can't get it to work at all.
This is the js I wrote, but doesn't work
function addSubject(){
const newSub = document.getElementById('newSub').name;
const selectedSubject = document.getElementById('subject').value;
if (selectedSubject === newSub){
document.getElementById(newSubtxt).style.display = 'block';
}
}
I would be so grateful if someone could help me out with the javascript, so that it will work. Thanks in advance :)
Change your invisible input to:
<input type="text" name="newSubtxt" id="newSubtxt" style="display: none" />
and your js:
function addSubject() {
var selectedSubject = document.getElementById('subject').value;
if (selectedSubject == 'newSub') {
document.getElementById('newSubtxt').style.display = 'block';
}
}
Also see my jsfiddle.
Change the style of textbox with
<input type="text" name="newSubtxt" ID="newSubtxt" style='display:none;'>
I have used jQuery (javascript library) to solve your problem.
$(document).ready(function () {
$('#subject').change(function (){
if($('#subject').val()=='newSub')
$('#newSubtxt').show();
else
$('#newSubtxt').hide();
});
});
See my jsfiddle

Categories