My form is looking like this.
<form action="add_customers.php" method="post" onsubmit="MM_validateForm('name','','R','passport_no','','R','remarks','','R');return document.MM_returnValue">
<tr>
<td>Name of the Applicant:</td>
<td><label for="name"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>Country applying for :</td>
<td><label for="country"></label>
<select name="country" id="country">
<option>Singapore</option>
<option>Malaysia</option>
<option>Istanbul</option>
<option>Thailand</option>
<option>China</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td>Visa Categeory :</td>
<td><label for="visa_categeory"></label>
<select name="visa_categeory" id="visa_categeory">
<option>Visit</option>
<option>Business</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td>Passport No:</td>
<td><input type="text" name="passport_no" id="passport_no" /></td>
</tr>
<tr>
<td>Remarks:</td>
<td><label for="remarks"></label>
<textarea name="remarks" id="remarks" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr></form>
Now My problem is
1) when a user select other option from Visa Category then automatically display a text box for user and capture that what he enter.
2) save that details into mysql database
Used languages are PHP,MYSQL
Thanks.
You can add a text box in your form and make it hidden first time.
Get the textbox visible on change of the dropdown you have mentioned using javascript.
You can get the value of the textbox in the page where you accepts the data and insert into data database in add_customers.php along with other variables.
<script>
function visacatOnchange(){
var visa_cat = document.getElementById('visa_categeory').value
if(visa_cat == "Visit")
document.getElementById("new_textbox").style.display="block";
else
document.getElementById("new_textbox").style.display="none";
}
</script>
In html form add
<input type="text" name="new_textbox" id="new_textbox" style="display:none;">
and change
<select name="visa_categeory" id="visa_categeory" onChange="visacatOnchange();">
Good luck :-)
Initially you can keep the textbox div hidden and then display it on "onchange" event of select input.
<div id="textboxdiv" style="visibility: hidden">
Visa Option <input type="text" id="someid"/>
</div>
<select name="visa_categeory" id="visa_categeory" onchange="showTextBox()" >
<option>Visit</option>
<option>Business</option>
<option value="99" >Other</option>
</select>
Using Jquery you can display it like this:-
function showTextBox(){
if ($('#visa_categeory').val() == '99') {
$('#textboxdiv').css({'visibility':'visible'});
}
Here's some basic JavaScript/jQuery that should help you solve your first problem: http://jsfiddle.net/j4aXQ/
HTML
<form>
<select name="visa_category">
<option>Visit</option>
<option>Business</option>
<option>Other</option>
</select>
</form>
JS
$('select').change(function() {
var $selected = $('select option:selected');
if ('Other' === $selected.text()) {
$('form').append($('<input>').prop({'type':'text','id':'visa_other','name':'visa_other'}));
$('form').append($('<label>').prop({'for':'visa_other'}).html('Testing'));
} else {
$('input').remove();
$('label').remove();
}
});
The above code will only display a text box (and accompanying label) if the "Other" option is selected, and will then remove that text box if a different option is subsequently picked.
Hope that helps!
Related
I want make option selected in dropdown html form, i am fetching data from database but into the html select cant get how to option selected
Below is the My database table data get. from that account holder is the my database value. I want to compare with select option nd make then selected into dropdown.
Array
(
[ipsf_public_pf_id] => 62
[ipsf_main_id] => 216
[account_no] => 54545455
[account_holder] => self
[pf_amt] => 1500.00
[date_of_payment] => 2020-11-15
)
and my html php form like this
$str .='<input type="hidden" name="ipsf_public_pf_id" value="<?php echo $ipsf_public_pf; ?>">
<table align="center" cellspacing="1" class="table table-bordered">
<tbody>
<tr>
<td>PPF A/c No</td>
<td>Account Holder</td>
<td>Date of Payment</td>
<td>Amount</td>
</tr>
<tr>
<td>
<input name="account_no_'.$ppf.'" type="text" id="account_no_'.$ppf.'" value="'.$account_no.'" size="20" maxlength="20" class="form-control" onkeyup="Validate_number(this)"/>
</td>
<td>
<select name="account_holder_'.$ppf.'" id="account_holder_'.$ppf.'" class="form-control">
<option value="" selected>Account Holder</option>
<option value="self">Self</option>
<option value="spouse">Spouse</option>
<option value="children">Children</option>
<!--<option value="others">Others</option>-->
</select>
</td>
<td>
<input name="date_of_payment_date_'.$ppf.'" id="date_of_payment_date_'.$ppf.'" maxlength="2" style="width: 50px;" type="text" onClick="this.value="";" value="'.$date.'" class="form-control-date" onkeyup="Validate_number(this)"/>-<input name="date_of_payment_month_'.$ppf.'" id="date_of_payment_month_'.$ppf.'" maxlength="2" style="width: 50px;" type="text" onClick="this.value="";" value="'.$month.'" class="form-control-date" onkeyup="Validate_number(this)"/>-<input name="date_of_payment_year_'.$ppf.'" id="date_of_payment_year_'.$ppf.'" maxlength="4" style="width: 65px;" type="text" onClick="this.value="";" value="'.$year.'" class="form-control-date" onkeyup="Validate_number(this)"/>
</td>
<td>
<input name="ppf_amt_'.$ppf.'" type="text" id="ppf_amt_'.$ppf.'" value="'.$pf_amt.'" size="10" onBlur="javascript:increment_ppf_amnt(this.value, '.$ppf.');" maxlength="14" class="form-control-date" onkeyup="Validate_number(this)"/>
<input type="hidden" name="prev_ppf_amount_'.$ppf.'" id="prev_ppf_amount_'.$ppf.'" value="'.$ppf.'"/>
<input type="hidden" name="is_added_0" id="is_added_0" value="no"/>
</td>
</tr>
</tbody>
<input type="hidden" name="total_ppf_amount" id="total_ppf_amount" value="0"/>
</table>';
I am confused how to selected that option inside .
Anyone know then please help me
On your html just fill the selected="<?=$selectedOption;?>.
Basically the logic behind it is: you fetch the data that you want to, and the values thaty ou on option tags you will gonna use the selected id / value or whatever that you want to use for in the selected atrribute on the <select></<select> html tag.
I am trying to post the name="pricetag" and name="size" which is in the item and price... I know this is not working because I have use echo but nothing is coming out but if I echo the pid of the product I shows. The codes below
<form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
<table>
<tr>
<td width="160">Price:</td>
<td name="pricetag" id="pricetag">£25.00</td>
</tr>
<tr>
<td width="160">Item:</td>
<td name="size" id="item">12 inch</td>
</tr>
<tr>
<td width="160">Length*:</td>
<td>
<select name="length" onChange="addCharge(this, this.selectedIndex);" id="priceDropDown" class="small">
<option value="£25.00">12 inch</option>
<option value="£30.00">14 inch (+£30.00)</option>
<option value="£35.00">16 inch (+£35.00)</option>
<option value="£40.00">18 inch (+£40.00)</option>
<option value="£45.00">20 inch (+£45.00)</option>
<option value="£55.00">22 inch (+£55.00)</option>
<option value="£60.00">24 inch (+£60.00)</option>
<option value="£70.00">26 inch (+£70.00)</option>
<option value="£80.00">28 inch (+£80.00)</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
<input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
</form>
click here for live code http://jsfiddle.net/J4rXX/5/ by Ghillied
It doesn't POST because forms don't submit contents from TD elements. Only from INPUTs, SELECTs and TEXTAREAs.
More information on HTML FORMs here. If you want to avoid displaying the same data twice, you should try INPUT type=hidden.
<table>
<tr>
<td>Pricetag</td><td>PRICE_HERE</td>
</tr>
</table>
<form>
<input type="hidden" name="pricetag" value="PRICE_HERE" />
...
</form>
This should do nothing to hurt your existing JS scripts.
The only information that is posted to the server from a HTML form are elements such as INPUT, SELECT, and TEXTAREA.
If you want to post additional data, but make it un-editable to the user, you need to ideally put the data into INPUT elements which have the attributes type="hidden". This will post the data from the field but make them hidden to the user posting the form.
Adding the elements anywhere in the form will work. I've positioned them at the bottom of the form element, with the already existing hidden field, in this snippet.
I've also removed the name attributes from the TD elements in the snippet, to conform with web standards.
<form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
<table>
<tr>
<td width="160">Price:</td>
<td id="pricetag">£25.00</td>
</tr>
<tr>
<td width="160">Item:</td>
<td id="item">12 inch</td>
</tr>
<tr>
<td width="160">Length*:</td>
<td>
<select name="length" onChange="addCharge(this,this.selectedIndex);" id="priceDropDown" class="small">
<option value="£25.00">12 inch</option>
<option value="£30.00">14 inch (+£30.00)</option>
<option value="£35.00">16 inch (+£35.00)</option>
<option value="£40.00">18 inch (+£40.00)</option>
<option value="£45.00">20 inch (+£45.00)</option>
<option value="£55.00">22 inch (+£55.00)</option>
<option value="£60.00">24 inch (+£60.00)</option>
<option value="£70.00">26 inch (+£70.00)</option>
<option value="£80.00">28 inch (+£80.00)</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pricetag" value="£25.00" id="hiddenpricetag" />
<input type="hidden" name="size" value="12 inch" id="hiddensize" />
<input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
<input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
</form>
EDIT:
Just noticed that the form uses javascript, upon the change of the SELECT data, to change the TD elements HTML data.
To make sure that the form posted the correct data that is in the TD elements, you would need to add ID tags to the hidden input fields so that you can program the javascript to update these as well as the TD elements.
I've added the IDs hiddenpricetag and hiddensize to the two hidden fields.
Your javascript function should be somewhat like this:
function addCharge(select, elemIndex)
{
var item = select.getElementsByTagName('option')[elemIndex];
var price = document.getElementById('pricetag');
var sizeDOM = document.getElementById('item');
var hiddenpricetag = document.getElementById('hiddenpricetag');
var hiddensize = document.getElementById('hiddensize');
var finalPrice = "";
/* Getting the price and showing it up */
finalPrice = parseFloat(item.text.substring(11, item.text.length-1));
/* We set a default £25.00 value */
if(isNaN(finalPrice)) finalPrice = "25";
/* we add decimal */
price.innerHTML="£"+finalPrice+".00";
/* Getting the size and showing it up */
var size = item.text.substring(0,7);
sizeDOM.innerHTML = size;
/* Altering the hidden field values */
hiddenpricetag.setAttribute("value", "£"+finalPrice+".00");
hiddensize.setAttribute("value", size);
}
JSFiddle can be found here: http://jsfiddle.net/J4rXX/7/
<div id="form1" />
<form name="choice">
<table>
<tr>
<td>
Number of new types you want to add
</td>
<td>
<select name="<?php echo $num; ?>">
<option value="1" selected>1 row</option>
<option value="2">2 rows</option>
<option value="3">3 rows</option>
<option value="4">4 rows</option>
<option value="5">5 rows</option>
</select>
<div class='inputs'></div>
</td>
</tr>
</table>
</form>
<form name="myForm" action="addAccessories.php" method="post" onsubmit="return validateForm(this);">
<table border="1">
<tr>
<th>Barcode<em>*</em></th>
<th>Description<em>*</em></th>
<th>Minimum required stock<em>*</em></th>
<th>Current stock</th>
</tr>
<?php
echo $num;
?>
<tr>
<td><input type="text" name="bar_code/></td>
<div class="ui-widget">
<td><input name="description/></td>
</div>
<td><input type="text" name="minimum_required_stock" /></td>
<td><input type="text" name="num_stock" value="0"></td>
<tr>
<td> </td>
<td> <button data-theme="b" id="submit" type="submit">Add accessories</button></td>
</tr>
</table>
</form>
Hello guys,
In this code I have 2 form, 1 is the drop box has 5 options and a form for text input.
In theory, if I choose option value="3", the text form will be loop 3 times and get 3 set of text inputs. However, I haven't succeeded in doing this by far. And also, how can I access these 3 sets of data in addAccessories.php file?
I appreciate any opinion on this topic.
Thank you very much.
Reading your question, it sounds like you want to have code such that when the selection in form choice changes, you change the number of rows in form myform. This is not possible from php because it will be a runtime change on the client machine. You can write this in javascript. You will need code called from form choice that adjusts the DOM in form myform.
You can either use a pure PHP solution and rebuild the page - but that's clunky and slow (which, might be fine for a pet project). Normally we'd use Javascript with AJAX to accomplish something like this.
Up to this point I've avoided javascript, mostly because I'm a newbie. But I think I can't avoid it anymore?
I have a simple form in a table that I process with php.
<table id="my-table">
<thead>
<tr>
<td>Name</td>
<td>Quality</td>
<td>Status</td>
<td>User</td>
<td>Password</td>
<td>IP</td>
<td>Port</td>
<td>Options</td>
</tr>
</thead>
<form action="<?php htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<tbody>
<tr>
<td>
<input type="text" size="18" maxlength="32" name="add_name" value="Enter name" />
</td>
<td>
<select name="add_quality">
<option value='HIGH' selected='selected'>High</option>
<option value='MEDIUM'>Medium</option>
<option value='MOBILE'>Mobile</option>
</select>
</td>
<td>
<select name="add_status">
<option value='ENABLED' selected='selected'>Enabled</option>
<option value='DISABLED'>Disabled</option>
</select>
</td>
<td>
<input type="text" size="14" maxlength="16" name="add_user" value="Enter username" />
</td>
<td>
<input type="password" size="12" maxlength="16" name="add_pass" />
</td>
<td>
<input type="text" size="10" maxlength="16" name="add_ip" value="Enter IP" />
</td>
<td>
<input type="text" size="12" maxlength="6" name="add_port" value="Enter Port #" />
</td>
<td>
<input type="submit" name="add" value="Add" />
</td>
</tr>
</tbody>
</form>
</table>
I'd like to take this table and only keep the first three input types shown (Name, Quality, Status). The rest would open up if you hit a "details" link or button. So the table would expand (or ideally go to the next line as this table is too long). That details button or link would have to toggle on and off. Any ideas would be appreciated.
Basic javascript approach:
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display === 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
Then place a button or link with:
<input type="button" value="Details" onClick="toggle_visibility('detailsWrapper')">
Finally, just make a div or maybe a new tr around the fields you want to hide with the id set to "detailsWrapper" and style it with display:none.
Alternatively you can use this function:
function toggle_visibility(id) {
var control = document.getElementById(controlId);
if(control.style.visibility == "visible" || control.style.visibility == "")
control.style.visibility = "hidden";
else
control.style.visibility = "visible";
}
If I remember correctly this will reserve the needed space to display the element, so it won't mess up your layout.
For animations, jQuery is my favorite JavaScript library. It makes things quite a lot easier.
This code might work for you:
$('#my-table tr td').each(function(i) {
if (i >= 3) {
$(this).hide();
}
});
$('#details').click(function() {
$('#my-table tr td').each(function(i) {
if (i >= 3) {
$(this).slideToggle();
}
});
});
I'm not much of a web programmer, but I'm creating a simple web app that has a form where the user can enter a series of points (x,y,z) but I don't know how many the user is going to enter. I don't want to guess the probable maximum (100 maybe?) and put 100 fields on the form because it would look ugly. What's the easiest way to add more fields (or unhide fields) as the user enters data without contacting the server.
Currently I'm just using html & php, but I assume to do this I'll need javascript?
Currently my code looks like this, as the user enters data, I want another row to appear.
<form action="edit.php" method="post">
<table border=1>
<tr>
<td>
<td>X
<td>Y
<td>Z
</tr>
<tr>
<td>1</td>
<td><input type=text name=x1 size=10 value="0.4318"></td>
<td><input type=text name=y1 size=10 value="0.0000"></td>
<td><input type=text name=z1 size=10 value="0.1842"></td>
</tr>
<tr>
<td>2</td>
<td><input type=text name=x2 size=10 value="0.4318"></td>
<td><input type=text name=y2 size=10 value="0.0000"></td>
<td><input type=text name=z2 size=10 value="-0.1842"></td>
</tr>
<tr>
<td>3</td>
<td><input type=text name=x3 size=10 value="-0.3556"></td>
<td><input type=text name=y3 size=10 value="0.0000"></td>
<td><input type=text name=z3 size=10 value="0.1636"></td>
</tr>
... I want more to appear here...
</table>
<button name="submit" value="submit" type="submit">Save</button>
</form>
Any idea the easiest way? Thanks...
I would use jQuery and append the new inputs.
You will most likely have to use javascript, yes. You can use this or write your own using it as a reference:
http://www.mredkj.com/tutorials/tableaddrow.html
1: HTML :
<div class="form-group app-edu">
<label for="Example Details" class="col-xs-3 col-sm-2 control- label">Example Details</label>
<div class="form-group add-field">
<div class="user">
<select name="xx[]" id="xx" required>
<option value="">Education</option>
<option value="Class 10">Class 10</option>
<option value="Class 12">Class 12</option>
<option value="Diploma">Diploma</option>
<option value="PG Diploma">PG Diploma</option>
<option value="Bachelors Degree">Bachelors Degree</option>
<option value="Masters Degree">Masters Degree</option>
<option value="Other Certifications">Other Certifications</option>
</select>
<input type="text" placeholder="Name of Board" name="xx[]" id="xx" required>
<input type="text" placeholder="Name of Institute" name="xx[]" required id="xx">
<input type="text" placeholder="xx" name="xx[]" required id="xx">
<select name="xx[]" id="xx" required>
<option value="">xx</option>
<option value="xx">xx</option>
<option value="xx">xx</option>
<option value="xx">xx</option>
</select>
<input type="text" placeholder="Year and Month of Exam" name="date[]" required id="date" autocomplete="off">
</div>
<div class="add-more"><span>+</span>Add More</div>
</div>
</div>
2: PHP
if(isset($_POST['submit']))
{
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$query=mysql_query("INSERT INTO `xxx` (`xx`, `xxx`, `xxx`) VALUES ('NULL', '$xx','$xx','$xx')");
}
3: JS
var data_fo = $('.add-field').html();
var sd = '<div class="remove-add-more">Remove</div>';
var data_combine = data_fo.concat(sd);
var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".user"); //Fields wrapper
var add_button = $(".add-more"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append(data_combine); //add input box
//$(wrapper).append('<div class="remove-add-more">Remove</div>')
}
// console.log(data_fo);
});
$(wrapper).on("click",".remove-add-more", function(e){ //user click on remove text
e.preventDefault();
$(this).prev('.user').remove();
$(this).remove();
x--;
})
What you're saying is that you're hand writing the input tags? Or are you saying that you want a dynamic action where a user clicks a button and it adds more table rows?
In anycase, for your code, you just need a loop, like so. I assume $data is whatever data you want to set based on an array that is probably from the database or something:
<?php
for($i=0, $iMaxSize=count($data); $i<$iMaxSize; $i++)
{
?>
<tr>
<td><?= $i+1 ?></td>
<td><input type=text name=x1 size=10 value="<?=$data[$i]['something']"></td>
<td><input type=text name=y1 size=10 value="<?=$data[$i]['somethingelse']"></td>
<td><input type=text name=z1 size=10 value="<?=$data[$i]['somethingelseagain']"></td>
</tr>
<?php
} // end for
?>
Of course you can't copy and past the above, but that's a good starting point.
For dynamically doing it, you can't use php. What it sounds like you want to use is javascript ajax, and php combination.
Appends some HTML to all paragraphs.
<!DOCTYPE html>
<html>
<head>
<style>
p { background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<p>I would like to say: </p>
<script>
$("p").append("<strong>Hello</`enter code here`strong>");
</script>
</body>
</html>