PHP Checkbox from db, unchecked after submit - php

I have this code. I need that all the checkbox (taken by database) that I choose remain checked even after submitting the page.How can I do that?
?>
<?php
function connetti(){
$conn=mysql_connect("localhost","user","pass");
mysql_select_db('colours');
return $conn;
}
?>
<html>
<head>
<title>Scelta colori</title>
<meta charset="utf-8">
</head>
<body>
<h1>Scelta colori</h1>
<h1>Benvenuto <?php echo $_SESSION['user']; ?></h1><br>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<?php
$conn=connetti();
$sql="SELECT tonalita FROM colori";
$risultato=mysql_query($sql);
while ($row=mysql_fetch_array($risultato)){
$valore=$row['tonalita'];
echo('<input type="checkbox" value="'.$valore.'" name="colori[]">'.$valore.'</input><br>');
}
mysql_free_result($risultato);
mysql_close($conn);
?>
<input type="submit" value="Invia">
<input type="reset" value="Annulla">
</form>
</body>
</html>

u can use, after submitting the form, the $_POST vars and check it with $valore
<?php
echo('<input type="checkbox" value="'.$valore.'" name="colori['.$valore.']"'.((isset($_POST["colori"][$valore])&&$_POST["colori"][$valore]==$valore)?' checked="checked"':"").'>'.$valore.'</input><br>');
?>
untestet but should work
edit: got the error on the name-attr and fixed issue on not settet index. testet on php5

Related

PHP Submit button doesn't have any effect (PhpStorm)

I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question

Delete article with submit button

i have a problem with deleting the articles. I want it to delete after click on submit button but i dont know how. I do not want use javascript to autosubmit form in select tag. Can u help me ? I would appriciate that. Thanks.
<?php
session_start();
include_once('../includes/conn.php');
include_once('../includes/article.php');
$article= new Article;
if(isset($_SESSION['logged_in'])){
if(isset($_POST['id'])){
$id=$_POST['id'];
$query=$pdo->prepare('DELETE FROM articles WHERE article_id=?');
$query->bindValue(1, $id);
$query->execute();
header('Location: delete.php');
}
$articles=$article->fetch_all();
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
CMS
<br/>
<h4>Delete article:</h4>
<form action="delete.php" method="post" name="id">
<select>
<?php foreach($articles as $article){?>
<option value="<?php echo $article['article_id']; ?>"><?php echo $article['article_title']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Delete article">
</form>
</div>
</body>
?>
According to your code, shift name="id" from form to select.
You are trying to get selected id of drop down, so the name should be given to select.
<form action="delete.php" method="post" name="id">
<select>
to
<form action="delete.php" method="post">
<select name="id">
Another issue noticed in your code is, you should terminate execution
immediately after header('Location: delete.php'); with die(); or
exit(); to ensure remaining lines should not be executed. In PHP,
header() is just a function which helps in setting header, and here
you are settling Location header, which further handled by browser
for taking necessary action (here redirection). So, header() does
not ensure stopping execution of remaining code.
add <?php echo $_SERVER['PHP_SELF']; ?> to the form instead of delete.php to execute the php code above and i assume the file looks as your post .
and add name="id" to select element to grap the post value to manipulate instead of form.
i hope this works according to what you provide in your post.
session_start();
include_once('../includes/conn.php');
include_once('../includes/article.php');
$article= new Article;
if(isset($_SESSION['logged_in'])){
if(isset($_POST['id'])){
$id=$_POST['id'];
$query=$pdo->prepare('DELETE FROM articles WHERE article_id=?');
$query->bindValue(1, $id);
$query->execute();
header('Location: delete.php');
}
$articles=$article->fetch_all();
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
CMS
<br/>
<h4>Delete article:</h4>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="id">
<?php foreach($articles as $article){?>
<option value="<?php echo $article['article_id']; ?>"><?php echo $article['article_title']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Delete article">
</form>
</div>
</body>

Loading new page after php code has run?

I have a form, this form is on a page called question1.php, and I want it to load question2.php when the submit button is pressed.
<form action="question2.php" method="post">
<input type="radio" name="ans" value="cuboid">
<input type="radio" name="ans" value="cone">
<input type="radio" name="ans" value="cylinder">
<input type="radio" name="ans" value="sphere">
<input type="submit" value="submit" name="submit">
</form>
But I also have this php code
<?php
if(isset($_POST['submit'])) {
if(isset( $_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "cuboid") {
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
}
}
}
?>
EDIT: I have made a simpler demo to try and explain myself better, i have got three pages.
page1.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Echo session variables that were set on previous page
if(isset($_POST['submit'])) {
if(isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
?>
</body>
</html>
And page3.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>
So on the first page I declare the session variable "favcolor", then on the second page if the user selects the radio button I want to update the color to red, however it just won't chane for me, on the third page it is still printing green
You had issue on page2.php because you are submitting form on page3.php but you are handling submission request on page2.php. So i corrected below for you.
page1.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php" method="post">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
page3.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
if (isset($_POST['submit'])) {
if (isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if ($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>
I hope this should work.
$_SESSION["cuboid"] = isset($_SESSION["cuboid"]) ? $_SESSION["cuboid"] : 0;
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
From what I can tell, you are attempting to redirect to another webpage.
Try sending HTTP headers using the header() function.
header('Location: http://example.com/page3.php');
exit();
If the HTTP header doesn't work at first, turn on the output buffer using ob_start().
You are checking for $_POST['ans'] in the wrong page.
Currently you are checking for 'ans' when user enters page2.php, which is before the user submits the form that contains that input element.
When you submit the form on page2.php, the action sends the request over to page3.php, so you actually want to move your if statement that sets the color to green to the top (below session start) of page3.php

I can't transport HTML information to PHP

My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>

How to make "MadLibs" form results appear below form fields after submitting?

Thanks in advance for your help. I've searched a lot before posting this but I end up more confused than when I started :)
I'm trying to have one page contain the form fields and after pressing submit, the resulting story with user's form field entries inserted into the story.
It would be great to have the text from the form fields remain so that the user doesn't need to retype everything if they need to change a word or two.
I really appreciate your help. Hopefully this will help many people at once.
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php if (isset($_POST['action']) && $_POST['action'] == "show"): ?>
<p>Hello, I am a <?php echo $_POST['adj'] ?> computer that owns a <?php echo $_POST['noun'] ?>.</p>
<?php else : ?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj"></p>
**strong text** <p>A noun: <input type="text" name="noun"></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php endif ?>
</body>
</html>
As you said you don't want to "keep it simple", you may simply add the needed value attribute to each of your <input>s, like this:
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php
if (isset($_POST['action']) && $_POST['action'] == "show") {
?>
<p>Hello, I am a <?php echo #$_POST['adj']; ?> computer that owns a <?php echo #$_POST['noun']; ?>.</p>
<?php
} else {
?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj" value="<?php echo #$_POST['adj']"; ?> /></p>
**strong text**
<p>A noun: <input type="text" name="noun" value="<?php echo #$_POST['noun']"; ?> /></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php
}
?>
</body>
</html>
Note the (sometimes unloved) "#" to prevent firing a notice when $_POST['...'] doesn't exist yet. I also added the same in your <p>Hello... line.

Categories