PHP actions with multiple Buttons resetting others - php

here you see my code example:
<?php
session_start();
$arbeit = $_GET["arbeit"];
echo "Angelegte Prüfung: ";
echo $arbeit;
$damen = 0;
$herren = 0;
if (array_key_exists('add_Fem',$_POST)) {
$damen = 1; // BESETZT DAMEN
}
elseif (array_key_exists('sub_Fem',$_POST)) {
$damen = 0; // Reset counter
}
elseif (array_key_exists('add_Mal',$_POST)) {
$herren = 1; // BESETZT HERREN
}
elseif(array_key_exists('sub_Mal',$_POST)) {
$herren = 0; // Reset counter
}
?>
<form method='post'>
<input name='add_Fem' type="submit" value='Damen' class="button">
</form>
<form method='post'>
<input name='sub_Fem' type="submit" value='Reset Damen' class="button">
<h3><em>
<?php
if ($damen == 1) {
echo "DAMEN BESETZT!";
} else {
echo "DAMEN FREI!";
}
?>
</em></h3>
</form>
<form method='post'>
<input name='add_Mal' type="submit" value='Herren' class="button">
</form>
<form method='post'>
<input name='sub_Mal' type="submit" value='Reset Herren' class="button">
<h3><em>
<?php
if ($herren == 1) {
echo "HERREN BESETZT!";
} else {
echo "HERREN FREI!";
}
?>
</em></h3>
</form>
the problem is, when i'm try to set both to 1(male and female) the other variable gets reset to 0. And i dont know why they are affect each other!
Also PHP isset not solves my problem, same situation, that was my first try.
I hope you can give me some advice.
Thank you

Related

add values with one another with input field submit in php calculator

I want a calculator with my PHP code that adds values with one after one with submit button.like when I input a number then submit it and show it on the page then and input other, it should add previous number.and then enter another then submit.like these, numbers are adding with one another after submitting.
<?php
session_start();
?>
<?php
error_reporting(0);
?>
<html>
<title>adding input single values</title>
<body>
<form method="post">
<input type="text" name='number' method="post"/>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number']))
{
}
else
{
$sum += $_POST['number'];
echo ++$sum;
}
?>
</body>
</html>
here is the output
You could use a hidden field instead of storing in the session.
<input type="text" name='number' method="post"/>
<?php
if(!isset($_POST['number'])) {
echo "<input type='hidden' name='prev_number' value=0 />";
} else {
$sum = $_POST['number'] + $_POST['prev_number'];
echo "<input type='hidden' name='prev_number' value=" . $sum . " />";
echo $sum;
}
?>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number'])) {
// ...
}
else {
$_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : '';
$_SESSION['number'] += $_POST['number'];
echo $_SESSION['number'];
}

checkbox handling php multiple checkbox

Working on a simple php code. When it press on only PH it show hello, and only on chlorine it show yello. When both is pressed it show sello.
<?php
if(isset($_POST['submit'])){
foreach($_POST['verdi'] as $animal){
if(isset($_POST['verdi[]==PH']))
{
echo "hello";
}
}
}
?>
<form name="input" action="" method="POST">
<input type="checkbox" name="verdi[]" value="PH">PH<br>
<input type="checkbox" name="verdi[]" value="Chlorine">Chlorine<br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can do a simple check in PHP:
if( in_array("PH", $_POST["verdi"]) ){
echo "in array!";
}
if(isset($_POST['submit']) && is_array($_POST['verdi'])) {
$checked = array();
foreach($_POST['verdi'] as $animal) {
// you can do extra validation here.
$checked[] = $animal;
}
if(in_array("PH", $checked) && in_array("Chlorine", $checked)) {
echo "sello";
} else {
if(in_array("PH", $checked)) {
echo "hello";
} else if(in_array("Chlorine", $checked)) {
echo "yello";
}
}
}

Creating PHP multiplication table with user input

I am creating form where the user can input a integer and get the multiplications (1-10) of that number. Here is my web form for user input:
<html>
<head>
<title>Assignment 9.1</title>
</head>
<body bgcolor="black" text="white">
<form method="post" action="table.php"
<strong>Enter No:</strong>
<input type="text" name="num" size="10">
<input type="submit" value="Get Table">
</form>
</body>
</html>
The table I have created is this:
<?php
$num = $_POST['num'];
if($num)
(
for ($i=1; $i<=10; $i++)
(
$mul = $num * $i;
echo "$num * $i = $mul<br>";
)
)
else
(
echo "Invalid Entry!";
)
?>
I am getting an error for the table. The error is for line 5 (FOR). I have no idea why I am getting this error. Can anyone Help?
Blocks of code are marked with curly brackets { ... }, you are currently using parentheses ( ... )
As far as I can tell, that's pretty much the only thing wrong with it. You might want to add a bit of validation:
$num = isset($_POST['num']) ? intval($_POST['num']) : 0;
But that'd just a touch-up. The brackets are your actual problem.
You need to use {} instead () in the for and if.
Like this:
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
Hope it helps you!
You are using parentheses when you should be using braces. Try this...
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
save the file as table.php and run working 100%
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul </center><br>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul <br></center>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>

How to count input that have value?

in my code, when we click ok button . It's will echo 3
I want to apply to count only input that have value only
How to do ?
<?php
if(isset($_POST["submit"]))
{
echo count($_POST["to_more"]);
}
?>
<form name="f1" method="post">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="submit" name="submit" value="OK">
</form>
try this
<?php
if(isset($_POST["submit"]))
{
echo count(array_filter($_POST["to_more"]));
}
?>
How about
if(isset($_POST["submit"])){
$count = 0 ;
foreach($_POST["to_more"] as $data){
if($data != '') $count++;
}
if($count > 0)
echo $count;
}
array_filter should help
<?php
$ar = isset($_POST["to_more"]) ? $_POST["to_more"] : array();
$ar = array_filter($ar, function($el){ return !empty($el);});
echo count($ar);
?>
Should be quicker than foreach, btw.
UPD: Oh, seems, NLSaini posted the precise solution, check it.

PHP Multiple form in same page

I'm doing form in php but I have some problem.
First I will have 3 different form in the same page.
What I want is only 1 form appear and then with the answer a second form will appear and so on.
The answer of the form will be display on the same page.
For now my first form work and after get the answer go to the 2nd form but I want to submit the 2nd form the problem appear.
It delete the answer of my first form and don't do anything (everything start like I am in my first form).
I try to find the problem but can't have idea about how to solve it.
Here is my code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1?
<input type="number" name="nbtemplate" min="1" max="30">
<input type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php
if(!isset($submitbutton1)) {
if (!empty($_POST['nbtemplate']) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for ($i = 1; $i <= $Nnbtemplate; $i++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
if(isset($submitbutton1) && !isset($submitbutton2)) {
if (!empty($_POST['nbtime']) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST['nbtime'];
for ($j = 1; $j <= $nbtime; $j++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time" name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
}
}
?>
That is some gnarly code you got there, brother. This is a really simple task when handled with some javascript. Then you can have a single post to your php. I like using the jQuery framework so here's a couple links I found quickly: this one and this one
Example code in response to comment about building form elements dynamically:
<html>
<head>
<!-- load jquery library -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<form action="toyourpage.php">
How Many?:
<input type="text" name="number" id="number">
<div id="add"></div>
</form>
<!-- javascript go -->
<script type="text/javascript">
$(document).ready(function()
{
$('input#number').keyup(function()
{
var num = $(this).val(); // get num
if(!isNaN(num)) // check if number
{
$('div#add').html(''); // empty
for(i = 1; i <= num; i++) // add
{
$('div#add').append('New Field ' + i + ': <input type="text" name="next_' + i + '" id="next' + i + '"><br>');
}
}
else
{
alert('Valid number required');
}
});
});
</script>
</body>
</html>
I did some changes on Your code, and have tested, it works.
You had any mistakes in your {} brackets and if conditions. Also as I commented I added extract($_POST).
<?php
extract ( $_POST );
if (! isset ( $submitbutton1 ) && !isset($submitbutton2)) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1? <input type="number" name="nbtemplate" min="1" max="30"> <input
type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php ;
}
if (isset ( $submitbutton1 )) {
if (! empty ( $_POST ['nbtemplate'] ) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST ['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or
die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for($i = 1; $i <= $Nnbtemplate; $i ++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
}
}
if ( isset ( $submitbutton2 )) {
if (! empty ( $_POST ['nbtime'] ) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST ['nbtime'];
for($j = 1; $j <= $nbtime; $j ++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time"
name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
?>

Categories