php


Reading and printing user inputs in HTML in PHP


I have got a form (php in html or the other way around). Once user selects an option in a drop down list, it would get the input value and create a few text boxes. It seems like I have to use onchange(). How do I read the input and perform logics within the script inself? Instead of opening another .php script?
Currently this is what I have.
<?php
$tables = $_POST["tables"];
?>
<html>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Table Name: <div id="tables">
<select name="tables">
<option value="Applications">Application</option>
<option value="Device">Device</option>
</select>
</div>
</form>
<?
echo "".$tables."";
?>
You can't interact with PHP once the HTML is sent to the browser without either
Refreshing the page, or
Using AJAX (JavaScript).
If you know the options in the <select> beforehand (which it seems like you do), you should write some JavaScript to accomplish what you need. Here is a simple example using jQuery.
$('#tables_select').change(
function( eventObject ){
alert('You chose ' + $(this).val());
switch( $( this ).val())
{
case 'Applications':
$('#tables').append('<input type="text" name="application_name" value="Enter an application name" />"');
break;
case 'Device':
$('#tables').append('<input type="text" name="device_name" value="Enter a device name" />"');
break;
}
}
);
You will need to add additional logic to remove the inserted elements if the user changes their choice, and to insert the correct <input> elements when the page first loads, but it is a good starting point.
if you want to add any input type ... here is the demo demo with code
you use following method.
<form method="post" action="<?php echo $PHP_SELF;?>" onChange="return createTxtbox()">
Table Name: <div id="tables">
<select name="tables">
<option value="Applications">Application</option>
<option value="Device">Device</option>
</select>
</div>
<span id="fooBar"> </span>
</form>
then write javascript,
<SCRIPT language="javascript">
function createTxtbox() {
var type="text";
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
}
</SCRIPT>

Related Links

Automatic Reinitialisation in PHP
How to import content from Excel file into Database by Laravel 5.2?
$_GET['id'] is not receiving id value [closed]
php SESSION gets destroyed after redirection
doing a regex on an array and storing in another array
php - using strtotime date goes to 1970s
Sort multidimensional array according to sub index
WordPress : Need to change text color from a PHP file
Pass a variable with JQuery to the Controller - PHP Codeigniter
How to make php code “echo 62837 << -16;” return same result between windows and linux?
Listbox items in edit mode in php
Setting a session variable for use in Symfony controller PHPUnit tests with redirects
Argument 1 passed to ::__construct() must be an instance of DateTimeInterface
Use PHP/Apache to restrict access to images
Codeigniter HMVC Error Too Many Redirects on WHMCS
How would you write a complex query in laravel 5?

Categories

HOME
memory
apache-nifi
generics
jms
d3.js
scroll
foaf
hid
ios-simulator
autocomplete
tabs
dronekit-python
tcp
gallery
tfs2015
iptables
material-components
dji-sdk
firebase-dynamic-links
native-base
angular2-template
responsive-design
elastic-load-balancer
siesta
nexus3
parsley.js
complexity-theory
distributed-computing
vmware-workstation
parceler
opam
yii2-basic-app
bosun
maven-surefire-plugin
liferay-6.2
webviewclient
offline-caching
msdeploy
mdns
windowsiot
shapeless
cloudera-manager
ms-media-foundation
nest
buildforge
jslint
installanywhere
macports
variable-variables
xll
selenide
suds
slideshow
grails3.2.0
hdiv
angular-datatables
fiware-wirecloud
latexml
kitura
tinyioc
android-5.0-lollipop
photoswipe
non-linear-regression
renderman
word-2016
researchkit
nclam
s-function
jpda
data-management
msbuild-4.0
jenkins-workflow
codecharge
django-redis
gridbaglayout
flask-security
teensy
json-spirit
self
chicagoboss
replicaset
erlog
kotlin-android-extensions
deque
mxe
tidyr
subview
nanomsg
zend-mail
coding-efficiency
neo4jphp
hyperloglog
nodeload
wso2bam
oxygene
iosched
spring-security-acl
facebook-game-groups
eclim
xsd2code
jacob
pacman
microformats
bcdedit
notin
spiral
ntlmv2
significant-digits
gprof
websphere-6.1
cakeyframeanimation
expression-web
getopts
database-deadlocks
android-2.1-eclair
iphone-sdk-4.3
eventaggregator
projectpier
rendering-engine

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App