How to get data from html forms - php

my html page has a number of textboxes (in div tags that show/hide) and I want the data in them to send to a text document once the submit button is clicked at the bottom.
I'm fairly new to html coding and have limited php/jquery experience so if theres an easy way to do this would be appreciated. Heres the current code:
My JS:
function showhide(layer_ref) {
var element = document.getElementById(layer_ref);
var state = element.style.display
if (state == 'block') {
state = 'none';
} else {
state = 'block';
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}
The HTML:
<form>
Name: <input type="text" name="name">
Date: <input type="numbers" name="date">
<p>Question 1</p>
<div id="div1" style="display: none;">
<img src="q1.jpg" alt="some_text">
<TEXTAREA Name="q1answer" ROWS=20 COLS=100></TEXTAREA>
</div>
<!-- same code pattern for div2 to div7 -->
<p>Question 8</p>
<div id="div8" style="display: none;">
<img src="q8.jpg" alt="some_text">
<TEXTAREA Name="q8answer" ROWS=20 COLS=100></TEXTAREA>
</div>
</form>

You could use php GET or POST to recive data from the HTML Form.
Here is a tutorial that could help you: http://www.tizag.com/phpT/postget.php

Change your HTML form tag into this:
<form action='page.php' method='post'>
Add a submit button before the end of form tag
<input type=submit name='submit' value='submit'>
Change the name tag of your name input field
<input type="text" name="name">
into (you cant use name as a name of the input)
<input type="text" name="myname">
And add this PHP code:
if(isset($_POST['submit'])){ //if button is clicked do this:
$name = $_GET['myname'];
$date = $_GET['date'];
$answer1 = $_GET['q1answer'];
$answer2 = $_GET['q2answer'];
$answer3 = $_GET['q3answer'];
$answer4 = $_GET['q4answer'];
$answer5 = $_GET['q5answer'];
$answer6 = $_GET['q6answer'];
$answer7 = $_GET['q7answer'];
$answer8 = $_GET['q8answer'];
}
else{
//if button is not clicked do this
}

Related

How to get the selected radio button value in Payment processing

I am trying to give payment processing option e wallet and cod in checkout page, with the help of radio button, but the selected value is not getting fetched in php. How can this be solved?
I could't upload my code, so I am explaining the required logic, please do help.
In payment process i need a radio button
Selected radio button value should be given to php, so that my further calculations will continue.
I am unable to get the value in to php.
CODE:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("input[name='cod']").click(function () {
if ($("#chkYes").is(":checked")) {
$("#dvPassport").show();
} else {
$("#dvPassport").hide();
}
});
});
</script>
<form method ="post">
<label for="chkYes">
<input type="radio" id="chkYes" name="cod" />
COD
</label>
<br/>
<label for="chkNo">
<input type="radio" id="chkNo" name="cod" />
E-Wallet
</label>
<hr />
<button type="submit" name="submit1" value="submit">Submit</button>
</form>
<div id="dvPassport" style="display: none">
<input type="text" id="txtPassportNumber" />
</div>
<?php
echo $com;
echo $grand_total;
if(isset($_POST['submit'])) {
if($grand_total <= $com) {
$com = $com - $grand_total;
$sql1 = mysqli_query($conn,"UPDATE commission SET total_commission=$com WHERE e_id = '".$_SESSION["e_id"]."'");
echo $com;
$total_pay = $com;
echo $total_pay;
} else {
$newcom = $grand_total - $com;
$sql2 = mysqli_query($conn,"UPDATE commission SET total_commission=$newcom WHERE e_id = '".$_SESSION["e_id"]."'");
$total_pay = $newcom;
echo $total_pay;
}
}
?>
It seems you haven't actually sent any variables to your php.
You can send the value of 'cod' by declaring a php var and assigning it the value via post, likewise for your txtPassportNumber, e.g.
$cod = $_POST['cod'];
$passNo = $_POST['txtPassportNumber'];
Include these php variables in your php file and then use them to continue with your calculations.
Hope this helps

php post data with appended radio input

First of all sorry for my English and im not an expert of HTML, JS or PHP.
I appended some radio inputs to a div inside a form.
The append process looks OK, but when i submit the form, i cant get the post data from the appended inputs.
<form method="POST" action="post.php">
<div id="categradio">
<input type='radio' name='catradio' value='opt1' id='cat1'></input><label for='cat1'>OPT1</label>
<input type='radio' name='catradio' value='opt2' id='cat2'></input><label for='cat2'>OPT2</label>
<input type='radio' name='catradio' value='opt3' id='cat3'></input><label for='cat3'>OPT3</label>
</div>
<div id="subcategradio"></div>
<input type="submit" value="Send"></input>
</form>
<script>
function updateSubCats(currcat) {
var data = [{"0":"Analisi","Categoria":"Analisi","1":null,"Sottocategoria":null},{"0":"Creazione","Categoria":"Creazione","1":"Pagina Web","Sottocategoria":"Pagina Web"},{"0":"Creazione","Categoria":"Creazione","1":"Tabella","Sottocategoria":"Tabella"},{"0":"Creazione","Categoria":"Creazione","1":"Applicazione","Sottocategoria":"Applicazione"},{"0":"Creazione","Categoria":"Creazione","1":"Query","Sottocategoria":"Query"},{"0":"Creazione","Categoria":"Creazione","1":"Database","Sottocategoria":"Database"},{"0":"Monitoraggio","Categoria":"Monitoraggio","1":null,"Sottocategoria":null},{"0":"Importazione","Categoria":"Importazione","1":null,"Sottocategoria":null},{"0":"Calcolo","Categoria":"Calcolo","1":null,"Sottocategoria":null}];
$('#subcategradio').empty();
for (var i in data) {
var cat = data[i][0];
var subcat = data[i][1];
if (cat == currcat && subcat != "" && subcat != null) {
$('#subcategradio').append("<input type='radio' name='subcatradio' value='"+subcat+"' id='subcat"+i+"'></input><label for='subcat"+i+"' class='nosel'>" + subcat + "</label>");
}
}
}
$('#categradio').change(function(){
var cat = $('#categradio input[type=radio]:checked').val();
updateSubCats(cat);
});
</script>
and the php file (post.php) to receive the post data:
<?php
$category = $_POST['catradio'];
$subcategory = $_POST['subcatradio'];
echo $category."<br>".$subcategory;
?>
i solved the problem adding an invisible input.
<input name="subcategory" class="invisible" value="" id="empty"/>
when the appended radios selection change, the value of the invisible input change, and when i submit the form i get the value of the invisible input.
thanks to everyone

Check whether input values are equal

I want to use this code for a barcode scanner as follows:
The scanned barcode is entered in the insert_code input and then I want to display "code is ok", when the value in search_code = insert_code.
My code, after validation, clears the input search_code and it is annoying to have to reintroduce the same code in search_code every time again.
What can I do to keep the value in search_code after each validation?
<form action="" method="post">
Cod I:<input type="text" name="search_code" value=""/><br/><br/>
Cod II:<input type="" name="insert_code" value=""/><br/><br/>
<input type="submit" name="button" value="validation" />
</form>
<?php
$search_code = $_POST ["search_code"];
$insert_code = $_POST ["insert_code"];
if ($search_code == $insert_code){
echo "code is ok";
} else {
echo "code is not ok";
}
?>
If you want to keep search_code input filled with the last submitted value, just echo the value of this post into the input if it was set:
<form action="" method="post">
Cod I:<input type="text" name="search_code" value="<?php echo isset($_POST['search_code'])?$_POST['search_code']:'' ?>"/><br/><br/>
Cod II:<input type="" name="insert_code" value=""/><br/><br/>
<input type="submit" name="button" value="validation" />
</form>
Also, to avoid warnings about undefined index, add this condition to your PHP code (check if those posts are set):
<?php
if(isset($_POST ["search_code"]) && isset($_POST ["insert_code"])){
$search_code = $_POST ["search_code"];
$insert_code = $_POST ["insert_code"];
if ($search_code == $insert_code){
echo "code is ok";
}else {
echo "code is not ok";
}
}
?>
You can do this without PHP, which will give a better user-experience. Here is a live example, just run to see it work:
// identify form elements:
var search_code = document.getElementById('search_code');
var insert_code = document.getElementById('insert_code');
var result = document.getElementById('result');
var button = document.getElementById('button');
// respond to button click
button.onclick = function validate() {
// show verification result:
result.textContent = search_code.value == insert_code.value
? 'code is ok'
: 'code is not ok';
// clear input when wrong:
if (search_code.value !== insert_code.value) {
insert_code.value = '';
}
return false;
};
insert_code.oninput = function () {
result.textContent = ''; // clear result;
};
<form action="" method="post">
Cod I:<input type="text" name="search_code" id="search_code" value=""/><br/><br/>
Cod II:<input type="" name="insert_code" id="insert_code" value=""/><br/><br/>
<input type="submit" id="button" name="button" value="validation" />
</form>
<div id="result"></div>
The test is done in Javascript, which responds to the button click and cancels the form's submission to the server (return false).
As a bonus the "ok/not ok" message is cleared as soon as you type a new value in the second input box.
How to use this code
Here is how the code should look in your document:
<body>
<form action="" method="post">
Cod I:<input type="text" name="search_code" id="search_code" value=""/><br/><br/>
Cod II:<input type="" name="insert_code" id="insert_code" value=""/><br/><br/>
<input type="submit" id="button" name="button" value="validation" />
</form>
<div id="result"></div>
<script>
// identify form elements:
var search_code = document.getElementById('search_code');
var insert_code = document.getElementById('insert_code');
var result = document.getElementById('result');
var button = document.getElementById('button');
// respond to button click
button.onclick = function validate() {
// show verification result:
result.textContent = search_code.value == insert_code.value
? 'code is ok'
: 'code is not ok';
// clear input when wrong:
if (search_code.value !== insert_code.value) {
insert_code.value = '';
}
return false;
};
insert_code.oninput = function () {
result.textContent = ''; // clear result;
};
</script>
</body>
Note that the content part has some differences to yours: every input has an id attribute, and there is an extra div.

Pass or Post the input value without using form

I'm working right now on how I can pass the value of <input id="sub" name="sub"> that has a value of first 3 letters of dropdown option text. I need to pass or post the value of <input id="sub" name="sub"> to process my query below.
When I choose for example the ITEquipment in dropdown, the <input name="sub"> get the first 3 letters of dropdown option text. So now it has the value of ITE, that ITE what I need in my query. In query for example, $sub=$POST["sub"] is equal to ITE so I used LIKE function, LIKE '$sub-__' . Hope you understand what I'm trying to do.
How I can do that without using submit form?
Here's my example process of what I'm working http://jsfiddle.net/xqGLS/1/
Help please?
<?php
$mysqli = new mysqli("localhost", "root", "", "2015");
$resultcode = $mysqli->query("SELECT category, id, maincode FROM category GROUP BY id ORDER BY maincode");
$code = '';
while($row = $resultcode->fetch_assoc())
{
$code .= '<option value = "'.$row['maincode'].'">'.$row['category'].'</option>';
}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
<br/>
Category
<select name="maincode" style="text-transform:uppercase;" onchange = "GetChangedValue(this);">
<option value=""></option>
<?php echo $code; ?>
</select>
</br>
Sub Code
<input type="" name="sub1code" id="sub1code" value="" readonly style="width:45px;text-transform:uppercase;">
<script>
$('[name="maincode"]').change(function() {
$('[name="sub1code"]').val($(this).val());
var input = $('[name="sub"],[name="sub1"]'),
input1 = $('[name="sub"]'),
input2 = $('[name="sub1"]'),
input3 = $('[name="equal"]');
input.change(function () {
input3.val(input1.val() + input2.val());
});
});
</script>
<script>
function GetChangedValue(e) {
var value = e.options[e.selectedIndex].text;
var elem = document.getElementById("sub"); elem.value = value.substring(0,3);
}
</script>
<input name="sub" id="sub" value="" style="width:35px;text-transform:uppercase;" readonly>
--My Query--
<input id="sub1" name="sub1" style="width:35px;text-transform:uppercase;" value='
<?php
$result2 = $mysqli->query("SELECT * FROM code
WHERE sub1code LIKE 'SUP-___' ORDER BY sub1code");
while($row = $result2->fetch_assoc())
{
$value = $row['sub1code'];
}
$first = substr($value, 0, 4);
echo $first;
$last = substr($value, -3);
$i="0";
while($i<=$last)
{
$i++;
}
$value2=strlen($i);
echo $first;
if($value2==1)
{
echo "00".$i;
}
elseif($value2==2)
{
echo "0".$i;
}
else
{
echo $i;
}
?>'>
<input id="equal" name="equal" value="" style="width:60px;text-transform:uppercase;" type="hidden">
<input type="submit" name="">
</form>
HEAVILY EDITED
$(document).ready(function(){
$("#dd").change(function(){
var v1 = $(this).val().substring(0,3);
$.ajax({
type : 'get',
url : 'call.php',
data : {varReq:v1}
}).done(function(returnResult){
alert(returnResult);
});
});
});
call.php would contain your php/mysql script. To get the value passed through there should be via $_GET or $_POST. But you have to include the proper type in your jquery's ajax too.
So in my example, What I have on my php, just a dummy one.
<?php
if(isset($_GET['varReq']))
{
if($_GET['varReq'] == 'ABC') echo "AAAA";
else echo "BBBB";
}
?>
It pass this html element...
<h1>Example</h1><p>BBBB</p>
Then pop up message (alert) will be called showing the return value.

submit multiple forms with single submit button in javascript

i have 1 php page containing 2 forms and 1 submit button.i want to submit both forms with this single button.My code works perfectly but there is 1 problem that in each form only 1 field submitted successfully. Below is my html and javascript code, plz tell me where i have error
2 html forms
<form name="form">
<input type="text" name="a" value="a">
<input type="text" name="b" value="b">
</form>
<form name="form">
<input type="text" name="c" value="c">
<input type="text" name="d" value="d">
</form>
<input type="submit" name="Submit" id="button" value="Submit" onClick="submitAllDocumentForms()">
Javascript code
<script language="javascript" type="text/javascript">
/* Collect all forms in document to one and post it */
function submitAllDocumentForms() {
var arrDocForms = document.getElementsByTagName('form');
var formCollector = document.createElement("form");
with(formCollector)
{
method = "post";
action = "test.php";
name = "formCollector";
id = "formCollector";
}
for(var ix=0;ix<arrDocForms.length;ix++) {
appendFormVals2Form(arrDocForms[ix], formCollector);
}
document.body.appendChild(formCollector);
formCollector.submit();
}
/* Function: add all elements from ``frmCollectFrom´´ and append them to ``frmCollector´´ before returning ``frmCollector´´*/
function appendFormVals2Form(frmCollectFrom, frmCollector) {
var frm = frmCollectFrom.elements;
for(var ix = 0 ; ix < frm.length ; ix++)
frmCollector.appendChild(frm[ix]);
return frmCollector;
}
</script>
My php code to echo submitted values
<?php
echo $_POST['a'];
echo $_POST['b'];
echo $_POST['c'];
echo $_POST['d'];
?>
The problem is that appendChild() takes the element away from the form, modifying the elements array as well as its length. To avoid this, you can e.g. store the number of elements in a variable and process the array of elements starting from the last element:
var frm = frmCollectFrom.elements;
var nElems = frm.length;
for(var ix = nElems - 1; ix >= 0 ; ix--)
frmCollector.appendChild(frm[ix]);

Categories