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

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

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

array_push not adding 2nd item after page refresh

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.

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

PHP Multiple form in same page

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

Categories