Why aren't my cookies delete? - php

Hi guys I'm trying to build a form wich allow me to create cookies, and I want to be able to delete them as well, to do that there is an option at the bottom of the form that send me to my file call see_cookies.php that generate another form with some php to create a loop that will go through al the $_COOKIE array and print every checkbox with the information. Also some submit buttons with allows my to delete all or the ones I choose, but I'm not able to delete them.
Could anyone help me please.
this is the code for see_cookies.php:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ver::Cookies</title>
</head>
<body>
<h1>Cookies en el sevidor</h1>
<form action="delete_cookies.php" method="post">
<?php
foreach ($_COOKIE as $nombre => $contenido) {
print <<<pinta
<input type="checkbox" name="nombre_cookie[]" value="$nombre"/>$nombre($contenido)<br>
pinta;
}
?>
<input type="submit" value="Delete" name="delete">
<input type="submit" value="Delete_All" name="delete_all">
</form>
</body>
</html>
this is the code for delete_cookie.php
<?php
function delete_cookie($nombre){
setcookie($nombre," ",-10);
}
if(isset($_POST["delete_all"])){
foreach ($_COOKIE as $nombre => $contenido ) {
delete_cookie($nombre);
}
}elseif(isset($_POST["delete"])){
foreach ($_POST["nombre_cookie"] as $key => $contenido) {
delete_cookie($contenido);
}
}
?>

try time less than current time.
setcookie($nombre, "", time()-3600);

Your second file is named delete_cookie.php when the form action goes to delete_cookies.php. I think that's the problem.

Related

Array Value Gets Reinitialize when form is submitted using 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>

On a loop: fgets keeps reading only one line that keeps getting used in the following loop

My goal from my code (using only HTML and PHP) is to provide a page for choosing the type of question (checkbox...) with the number of inputs and display the result in a different page by reading the 3 files that has been already saved in 3 main files (file for questions, file for number of inputs, file for options content), my idea is almost.
Here's my code from the displaying page :
<!doctype html>
<html>
<head>
<title>
form du reponse
</title>
</head>
<body>
<?php
//3 files
$k=fopen("choix multiples.txt","a+");//questions
$nn=fopen("nombre.txt","a+");//number of inputs
$no=fopen("les_options_multiples.txt","a+");//options content
//from here it only reads the first line of $nn and it keeps using it on the last loop
for($lol=fgets($nn);!feof($nn);$lol=fgets($nn))
{
for($questioncm=fgets($k);!feof($k);$questioncm=fgets($k))
{
echo "$questioncm :<br/>";
for($x=1;$x<=$lol;$x++)
{
echo fgets($no);
?>
<form action="" method="POST">
<div>
<input name="haha" type="radio" value="hahaha">
</div>
<?php
}
?>
</form>
<?php
}
}
</form>
</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>

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