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
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 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 was wondering if you can help me with my php function
I have created a quiz which uses radio buttons and a submit button. I want the function to check if a specific radio button is selected and if so add 1 onto the user score. however nothing is being added the function for this is
function Score()
{
if(isset($_POST['correctAnswer'] ))
{
$answer=$_POST['correctAnswer'];
$_SESSION['score']=$userScore+1;
}
else
{
$_SESSION['score']=$_SESSION['score'];
}
}
and the form in which it is submitted is
echo '<strong>'."$theQuestion".'</strong><br>';
?> <form name="correctAnswer" form method="post" action="quiz.php" onSubmit="Score()">
<?php
echo "$theAnswer1";?> <input type="radio" id="correct_answer" name="correctAnswer">
<?php
echo "<br>$theAnswer2"; ?> <input type="radio" id="wrong_answer1" name="wrongAnswer1">
<?php
echo "<br>$theAnswer3"; ?> <input type="radio" id="wrong_answer2" name="wrongAnswer2">
<?php
echo "<br>$theAnswer4"; ?> <input type="radio" id="wrong_answer3" name="wrongAnswer3">
<input type="hidden" name="score" value="userScore">
<br><input type="submit" value="Submit Answer">
</form>
Hope you can help
You have to pass the counter as an argument of the function like this
function Score($userScore){
//Do the rest
}
Looks like the $_SESSION['score'] is undefined
function Score() {
if (!isset($_SESSION['score'])) {
$_SESSION['score'] = 0;
}
if (isset($_POST['correctAnswer'])) {
$_SESSION['score']++;
}
}
I did a little form with jQuery, that when a button is clicked, it adds a new input with a name.
The jQuery I add to the page:
<script>
$(document).ready(function() {
var i = 2;
$("span").click(function() {
$("#add").before("<tr><td>Name</td><td><input type='text' name='name-"+i+"' /></td></tr>");
$("#numper").val(i);
i++;
});
});
$("#numper").val(i) is an hidden input to know how many times I added an input.
When I check the console and Elements (F12 in Chrome) the fields are added, and the hidden one is changed correctly, but when I try to obtain the values with PHP, it says that the added inputs are undefined.
for($i = 1; $i <= $_POST['numper']; $i++) {
echo $_POST['name-'.$i];}
If you can help me to sort this out, I would be very grateful!
Thanks!
I would simply approach your requirements in a different way, name all your inputs so PHP reads them as an array like so:
<input name="name[]" ..... />
PHP:
foreach($_POST['name'] as $index => $input_value) {
....
}
Please check if below code helps you.
<html>
<head>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
$('#addbutton').click(function() {
var i = parseInt($('#numper').val()) + 1;
$('#table').prepend("<tr><td>Name</td><td><input type='text' name='name-"+i+"' /></td></tr>");
$('#numper').val(i);
});
});
</script>
</head>
<body>
<form method='post' action='<?php echo $PHP_SELF;?>'>
<table>
<tr>
<td>
<input type='button' id='addbutton' value='Add new input box' />
</td>
</tr>
</table>
<table id='table'>
<tr>
<td>Name</td>
<td>
<input type='text' name='name-1' />
</td>
</tr>
<tr>
<td>
<input type='hidden' id='numper' name= 'numper' value='1' />
<input type='submit' value='submit' />
</td>
</tr>
</table>
</form>
</body>
<?php
for ( $i = 1; $i <= $_POST['numper']; $i++ ) {
echo $_POST['name-' . $i] . '<br>';
}
?>
In Javascript you have variable i = 2. So, your first input will have a name "name-2".
In PHP, you have for loop with variable $i = 1. So it will try to get $_POST['name-1'].
So, it will give error for undefined.
for the jquery part, you can write this
$(document).ready(function(){
$("#btn1").click(function(){
$("p").append("<input name='whatever' type='text' class='whatever'>");
});
});
if you want the php part, then notify me
I am creating a new website. For that website I will need to transfer a quantity and amount value from one if condition statement into another if condition statement. Both if statements are accessed by separate submit buttons named "checkamt" & "buy".
I need to transfer the "quantity", "value", and "net" values to and from the checkamt if statement to the buy if statement.
Here's my code:
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
I think that the problem you're having is that variables don't persist on page change. If you want that, you'll need to use a session. First, you must call session_start before anything, including HTML, is sent to the user. Then, you can use the $_SESSION variable.
<?php
session_start();
?>
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$_SESSION['qun']=1;
$_SESSION['val']=5000;
$_SESSION['total']=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $_SESSION['qun'];
echo $_SESSION['val'];
echo $_SESSION['total'];
}
?>
Improve your English! Not sure if this is what you want, but if you want to share the values of your variables between the two ifs? You have to declare them at a higher scope than your if:
<?php
$qun = 0;
$val = 0;
$total = 0;
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
Not sure if I'm getting the question right, but why not do something like this :
if(isset($_POST[checkamt]) || isset($_POST[buy]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
echo $qun;
echo $val;
echo $total;
}
You need to track your variables between the different forms. You can use SESSION like Xeon06 suggested, or do the following. I'm only showing for $qun:
<?php
if(isset($_POST['checkamt'])) {
$qun=1;
}
if(isset($_POST['buy'])) {
echo $qun;
}
?>
<form action="index.php" method="post">
<input type="hidden" name="qun" value="<?php echo $qun; ?>" />
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>