Start New Discussion Reply to this Discussion Get The Sum Of All The Textfield
so here it is, first you will input how many textfield you have then click ok after clicking ok input the value in all the textfield after that if you click add you will see the sum of all the value in the textfield help pls
<html>
<head>
<title>Margz</title>
</head>
<form method="POST" action="">
<body>
<p>Number:
<label for="textfield"></label>
<input type="text" name="textfield" id="textfield" />
<input type="submit" name="button1" id="button1" value="OK" />
<input type="submit" name="button2" id="button2" value="ADD" />
</p>
</body>
</form>
<?PHP
function display ($x)
{
for($i=1;$i<=$x;$i++)
{
echo "Num " . $i ." ------------------ <br>";
?>
<input type="text" name=<?PHP echo $i;?>
<?PHP
echo "<br>";
echo "<br>";
}}
?>
<?PHP
if(isset($_POST['button1']))
{
display($_POST['textfield']);
}
if(isset($_POST['button2']))
{
}
?>
</html>
44 Minutes
Discussion Span
Related Article: How to invoke jquery element in php loop
is a solved PHP discussion thread by rkumaram that has 6 replies, was last updated 1 year ago and has been tagged with the keywords: jquery, php, loop.
Shodow
Junior Poster
167 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
Skill Endorsements: 0
Are there multiple text fields for the values?
Or, are the values entered like:
1234567
OR
1, 2, 3, 4, 5, 6, 7
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
Shodow
Junior Poster
167 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
Skill Endorsements: 0
This isn't tested.. But I think this is what you mean..
<?php
function display($x)
{
$sum = (int) 0;
foreach($x as $xs)
{
$sum += $xs;
}
return $sum;
}
if(!isset($_POST['submitted']))
{
echo '<form method="post" action="">';
for($i=0; ($i < 10); $i++)
{
echo '<input type="text" name="values[]" id="textfield" /><br />';
}
echo '<input type="submit" name="button2" id="button2" value="ADD" />';
echo '<input type="hidden" name="submitted" value="TRUE">';
}else{
$values = $_POST['values'];
echo display($values);
}
?>
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
here is the preview http://shodow.3web.me/FunctionLoop.php
first input how many textfield you want
then click ok
numbers of textfield will be created
after that put any value in all textfields
when you click add you will get the sum
Shodow
Junior Poster
167 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
Skill Endorsements: 0
Your problem looks like this:
You cannot have your text fields with id's such as "1", "2", "3", "4" etc..
You need to pass them as an array, so in your html just have values[] this implies that there is an array of fields.
You can then pass this array into a function to calculate the values.
Hope this makes sense.
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
Notice: Undefined index: values in - on line 62
Warning: Invalid argument supplied for foreach() in - on line 43
0
<html>
<form method="POST" action="">
<body>
<p>Number:
<label for="textfield"></label>
<input type="text" name="textfield" id="textfield" />
<input type="submit" name="button1" id="button1" value="OK" />
<input type="submit" name="button2" id="button2" value="ADD" />
</p>
</body>
</form>
<?PHP
function display ($x)
{
for($i=1;$i<=$x;$i++)
{
echo "Num " . $i ." ------------------ <br>";
?>
<input type="text" name="values[]" id="textfield">
<?PHP
echo "<br>";
echo "<br>";
}}
?>
<?PHP
function get($x)
{
$sum = (int) 0;
foreach($x as $xs)
{
$sum += $xs;
}
return $sum;
}
?>
<?PHP
if(isset($_POST['button1']))
{
display($_POST['textfield']);
}
if(isset($_POST['button2']))
{
$values = $_POST['values'];
echo get($values);
}
?>
</html>
Shodow
Junior Poster
167 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page rendered in 0.0754 seconds
using 2.71MB