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.
Related
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
I have a form which submits data, and as a test I am attempting to first check if a variable is set on submit. If the variable is set, a text will be displayed stating something like "Variable is set". However if it isn't set, a text will be displayed saying "variable not set", once the variable hasn't been set it should then be set so next time when the form is submitted it displays variable is set however this is not working for me, for some reason it always displays that the variable is not set, my PHP code will be below:
<?php
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
I feel really stupid for not being able to do something which seems so easy, I am only just learning and sort of trying to teach myself, thank you for any help provided!!!
Working code and explanation:
<?php
$test="";
if (isset($_POST["text"])) { //always directly check $_POST,$_GET var without assigning
echo "This var is set";
$test=$_POST["text"]; // then assign
}
else{ // and use else clause for otherwise case
echo "This var is not set";
$test = 'set'; // AND if you want set to default/custom value in case of not set.
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off">
<br /><br />
<input type="submit" value="submit">
</form>
If you are using form to submit values, then try this one,
if (isset($_POST['text'])) {
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
Otherwise, If a variable set to empty value like $test = '';
(It means variable is set but it has no values) It will execute your first if condition only.
<?php
$test=$_GET["text"];
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="#" method="get">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
You haven't declared the variable $test.
Unless you've still got a bit of PHP somewhere that you haven't included here, your variable is empty. When a form is submitted, the input will be added to either the $_POST array (for method = "post") or else the $_GET array (for method = "get").
To Fix:
<?php
if (isset($_POST['text'])) {
$test = $_POST['text'];
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</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.
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]);
I'm trying to figure out how to make the logic in the following form work. Basically if either of the first two radio buttons is checked, make the hidden input named categories have a value of vegetables. Else, make the hidden input named categories have a value of fruits.
I'm not sure if this should be done with PHP or JavaScript, but if it is done with PHP I think the form would have to be submitted to itself to be pre-processed and then the collected, pre-processed information would be sent to external_form_processor.php. If this is how you do it, what would be the PHP code that I need to use to make it work?
<?php
if($_POST["food"]=="carrots" || $_POST["food"]=="peas") {
$category = "vegetables";
} else {
$category = "fruits";
}
?>
<form name="food_form" method="post" action="external_form_processor.php" >
<fieldset>
<input type="radio" id="carrots" name="food" value="carrots" />
<input type="radio" id="peas" name="food" value="peas" />
<input type="radio" id="orange" name="food" value="orange" />
<input type="radio" id="apple" name="food" value="apple" />
<input type="radio" id="cherry" name="food" value="cherry" />
</fieldset>
<input type="hidden" name="categories" value="<?php $category ?>" />
</form>
If using jQuery would be easier, how could I call the variable as the value of the hidden input if I use the following in the head of the page?
$(function(){
$('input[name=food]').click(function(){
var selected_id = $('input[name=food]:checked').attr('id');
if (selected_id == 'carrots' || selected_id == 'peas') {
var category = "vegetables";
} else {
var category = "fruits";
}
});
});
Any help with this would be greatly appreciated. Thanks!
I think jQuery would work perfect for you, you just need to pass the category value to the input field:
$(function(){
$('input[name=food]').click(function(){
var selected_id = $('input[name=food]:checked').attr('id');
if (selected_id == 'carrots' || selected_id == 'peas') {
var category = "vegetables";
} else {
var category = "fruits";
}
$('input[name=categories]').val(category);
});
});
I would set the category in PHP when the form is submitted.
//validate inputs... always
$food = "";
if(isset($_GET['food'])){
$food = preg_replace("/[^a-zA-Z]+/", "", $_GET['food']);
}
$category = ($food=="peas"||$food=="carrots")?"vegetables":"fruits";