make view of array with filling input in the form - php

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

Related

Making matrix in php with form html

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>"

PHP get first four item in array on first loop then get next four item on second loop?

//First file
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<div>
<form action="questions.php" method="post">
Quiz Name: <input type="text" name="quizname" /><br/><br/>
Number of M.C.Q questions: <input type="number" name="mcq" min="0" /><br/><br/>
Number of True/False questions: <input type="number" name="truefalse" min="0" /><br/><br/>
<input type="submit" name="submit" value="Start Generating Questions" />
</form>
</div>
</body>
</html>
//Second file
<?php
if(isset($_POST['submit'])){
$quizName = $_POST['quizname'];
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
echo '<form action="finalquestions.php" method="post">';
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$i.' <input type="text" name="question1[]" /> </br></br>';
echo '<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>';
echo '</br></br>';
}
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$i.' <input type="text" name="question2[]" /> </br></br>';
echo '<input type="radio[]" name="question2">True</br>
<input type="radio[]" name="question1">False</br>';
echo '</br></br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>
//Third file
<?php
if(isset($_POST['submit'])){
//array of input elements named "question1"
$question1 = $_POST['question1'];
//array of input elements named "radiooptions"
$radiooptions = $_POST['radiooptions'];
//get the length of those array
$question1length = count($question1);
$radiooptionslength = count($radiooptions);
//loop through array to get first input with four radiooptions inputs
for($x = 0; $x < $question1length; $x++) {
echo $question1[$x];
echo "<br>";
echo "<br>";
for($i = 0; $i < $radiooptionslength; $i++) {
echo $radiooptions[$i];
echo "<br>";
echo "<br>";
}
}
guys in first loop of the array [question1] I want to get first input in array of [question1] and in second loop I want to get the first 4 inputs of array [radiooptions] and when the loop gets second time I want it to give the second input of array [question1] and in the [radiooptions] array loop I want it to give me the next 4 inputs for example next 4 inputs will be 5,6,7,8.....But its giving me different [question] inputs but same first 4 item of array [radiooptions]
You would need to do something like the following using a simple pagination function on an array.
<?php
$array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
for($i = 1; $i < 5; $i++){
$utility = new Utility();
$utility->getItems($i, $array);
echo '<br/>';
}
class Utility{
public function getItems($index, $array, $count = 4){
$min = ($index - 1) * $count;
$max = $index * $count;
for($k = $min; $k < $max; $k++){
echo $array[$k] . ' ';
}
}
}
?>

Unset certain items from session array when they're clicked?

What I'm basically looking for is the ability to remove a certain item from the array when that item is clicked, for this instance... If I hit "Two", it will disappear.
Demo: http://query.notesquare.me
CODE:
<form method="post">
<input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
<input type="submit" id="button-create-playlist" value="Create Playlist" />
</form>
<?php
ini_set("session.save_path", "/home/kucerajacob/public_html/query.notesquare.me/test-sessions");
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$create_playlist = $_POST['create_playlist'];
$_SESSION['user_playlists'][] = $create_playlist;
}
$playlists = array("One", "Two", "Three");
if (isset($_SESSION['user_playlists'])) {
for ($i = 0; $i < count($_SESSION['user_playlists']); $i++) {
array_unshift($playlists, $_SESSION['user_playlists'][$i]);
}
}
$_SESSION['main'] = $playlists;
for ($i = 0; $i < count($playlists); $i++) {
echo $playlists[$i] . "<br />";
}
?>
Its possible, you'll need to handle that request as well. If you want the click posted, a simple <button> next to that should suffice.
Upon rendering the markup, (of course using the session array) use the key which can be used in unsetting the values.
<?php
// initialization
if(empty($_SESSION['user_playlists'])) {
$_SESSION['user_playlists'] = array("One", "Two", "Three");
}
if(isset($_POST['add'], $_POST['create_playlist'])) {
// handle additions
$_SESSION['user_playlists'][] = $_POST['create_playlist'];
}
if(isset($_POST['remove'])) {
// handle remove
$key = $_POST['remove'];
unset($_SESSION['user_playlists'][$key]);
}
?>
<form method="post">
<input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
<input type="submit" id="button-create-playlist" name="add" value="Create Playlist" />
<hr/>
<?php foreach($_SESSION['user_playlists'] as $k => $p): ?>
<?php echo $p; ?> <button type="submit" name="remove" value="<?php echo $k; ?>">Remove</button><br/>
<?php endforeach; ?>
</form>
Sample Demo
Try below:
for ($i = 0; $i < count($playlists); $i++) {
// echo $playlists[$i] . "<br />";
printf('%1$s<br/>', $playlists[$i]);
}
if ($_GET['query'])
{
unset($playlists['query']);
}

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

Categories