Making matrix in php with form html - php

can you help me with this problem, i want make matrix with 2 forms, the first form is for how much is row and column is needed and second form is use for input value of array,
I was stuck in how to display it but if there are other mistake please tell me... thx u :))
this for input row and column
<form method="POST" action="prak_10.php">
Masukkann n : <input type="text" name="bilangan_n">
<br>
Masukkann m : <input type="text" name="bilangan_m">
<input type="submit" name="submit" style="background: red;color:white;border-style: none;padding: 5px;" name="" value="Hasil">
and this for input value of matrix
<?php
$n = $_POST['bilangan_n'];
$m = $_POST['bilangan_m'];
if (isset($_POST['submit'])){
?><form method="POST" action="prak_10.php">
<?php
for ($i = 0; $i < $n; $i++) {
for ($j=0; $j < $m; $j++) {
echo "Masukkan nilai array baris : ".$i." kolom : ".$j.": <input type='text' name='nilai[][]'><br>";
}
}
?>
<input type="submit" value="Hasil">
</form>
<?php }
for ($i = 0; $i < count($_POST['nilai']); $i++) {
for ($j=0; $j < count($_POST['nilai']); $j++) {
echo $_POST['nilai'][$i][$j]." ";
}
echo "<br>";
}print_r($_POST['nilai']);
?>

Change This line
echo "Masukkan nilai array baris : ".$i." kolom : ".$j.": <input type='text' name='nilai[".$i."][".$j."]'><br>"

Related

error don't hold value in increase and decrease quantity in php using loop

I have a problem, when I click to other -/+ in new row, the old row return initial value. How can I fix it?
<?php
$arr = array(1, 2, 3);
for ($i = 0; $i < 3; $i++) {
$arr[$i] = isset($_POST["item".$i]) ? $_POST['item'.$i] : $arr[$i];
if (isset($_POST['incqty'.$i])) {
$arr[$i] += 1;
}
if (isset($_POST['decqty'.$i])) {
$arr[$i] -= 1;
}
}
?>
<?php
for ($i = 0; $i < 3; $i++) {
?>
<form method='post' action='<?= $_SERVER['PHP_SELF']; ?>'>
<td>
<button name='decqty<?php echo $i; ?>'>-</button>
<input type='text' size='1' name='item<?php echo $i; ?>' value='<?= $arr[$i]; ?>' />
<button name='incqty<?php echo $i; ?>'>+</button>
</td>
</form>
<?php
}
?>
change value when end click other row

make view of array with filling input in the form

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']);
}
?>

created input tag in php for loop and get value

I want to create limit input tag with php for loop an get inputs values. My sample code is this:
<?php
$limit = 10;
for ($i=1; $i<=$limit; $i++) { ?>
<input name="<?php echo $i ?>" type="text" /><br>
<?php } ?>
Is my code correct? How can I get input values?
You can try my script.
<?php
$limit = 10;
?>
<form method="post">
<?php
for ($i = 1; $i <= $limit; $i++) {
?>
<input name="anything[]" type="text" /><br>
<?php } ?>
<input type="hidden" name="op" value="sent" />
<input type="submit" value="submit" />
</form>
<?php
if (!empty($_POST["op"])) {
for ($i = 1; $i <= $limit; $i++) {
if (strlen($_POST["anything"][$i]) !== 0) {
?>
<p>The value of the <?php echo $i; ?> text field is: <?php echo $_POST["anything"][$i]; ?>
<?php
} else {
?>
<p><?php echo $i; ?> was not set.</p>
<?php
}
}
}
Looks alright but I would use an array as the input name. For example:
<?php
$limit = 10;
for ($i=1; $i<=$limit; $i++) {
?>
<input name="number[<?php echo $i; ?>]" type="text" /><br>
<?php
}
?>
That way in the back end you can just loop through the number array like so.
foreach ($_POST['number'] as $key => $value) {
// Do stuff
}
Your code should render 10 html input fields with name 1, 2, 3, ... 10 correctly
To get input values, wrap your input fields in a form element with an action pointing to the php script in which you want to read the values (e.g. action="myscript.php").
(You should add a input type="submit" to have a way to submit the form. I assume you know HTML good enough to create a simple form.)
The script invoked by submitting the form (e.g. myscript.php) will now be able to read the values using the $_GET array. See http://php.net/manual/de/reserved.variables.get.php
You could print the values like so:
<?php
for($i=1;$i<=10; $i++) {
echo $i . ' : '. $_GET[$i];
}
?>
Edit: As #David Jones mentioned it would be better to use an array as input name

Processing radio buttons in loop in PHP

I have a loop that iterates 3 times. Inside the loop I have a HTML form that has radio buttons. I'm processing the input using PHP. When I echo the form data, its not showing the correct values. Is it a wrong way of processing the data ? Any help is appreciated.
test.php
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<form action = 'test.php' method = 'post'>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
</form>
<?php
}
?>
<input type = 'submit' value = 'Go'>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
Move your form outside of your for loop. You are currently creating three forms with one submit button (which isn't attached to any of them).
try this way
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
use following code: Or you can use radiogroup array it is easier than this.
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
#Cagy79 added additional submit buttons.. I have revised the code,
<form action ="" method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
This works. :)
You have 3 forms without a submit button (it needs to be in between the tags). You need to create one form with one submit button like this so all your data is posted to $_POST.
<form method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>

How to output checkboxes from an HTML form by PHP?

I have an HTML form as:
<form action="something.php" method="POST" enctype="multipart/form-data" id="color">
<fieldset>
<legend><h2><span style="color: #993300; text-shadow: #808080 2px 1px 2px; font-size: 25px;">Color Preferences:</span></h2></legend>
<p><table>
<tbody>
<tr>
<td width="350px;"><span style="color: #993300;">*</span> Colors you consider to use to your new Site:</td>
<td>
<br/>
<input type="checkbox" name="color[]" value="Black" /> Black<br/>
<input type="checkbox" name="color[]" value="Blue" /> Blue<br/>
<input type="checkbox" name="color[]" value="Brown" /> Brown<br/>
<input type="checkbox" name="color[]" value="Gray" /> Gray<br/>
<input type="checkbox" name="color[]" value="Green" /> Green<br/>
</td>
</tr>
</tbody>
</table></p>
</fieldset>
<input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" />
To execute the color form, I used a PHP as:
$color = $_POST['color'];
if (!isset($color))
{
unset($_GET['do']);
$message = "Error = Colors are required. Please try again.";
break;
} else {
$N = count($color);
for($i=0; $i < $N; $i++){
echo($color[$i] . " ");
}
}
But when I test my code over the output area it only giving an "Array" as a result.
Can you please help me to fix this code?
(I need to get the selected colors as an output instead of "Array".)
I even changed my codes into:
$color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light');
if (!isset($_POST['color']))
{
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
}
but still the result outcome is only "Array" instead of colors name. can you please help me to resolve this issue?
I even changed my codes into:
$color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light');
if (!isset($_POST['color']))
{
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
}
but still the result outcome is only "Array" instead of colors name. can you please help me to resolve this issue?
Try this:
PHP Code:
if (isset($_POST['submit'])) {
if (!isset($_POST['color'])) {
unset($_GET['do']);
echo "Error = Colors are required. Please try again.";
} else {
$color = $_POST['color'];
$N = count($color);
for ($i = 0; $i < $N; $i++) {
echo($color[$i] . " ");
}
}
}
with color[], you will receive an array ($_POST['color']). You can test if it is isset() or it is empty()
if it is NOT empty, then you can iterate over the values for instance like this:
$n = count($_POST['color']);
echo("You selected $n color(s): ");
for($i=0; $i < $n; $i++)
{
echo($_POST['color'][$i] . " ");
}
(untested. Code taken from here: http://www.html-form-guide.com/php-form/php-form-checkbox.html)

Categories