array_push not adding 2nd item after page refresh - php

I understand that array_push is not adding a 2nd item because when the page refreshes after adding another item the original one goes away and get's replaced by the most recent entry from the text box.
I am trying to achieve this tactic of trying to either...
a. Have a session remember the next item being added EVEN through a page refresh.
or
b. Just not refresh the page at all.
See demo here: http://query.notesquare.me/home.html/
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
session_start();
$create_playlist = $_POST['create_playlist'];
$playlists = array("One", "Two", "Three");
$_SESSION['main'] = $playlists;
array_push($playlists, $create_playlist);
for ($i = 0; $i < count($playlists); $i++) {
echo $playlists[$i] . "<br />";
}
?>

Try
<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
session_start();
$create_playlist = $_POST['create_playlist'];
$_SESSION['user_playlists'][] = $create_playlist;
$playlists = array("One", "Two", "Three");
for ($i = 0; $i < count($_SESSION['user_playlists']); $i++) {
array_push($playlists, $_SESSION['user_playlists'][$i]);
}
$_SESSION['main'] = $playlists;
for ($i = 0; $i < count($playlists); $i++) {
echo $playlists[$i] . "<br />";
}
?>
Your approach was setting the $_SESSION['main'] = $_POST['create_playlist'] before the desired effects of array_push.

Related

how to make a button on a form work when clicked in php?

i have code like this
<body>
<form method = "POST">
<label for="">Star </label>
<input type="text" name = "star"></br>
<input type="submit" value= "Submit">
</form>
<?php
$Star = $Add = $Subs = NULL;
$Picture = "<img src = star.png>";
if($_SERVER['REQUEST_METHOD']=="POST"){
if(isset($_POST['star'])){
$Star = $_POST['star'];
for ($i=1; $i <= $Star + 1 ; $i++) {
echo "$Picture"; }
if(isset($_POST['Op'])){
$op = $_POST['Op'];
switch($op){
case 'ADD':
for ($i=1; $i <=$Star + 1 ; $i++) {
echo "$Picture";
}
case 'SUBS':
for ($i=1; $i <=$Star - 1 ; $i++) {
echo "$Picture";
}
}
}
}
?> <form method="POST">
<input type="submit" value="ADD" name = "Op"/>
<input type="submit" value="SUBS" name = "Op"/>
</form>
<?php } ?>
when I run it in the browser, the submit button works fine and displays a star image according to the input, but when the button is added and minus the star image doesn't increase and decrease. for example, if I input the number of 5 stars and I submit the program it works fine, after that when I click the add or subs button, the stars don't increase or decrease. sorry for my english
You have two forms in this code, first of all, combine both of them into a single form it looks way cleaner than this, and follow the below code it will help to achieve your task. ( Read comments in the code. )
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<label for="">Star </label>
<input type="text" name="star"></br>
<input type="submit" value="Submit" name="submit">
<input type="button" value="ADD" onclick="addNSub(this.value)" /> <!-- (this.value) will send the pressed button attribute value to the function -->
<input type="button" value="SUBS" onclick="addNSub(this.value)" />
</form>
<div id="starContainer">
<?php
$Star = $Add = $Subs = NULL;
$Picture = "<img src = 'star.png' class='starPng'>";
if (isset($_POST['submit'])) {
if (isset($_POST['star'])) {
$Star = $_POST['star'];
if (!empty($Star)) {
for ($i = 1; $i <= intval($Star); $i++) {
echo "$Picture";
}
}
}
}
?>
</div>
<script>
function addNSub(btnValue) {
const parentElement = document.getElementById("starContainer"); //get the star parent dive by it's id
const elements = document.querySelectorAll(".starPng"); //get all the star elements by it's class
if (btnValue == "ADD") { //if button value == ADD this code will execute
for (let i = 0; i <= elements.length; i++) {
if (elements.length == i) {
const getChild = elements[0]; //gets the first element (class ="starPng") under "starContainer" div
const cloneElement = getChild.cloneNode(true); // making clone of that element
parentElement.appendChild(cloneElement); //setting the clone to parent element ("starContainer" div)
}
}
} else if (btnValue == "SUBS") { //if button value == SUBS this code will execute
for (let i = 0; i <= elements.length; i++) {
if (elements.length == i) {
parentElement.removeChild(elements[0]); // removes the first HTML element (class ="starPng") under "starContainer" div
}
}
}
}
</script>
</body>

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

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

Categories