Associative array can't echo data from input form - php

I insert data to the array from an input box. But I don't know why I can't print the associative array. I can only echo the latest data from the input box. I want to add data to the array and echo it every time I write to the input box.
<?php
$part_insert_message = "";
$inserted_parts = array();
session_start();
$part_inserted_id;
if(isset($_POST['submit'])) {
$part_inserted_id = $_POST['arrdata'];
$inserted_parts[$part_inserted_id] = $part_inserted_id;
echo sizeof($inserted_parts);
// store session data
$_SESSION['views']= $inserted_parts;
$part_insert_message = "ID: " . $part_inserted_id;
}
?>
<html>
<body>
<div>
<h2>Part</h2>
<form action="array_session_example.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
Array Data: <input type="text" name="arrdata"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php echo $part_insert_message;
foreach($inserted_parts as $key => $value){
echo $key;
}
?>
</div>
</body>
</html>

You're creating a new array every time, and then adding that to the session. You'll need to check if an array has already been stored, and if it has add to that.
$inserted_parts = array();
session_start();
if(isset($_SESSION['views']) && is_array($_SESSION['views']))
$inserted_parts = $_SESSION['views'];

Related

how to echo input text in For{}

I write below code and put an html input text in for :
for($i=0;$i<5;$i++)
echo '<input type="text" name="subject">';
but when i want to echo the that value I type in to the above text box
it returns "" nothing
echo $_POST['subject'];
I have
<form name="form1" method="post" action="index.php">
Tag top of this codes ?
Sorry if I had Mistake
Make the input as array and read the values in PHP using loop like below.
HTML code :
<form name='myForm' action='index.php' method="post">
<?php
for {$i=i;$i<5;$i++} {
echo "<input type='text' name='subject[]'>";
}
?>
</form>
PHP Code :
$subjectArray = $_POST['subject'];
foreach($subjectArray as $key => $val) {
if($val != "") {
echo $key."==".$val."<br>";
}
}
It's bad manner to echo your HTML code. It can become annoyingly hard for someone to read the code after you (for exemple, in a company)
I would suggest doing this:
for($i=0;$i<5;$i++) {
?><input type="text" name="subject"><?php
}
If you want to input back in the form what was just typed, then you're doing too much.
Is your form in index.php too?! If so, action="index.php" become action=""
• With your array:
$subjectArray= array();
// Gather data from $_POST if it's the subject form
if(!empty($_POST) aa isset $_POST['subjectForm']) {
// Remove the hidden input from the list
unset($_POST['subjectForm']);
// Add the remaining entry to $subjectArray
foreach($_POST as $key => $value) {
$subjectArray[$key]= $value;
}
}
<form name="myForm" action="" method="post">
<!-- hidden input to find our form after validation -->
<input type="hidden" name="subjectForm" value=1>
<?php
for($i=1; $i<=5; $i++) {
?><input type="text" name="subject<?= $i ?>" value="<?php if(isset(subjectArray['subject'.$i])) { echo $_POST['subjectArray'.$i]; } ?>"><?php
}
?>
</form>
• Without your array:
<form name="myForm" action="" method="post">
<?php
for($i=1; $i<=5; $i++) {
?><input type="text" name="subject<?= $i ?>" value="<?php if(isset($_POST['subject'.$i])) { echo $_POST['subjectArray'.$i]; } ?>"><?php
}
?>
</form>

Use a single textbox to keep adding values to array after post

So I'm currently working on a little project that allows a user to input a number into a textbox, after clicking a button that says "add" it should store that value into an array and then allow the user to input another value into that array. There is also a button on the page when the user is finished and wants to sum the values called "Submit". The problem I'm running into is everytime the form posts back, it recreates a new blank array. Any tips?
See the code below:
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
</p>
<?php
$array = array();
if (isset($_POST['submit']))
$num = $_POST['strNumber'];
$array[] = $num;
foreach($array as $num)
echo $num . ' + ';
if(isset($_POST['calculate']))
foreach($array as $num)
echo $num . ' + ';
?>
</form>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
<input type='submit' name='clear' value='clear' />
</p>
<?php
if (isset($_POST['submit'])) {
if(!array_key_exists("numbers", $_SESSION)) {
$_SESSION["numbers"] = array();
}
array_push($_SESSION["numbers"], $_POST["strNumber"]);
}
if(isset($_POST['clear'])) {
$_SESSION["numbers"] = array();
}
if(array_key_exists("numbers", $_SESSION)) {
echo implode("+", $_SESSION["numbers"]);
}
if(isset($_POST['calculate'])) {
if(array_key_exists("numbers", $_SESSION)) {
$expression = implode("+", $_SESSION["numbers"]);
eval( '$result = (' . $expression . ');' );
echo "=" . $result;
}
}
?>
</form>
</body>
</html>
Start a session
When the action is "submit"
Check if the session which will store the numbers is initialized
If it's not initialize it as an array
Finally push the number into the array
Check if there is a session initialized if there is print all the numbers ( you can use implode to do that)
if the action is calculate .. just make the calculation ( check eval function )

form checkbox value php

I have a form created by a while loop in php like this :
<form action='#' method='post'>
<input type='hidden' name='form_valid' id='form_valid'>
<?php
$i=-1;
$result_array = array();
//the while is from a simple mysql query
while( $line = $results->fetch() )
{
$i++;
echo"<input type='checkbox' class='checkbox' value='".$line->nid."' id='".$i."'>";
echo $line->title;
echo'<br>';
$result_array[$i] = $line->nid;
}
<input type='submit'>
?>
</form>
Then later on the code I'd like to store the values of the checked checkboxes only in a new array :
if (isset($_REQUEST['form_valid'])) //checking is form is submitted
{
foreach($result_array as $result)
{
if($result == $_REQUEST[$j]) <<<< ERROR
{
$final_array[$j] = $result;
}
}
}
Surprisingly, this code does not work at all.
The error message is "Notice : Undefined offset: 0", then offset 1 then 2 etc ...
The line where the message says theres an error is the marked one.
I really have no idea how to do this. Someone ? =)
Don't try to do it this way, this just makes it hard to process, just use a grouping name array: name="checkboxes[<?php echo $i; ?>]", then on the submission, all values that are checked should simply go to $_POST['checkboxes']. Here's the idea:
<!-- form -->
<form action="" method="POST">
<?php while($line = $results->fetch()): ?>
<input type="checkbox" class="checkbox" name="nid[<?php echo $line->nid; ?>]" value="<?php echo $line->nid; ?>" /><?php echo $line->title; ?><br/>
<?php endwhile; ?>
<input type="submit" name="submit" />
PHP that will process the form:
if(isset($_POST['submit'], $_POST['nid'])) {
$ids = $_POST['nid']; // all the selected ids are in here
// the rest of stuff that you want to do with it
}

How to create isset($_POST['id'.$var]) within loops?

I am trying to create a page where there are several fields and users can comment on each one. To create these fields and text inputs, I am running a while loop with the following html within it:
<form name = "replyform" method = "post" action = "">
<input id = "replytext<? echo $replyID; ?>" value = "replytext<? echo $replyID; ?>" name = "replytext<? echo $replyID; ?>" type="text" class = "span5">
</form>
And then using the following code to call the 'wall_reply()' function, submitting the text values.
if (isset($_POST['replytext'.$replyID])) {
echo wall_reply();//5, $_POST['replytext'.$replyID]);
}
Something's a miss though. Any ideas what could be wrong here?
You have loop to create these form and input?
put the loop inside the form tag, so that only one form will be created with multiple inputs.
this seem to work correctly, use it as your guide ;)
<?php
$maxposts=7;
if (isset($_POST['submit'])){
function wall_reply($id,$text){
echo '<hr />updating id '.$id.' with '.$text;
}
var_dump($_POST);
for ($i=0;$i<$maxposts;$i++){
$replyID = $i;
if (isset($_POST['replytext'.$replyID])) {
wall_reply($i,$_POST['replytext'.$replyID]);//5, $_POST['replytext'.$replyID]);
} else {
echo 'not set';
}
}
}
?>
<form name = "replyform" method = "post" action = "">
<?php
$replyID = 5;
for ($i=0;$i<$maxposts;$i++):
$replyID = $i;
?>
<input id = "replytext<?php echo $replyID; ?>" value = "replytext<?php echo $replyID; ?>" name = "$
<?php endfor; ?>
<input type="submit" name="submit" value="go"/>
</form>

Stack array losing previous values for single page submission

I'm trying to do the push-and-pop stack array in PHP but only the last value is stored. How can I store the other values as well even if I click on the button again & load the same page?
This is what I've done:
<?php
if(!$_GET)
$myStack = array();
else
$myStack[] = "";
?>
<html>
<head> <title>Exercise</title>
</head>
<body>
<form action="test.php" method="get">
Element: <input type="text" name="num" value="0"/><br/>
<input type="submit" name="push" value="push" />
<input type="submit" name="pop" value="pop" />
</form>
<?php
if(isset($_GET["push"])){
array_push($myStack, $_GET["num"]);
foreach($myStack as $val)
echo $val . " ";
}
elseif(isset($_GET["pop"])){
array_pop($myStack);
foreach($myStack as $val)
echo $val . " ";
}
?>
</body>
</html>
Every http request php will execute script with all variables from scratch. You have to use $_SESSION or static variables to save values between requests. To store array in $_SESSION just assign it to key:
$_SESSION["myStack"] = array();
$_SESSION["myStack"][] = 1;
$_SESSION["myStack"][] = 2;
You have a reset at the top off your script. After reload your stack will be empty. Also you have to save your stack into a session var.
Here's the code with using a session to store the array:
<?php
//starts the session
session_start();
$myStack = array();
//gets the array from the session if it exists
if (isset($_SESSION['stack']))
$myStack = $_SESSION['stack'];
?>
<html>
<head> <title>Exercise</title>
</head>
<body>
<form action="test.php" method="get">
Element: <input type="text" name="num" value="0"/><br/>
<input type="submit" name="push" value="push" />
<input type="submit" name="pop" value="pop" />
</form>
<?php
if(isset($_GET["push"])){
array_push($myStack, $_GET["num"]);
foreach($myStack as $val)
echo $val . " ";
}
elseif(isset($_GET["pop"])){
array_pop($myStack);
foreach($myStack as $val)
echo $val . " ";
}
//stores the array in the opened session
$_SESSION['stack'] = $myStack;
?>
</body>
</html>

Categories