Array Value Gets Reinitialize when form is submitted using php - php

I want to Create an ItemList and simply display it. For this, I have created an array and a form with add item "text field" and "submit" button.
The problem is when I add a new value by clicking on the "submit" button, the page gets reloaded and previously added values are lost. I want previously added values to persist, without storing these data into the database.
Please help me I am a beginner in this field.
Code :
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>PHP ItemList</title>
</head>
<body>
<div class="container">
<?php
$itemList = array();
if ( $_SERVER["REQUEST_METHOD"] == "POST") {
array_push( $itemList, $_POST["Item"] );
print_r( $itemList );
}
?>
<h3>Add : </h3>
<form method="POST" action = "index.php">
<p>Item : <input type="text" name="Item"></p>
<input type="submit" name="submit" value="submit">
</form>
</div>
</body>
</html>

Related

Echo textarea's value with PHP

I want to echo the textarea value with PHP, so I create a simple form with HTML, and inside it I include textarea element with name of b64_place and then input to submit the values.
I check if b64_place is set, and if it is I echo the value of the textarea. But my program doesn't even get into the condition block, I try debugging and it is just not doing nothing.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="index.php" method="GET">
<textarea name="b64_place" form="encode">Enter text here:</textarea>
<input type="submit" value="Encode">
</form>
<?php
if (isset($_GET['b64_place'])) {
$base64e_text = htmlspecialchars($_GET['b64_place']);
echo $base64e_text;
}
?>
</body>
</html>
Your textarea contains an attribute form This attribute is used to define the id of the form this input is attached to. So, when you submit the form, the textarea isn't bound with that form and the datas aren't send
You can either add an id to the form :
<!-- check this ----------------------v---------v -->
<form action="index.php" method="GET" id="encode">
<textarea name="b64_place" form="encode">Enter text here:</textarea>
<input type="submit" value="Encode">
</form>
or simply remove the form="encode"
Edit based on suggestion from senior SO members,
The reason i recommend you to change the method to POST is because of the length limit of the GET method. At some point you may want to encode very large data and it may get trimmed of because of URL length limit. But with POST you don't have to worry about this restriction.
Steps to solve your issue.
If your Form and your PHP code is in the same file changethe action="index.php" to action="" and change the method="GET" to method="POST"
In text area use placeholder to tell the user what to input instead of writing it between the tags.
change $_GET to $_POST everywhere in your code.
You can copy the following code into the index.php and it will work fine.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="" method="POST">
<textarea name="b64_place" placeholder="Enter text here:"></textarea>
<input type="submit" value="Encode">
</form>
<?php
if (isset($_POST['b64_place'])) {
$base64e_text = htmlspecialchars($_POST['b64_place']);
echo $base64e_text;
}
?>
</body>
</html>

Is it possible to place two forms and save two cookie values in PHP?

I'm wondering if it would be possible to place two forms on the same page
and saving their cookie values respectively after clicking submit buttons.
With code below, when I click submit button on the first form, its cookie value is saved successfully but when I click on the second form, second value is saved but the first value is overwritten.
<?php
setcookie('username[user111]', $_POST['user111'], time()+60);
setcookie('username[user222]', $_POST['user222'], time()+60);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>name:<?php echo $_POST['user111']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user111" value="TOM">
<input type="submit" value="close">
</form>
<p>name:<?php echo $_POST['user222']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user222" value="BOB">
<input type="submit" value="close">
</form>
</body>
</html>
I'd like to save both
Name:username[user111] Value:BOB
Name:username[user222] Value:TOM
If I could save the values respectively, it'd not necessary to use form submit
but if possible I'd like to use PHP instead of JavaScript.
Any advice would be appreciated.

How to stop a session with a button?

I have a form and added a session_starts I want to track of how many times I have visited the page ..I want a Button indicating the stop session and the session should stop and start with a new session ...How do I do this?
Suggestion:
Try creating a button in a form and after submitting it change session values to what you want and the start another.
Example
<form method="POST">
<input type="submit" name="emptySession">
<form>
<?php
if(isset($_POST['emptySession']){
//you continue
}
?>
I'd have thought that something like the following ought to work- the logic can be adapted to suit whatever page you have.
<?php
session_start();
if( !empty( $_POST['reset'] ){
#session_unset();
#session_destroy();
#session_start();
#session_regenerate_id( true );
}
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<title>Sessions</title>
</head>
<body>
<form method='post'>
<input name='reset' value='Reset' type='submit' />
</form>
</body>
</html>

How to carry data from a form on one file to another using PHP sessions?

I'm trying to make a form that acts as a search engine and returns results. However, the data from the user's entry is either unable to save to a session or the session cannot be passed to another file. Here is the code for the "home" search page and the "Search-Engine" results page.
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
<?php session_register(); session_start(); ?>
<?php $_GET['query'] = $_SESSION['Query']; ?>
</body>
</html>
Search-Engine.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
<?php session_start(); ?>
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
I don't know the exact purpose of using Sessions in your form. But you are doing in a wrong way by starting Session in middle of page and using Sessions within the form. You can add value in Sessions in another page after submitting the form.
You can update your files in the below way:
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" name="query" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
</body>
</html>
Search-Engine.php
<?php session_start();
$_SESSION['Query'] = $_GET['query']; ?>
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>

Loop through php $_POST, and emailing values

I am building a confirmation page and a simple shopping cart script. But I am unable to send the values by mail using the "Send" button which I have put on the end of the page.
If I don't wrap the mailer part inside the "if" condition for the button, it works and sends the stuff on page load. But as I wrap it inside the "if" condition, I get only a blank email.
UPDATE:
Inside the previous page, wich has the "Ordering" form, I have switched from method="post" to method="get".
And in the second page,
I have changed the method inside the loop from $_POST to $_GET. As for the button kept the $_POST method together with the method="post" inside the form (on the bottom). Now it works, but I can't understand why.
First page with form:
<!DOCTYPE html>
<head>
<title>Alm Chalet Menu</title>
<link href="css/template.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h2>Alm Chalet Menu</h2>
<p>Biologische Säfte </p>
<form method="get" action="order.php">
<p><input type="number" name="orange" min="0" value="0" class="count_style">
Orange</p>
<p><input type="number" name="multivitamine" min="0" value="0" class="count_style">Multivitamine</p>
<input type="submit" name="send" value="Send Menu" />
</form>
</body>
</html>
Second, confirmation page:
<!DOCTYPE html>
<head>
<link href="css/template.css" type="text/css" rel="stylesheet" />
<title>Ordered Food</title>
</head>
<body>
Your order the following:
<table id="order_table">
<tbody>
<?php
$items = '';
foreach($_POST as $key => $value){
if ($value == 0) {
continue;
}
echo "<tr><td>$key</td><td class='value'>$value</td></tr>";
$items .= "$key: $value\n";
}
if (isset($_GET['send'])) {
$message = $items;
mail("****#yahoo.com", $subject, $message, $headers);
echo "<p>Thanks for your order!</p>";
}
?>
</tbody>
</table>
<p>
<form method="get">
<input name="send" type="submit" value="Send Order">
</form>
</p>
</body>
</html>
I assume you are sending post data from some other page to this page? If so after you post to this page then submit another form using the get method, the $_POST variable will no longer have anything in it because you submitted another form to this page (via get). You need some way of saving the posted data (perhaps a php session) so that when you click the Send Order submit button the previously posted data isn't lost.

Categories