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
Related
i have a form of multiple checkboxes. but i want to retain the checkbox checked but reset it after form submit. here is my code.
<?php
if(isset($_POST['query'])){
$searched = $_POST['query'];
$conn = mysqli_connect('localhost','root','','orderqueing') or die (mysqli_error());
$query = "SELECT * FROM product_tbl WHERE itemName LIKE '%$searched%'";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
echo '<label style="float:left" class="option_item">
<input type="checkbox" id="mycheckbox" class="checkbox" name="prodid[]" value="'.$row['itemName'].'">
<div class="option_inner facebook">
<div class="tickmark"></div>
<div class="icon">'.$row['itemName'].'<input type="hidden" name="prodname[]" value="'.$row['itemName'].'"></div>
<div class="icon">'.$row['itemPrice'].' PHP<input type="hidden" name="price[]" value="'.$row['itemPrice'].'"></div>
<div class="icon"><input type="number" name="multiple[]" style="padding:5px;width:60px;" value="1" ></div>
</div>
</label>';
}
} else {
$conn = mysqli_connect('localhost','root','','orderqueing') or die (mysqli_error());
$query = "SELECT * FROM product_tbl";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
echo '<label style="float:left" class="option_item">
<input type="checkbox" id="mycheckbox" class="checkbox" name="prodid[]" value="'.$row['itemName'].'">
<div class="option_inner facebook">
<div class="tickmark"></div>
<div class="icon">'.$row['itemName'].'<input type="hidden" name="prodname[]" value="'.$row['itemName'].'"></div>
<div class="icon">'.$row['itemPrice'].' PHP<input type="hidden" name="price[]" value="'.$row['itemPrice'].'"></div>
<div class="icon"><input type="number" name="multiple[]" style="padding:5px;width:60px;" value="1" ></div>
</div>
</label>';
}
}
?>
this is my UI :
enter image description here
because the checked checkboxes resets when i search an item.
i want to retain it checked before the form submits.
i could not think of a solution to this problem. thanks for the help
You can consider recording the checkbox you selected in the cookie when submitting or at the end of the keyup function; then get the cookie in window.onload or your code and give the corresponding checkbox a selected state
I'm not quite sure if you are requesting this data via ajax.This is untested code, but provides an idea; you may need to modify or even use
I thought of the following workaround:
window.onload=function(){
//Called after initialization or search. Check the checkbox
function reloadCheckBox(){
let allProdCheck = document.querySelectorAll("input[name=prodid[]]");
allProdCheck.forEach(element => {
let cooki = getCookie("prod_"+element.value);
if(cooki!=''&&Boolean(cooki)==true){ //if exists (previously selected)
element.checked = true;
}else{
element.checked = false;
}
});
}
}
//Record in cookie when selected
function prodCheck(ele){
setCookie("prod_"+ele.value,ele.checked);
}
function setCookie(cname,cvalue='',exdays=1){
var d = new Date();
//d.setTime(d.getTime()+(exdays*24*60*60*1000));
//var expires = "expires="+d.toGMTString();
//document.cookie = cname+"="+cvalue+"; "+expires;
document.cookie = cname+"="+cvalue;
}
function getCookie(cname){
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
}
return "";
}
Then add event onclick="prodCheck(this)" to this statement <input type="checkbox" id="mycheckbox" class="checkbox" name="prodid[]" value="'.$row[' itemName'].'" >
<input type="checkbox" id="mycheckbox" class="checkbox" name="prodid[]" value="'.$row['itemName'].'" onclick="prodCheck(this)" >
you saied cant figure it out on how to use it in multiple checkboxes?http://jsfiddle.net/Lwxoeyyp/1/
<input type="button" id="ReserveerButton1" value="save" onclick="save()"/>
<input type="button" id="Wisbutton1" value="delete" onclick="wis()"/>
<input type="checkbox" id="checkbox1zaal1" onchange="checkChange(this)">1e film van de dag</input>
<input type="checkbox" id="checkbox1zaal2" onchange="checkChange(this)">1e film van de dag</input>
<input type="checkbox" id="checkbox1zaal3" onchange="checkChange(this)">1e film van de dag</input>
function load(){
let checks = JSON.parse(localStorage.getItem('checkbox'));
if(checks!={}){
for(let key in checks){
document.getElementById(key).checked = checks[key];
}
}
}
/*one checkbox is checked or unchecked
If you want to use the save method you wrote, you can pass the corresponding single checkbox object as the parameter
*/
function checkChange(ele){
let oldCheck = JSON.parse(localStorage.getItem('checkbox'));
if(oldCheck==null||oldCheck=={}){
oldCheck={};
}
oldCheck[ele.id] = ele.checked;
localStorage.setItem('checkbox', JSON.stringify(oldCheck));
}
load();
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.
I have a problem with checkbox array
this is my code
<?php
include('config.php');
if(isset($_POST['submit']))
{
for($x = 0;$x <= 5;$x++)
{
if(isset($_POST['check'][$x]))
{
echo "THERE IS A VALUE<br>";
}
else{
echo "EMPTY<br>";
}
}
}
?>
<html>
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#add-file-field").click(function(){
$("#text").append("<div class='added-field'><input type='checkbox' name='check[]' />Never Expired</div>");
});
});
</script>
</head>
<body>
<form enctype="multipart/form-data" action="" method="POST">
<div id="text">
<input type="checkbox" name="check[]" />Never Expired<br>
</div>
<input type="button" id="add-file-field" name="add" value="Add input field" />
<input type="submit" value="Upload File" name="submit" id="submit" />
</form>
</body>
</html>
in here i make example i create 5 checkbox
Checkbox1
Checkbox2
Checkbox3
Checkbox4
Checkbox5
and when i check Checkbox3
the output result is
THERE IS A VALUE
EMPTY
EMPTY
EMPTY
EMPTY
I want the result is like this
EMPTY
EMPTY
THERE IS A VALUE
EMPTY
EMPTY
how to make it like that ? please help me
You need to add the key to the array
<input type="checkbox" name="check[0]" />
and
var num = 1;
$("#add-file-field").click(function(){
$("#text").append("<div class='added-field'><input type='checkbox' name='check["+num+"]' />Never Expired</div>");
num++;
});
Checkboxes that are not checked are not sent to the server by the browser, and hence are not in the $_POST['check'] array. This is why THERE IS A VALUE always comes first.
You can see this for a guide in the future...
http://www.html-form-guide.com/php-form/php-form-checkbox.html
Your problem is , you just check that is is set. You must check thier value..
Fix your html form like this
<input type="checkbox" name="check" value="1"> checkbox1</input>
"----do the same---------------------------"2""---------2------->"//do this until you get your five checkbox
Then on php
<?php
include('config.php');
if(isset($_POST['submit']))
{
for($x = 0;$x <= 5;$x++)
{
if($_POST['check']==strval($x))
{
echo "THERE IS A VALUE<br>";
}
else{
echo "EMPTY<br>";
}
}
}
?>
Hope it works
PS. If anyone see errors please edit it.
Hope this is work
add
<input type="checkbox" name="check[0]" />
then
edit this
var x = 1;
$("#add-file-field").click(function(){
$("#text").append("<div class='added-field'><input type='checkbox' name='check["+x+"]' />Never Expired</div>");
x++;
});
then simple php code
if(isset($_POST['submit']))
{
// $x = cout($_POST['check']);
for($x = 0;$x <= 5;$x++)
{
if(strlen($_POST['check'][$x])>1)
{
echo "THERE IS A VALUE<br>";
}
else{
echo "EMPTY<br>";
}
}
}
thanks
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'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";