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.
Related
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
i want make a view of array, in the first i need fill length of array for first form (this for how much is show) and then i use that to fill value of array then i show that.
<form method="POST" action="prak_9_1.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
second code is different file
<?php
$bilangan = $_POST['bilangan'];
$bil = $bilangan;
$array = array();
$n = 0;
?>
<form method="POST" action="prak_9_1.php">
<?php
for ($i=0; $i < $bilangan ; $i++) {
echo "input value of array : <input type='text' name='nilai_".$i."'><br>";
}
?>
<input type="submit" value="Hasil">
</form>
<?php
for ($i=0; $i < $bil ; $i++) {
$array[$n] = $_POST['nilai_'.$i];
$n++;
}
print_r($array);?>
sorry if my english is bad... thanks for ur helps...
first file :
<form method="POST" action="second.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
</form>
and second file :
<?php
if (isset($_POST['bilangan'])) {
echo '<form method="POST">';
for ($i = 0; $i <= $_POST['bilangan']; $i++) {
echo "input value of array : <input type='text' name='nilai[]'><br>";
}
echo '<input type="submit" value="Hasil">';
echo '</form>';
}
if (isset($_POST['nilai'])) {
print_r($_POST['nilai']);
}
?>
I have a list a checkboxes with accompanying input text fields. If the user checks a box, the accompanying text field will be add to an array.
I am new to PHP and was wondering if anyone can help me in the right direction.
Should I use a for loop, foreach, while, unique "name" for each input, or something else?
Below is what I have so far.
<?php
if(isset($_POST['submit'])){
$array = array();
while(isset($_POST['check'])){
if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
echo "Please include your text for each checkbox you selected.";
exit();
}
$array[] = $_POST['some_text];
}
}
?>
<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
You first need a way to associate your checkboxes with their corresponding text fields, and a way to tell them apart. For example:
<form>
<input type="checkbox" name="check[]" value="1"><input type="text name="text1">
<input type="checkbox" name="check[]" value="2"><input type="text name="text2">
<input type="checkbox" name="check[]" value="3"><input type="text name="text3">
</form>
Now you can loop through it as follows:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['check']) && is_array($_POST['check']) && !empty($_POST['check'])) {
$array = array();
foreach ($_POST['check'] as $value) {
$text_field = 'text' . $value;
if (!isset($_POST[$text_field]) || trim($_POST[$text_field]) == '') {
echo "Please include your text for each checkbox you selected.";
exit;
}
$array[] = $_POST[$text_field];
}
}
}
Try this:
$select = array();
foreach($_POST['check'] as $key => $selected){
$select[$key] = $selected;
}
Please try this:
<?php
if(isset($_POST['submit'])){
for($i=0; $i<100; $i++)
{
if($_POST['check_'.$i])
{
$array[] = $_POST['input_'.$i];
}
}
}
?>
#################### HTML
<form method="post" action="">
<?php for($i=0; $i<100; $i++) { ?>
<input type="checkbox" name="check_<?php echo $i ?>"><input type="text" name="input_<?php echo $i ?>">
<?php } ?>
<input type="submit" name="submit">
</form>
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>";
}
?>
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>';
}
}
?>