PHP Form cant output value - php

Im new to php and tryed to output one of the input (radio) values.When i used $_POST to output i got Undefined index error/notice and when i used var_dump on $_POST i got array(0) { }.How could i output values / what might be the problem ?
require('connect.php');
?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?php
$kusimus = $db->prepare('SELECT k.kusimus FROM kusimus AS k JOIN kusimus_valik AS kv ON k.Kusimus_id = kv.Kusimus_id WHERE k.Is_active = 2');
$vastused = $db->prepare('SELECT kv.valik FROM kusimus AS k JOIN kusimus_valik AS kv ON k.Kusimus_id = kv.Kusimus_id WHERE k.Is_active = 2');
$kusimus->execute();
$vastused->execute();
$result2 = $vastused->fetchAll();
$result = $kusimus->fetchAll();
?>
<?php echo '<b>'.$result[1][0].'</b><br>'; ?>
<?php if(!isset($result2[0][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[0][0].'">'.$result2[0][0].'<br>';
} ?>
<?php if(!isset($result2[1][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[1][0].'">'.$result2[1][0].'<br>';
} ?>
<?php if(!isset($result2[1][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[2][0].'">'.$result2[2][0].'<br>';
} ?>
<?php if(!isset($result2[3][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[3][0].'">'.$result2[3][0].'<br>';
} ?>
<?php if(!isset($result2[4][0])){
echo '';
}else{
echo '<input type="radio" name="'.$result[1][0].'" value="'.$result2[4][0].'">'.$result2[4][0].'<br>';
} ?>
<br><br><br><br><br><br><br>
<input type="submit" value="Vasta">
</form>
<?php
var_dump($_POST);
?>
</body>
</html>
<?php
$db = null;
?>

You should use a switch statement instead of all of those if and else statements. You have to specify what variable you want to use var_dump on. $_POST isn't a variable. $_POST['var'] would be an example of a variable.
I seriously hope this code isn't going to be deployed.
Also, if you want more people to take you seriously, focus on improving your spelling, grammar, and punctuation before you focus on programming.

Related

Building a checkbox list from a database query and retaining the checkbox after a form submit

I am trying to build a simple form which queries a database, grabs a list of email addresses and then creates a table based on the results. What I would like it to do is retain the checked boxes after a submission but am having trouble figuring it out based on the way I've created my table. I can do it no problem if I manually build the table but that defeats the purpose. Here is the code I am working with, again the only change I would like it to do is retain the checked boxes.
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="style/style.css"/>
</head>
<body>
<?php include('include/connect.php'); ?>
<h1>This is a test</h1>
<div class="emailform">
<form action="" method="post">
<table id="emails">
<?php
while($row = $result->fetch_assoc()) {
unset($email);
$email = $row['Email'];
?>
<tr><td><input type="checkbox" name="select[]" value="<?php echo $email;?>"/><?php echo $email; ?></td></tr>
<?php
}
?>
</table>
<br/><br/>
<input id="manual" type="text" name="select[]"><br/><br/><br/>
<button type="submit" name="SubmitButton">Select Email Addresses</button>
</form>
</div>
<?php
if(isset($_POST['SubmitButton'])){
if(isset($_POST['select'])){
$shift = $_POST['select'];
if (count($shift) > 1 ){
$list = implode(", ", $shift);
echo $list;
} else {
echo "$shift[0] <br/>";
}
}
}
?>
</body>
</html>
Help would be appreciated, thanks
Just check if the current email in the loop exists in $_POST['select'], if it is, you check it, if it is not, clear the check. This check will be displayed in the input checkbox as <?php echo $checked;?> :
<?php
while($row = $result->fetch_assoc()) {
unset($email);
$email = $row['Email'];
// IF EMAIL EXISTS IN $_POST, CHECK IT.
$checked = "";
if(isset($_POST['select'])){
$shift = $_POST['select'];
$list = implode(", ", $shift);
if (strpos($list,$email)===false)
$checked = ""; // EMAIL NOT IN $_POST.
else $checked = "checked"; // EMAIL IS IN $_POST.
}
?>
<tr><td><input type="checkbox" name="select[]" <?php echo $checked;?>
value="<?php echo $email;?>"/><?php echo $email; ?></td></tr>
<?php
}
?>
Check if $row['Email'] isn't empty, then output "checked" attribute.
<?php
while($row = $result->fetch_assoc()) {
unset($email);
$email = $row['Email'];
?>
<tr><td><input type="checkbox" name="select[]" value="<?php echo $email;?>"<?php if($row['Email'] != false) { echo ' checked'; } ?>><?php echo $email; ?></td></tr>
<?php
}
?>

displaying html checkbox multiple checkbox content in php page using loop

//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php') ?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
<input type="checkbox" name="list[]" value="<?php echo "$f6";?>" /><?php echo "$f6","<br>"; ?>//display result in htmlpage
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
print.php
//after submitting in php page
<?php include('connection.php') ?>
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected){
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem
I want to display content of checkbox from html form to php page.I extracted the checkbox contents from database.problem is how I display the content in php page selecting multiple checkbox.
First of all add error_reporting in your code:
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
You have few issues in your HTML/PHP combination:
Issues:
You are using <input type="checkbox" .. inside the PHP.
your concatenation is wrong "$f6","<br>"
Also need to add name for like name="submit" in button.
Modified Code:
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?><br/>
<?php
$i++;
$d = $f6;
}
?>
Side note:
Suggestion of error_reporting is only for development and staging not for production.
Please use mysqli_* or PDO because mysql_* is deprecated and not available in PHP 7.
<?php echo "$f6","<br>"; ?>
wrong concatenation
<?php echo "$f6"."<br>"; ?>
use this
Your code should be:-
<?php
// This line should be first.
// You have missed semicolon here.
include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<form action="print.php" method="post">
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<!-- You should write below line as -->
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?> </br>
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
there are couple of errors in your codes. please find the modified codes below with my comment started with //seeyouu:
//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_num_rows($sql);//seeyouu: used wrong function>>mysql_numrows
$i=0; //seeyouu: after my amendment, probably can remove this
while ($row = mysql_fetch_array($sql)) {
//seeyouu: no such way of doing, your $sql already a result >> $f6=mysql_result($sql,$i,"item_name");
//seeyouu: forgot to close your php tag here
?>
<input type="checkbox" name="list[]" value="<?php echo $row["item_name"]; ?>" />
<?php //seeyouu: wrong>>echo "$f6","<br>";
//correct way:
echo $row["item_name"]."<br />";
//$d=$f6;i don't know what is this line for, so i leave it to you.
}
?>
<input type="submit" name="submit" value="submit"/> <!-- seeyouu: forgot to set name: submit -->
</form>
print.php
//after submitting in php page
<?php include('connection.php');
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected)
{
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem

A simple operation but it giving an error I want to update each id

It is simple but I am not getting values of radio buttons for updation when I press submit.
It is giving an error in foreach statement like :Warning: Invalid argument supplied for foreach() Please say how to get the values of radio button after pressing submit button,
here I am updating the status of state .
<?php
include("db.php");
?>
<html>
<body>
<?php
$state=$_POST['state'];
if(isset($_POST['submit']))
{
$result1 = mysql_query("select state,id,status from states ");
while($rr1=mysql_fetch_array($result1))
{
//getting values of radio buttons
$myval=$rr1['id'];
echo $myval;
$val=$_POST['yes.$rr1["id"]'];
echo $val;
$val1=$val.$myval;
$vall=yes.$rr1['id'];
foreach($vall as $values)
{
echo $values;
$update=mysql_query("UPDATE states SET status='". $values."'
WHERE id='$myval' ");
}
}
}
echo "ok";
?>
<form action="" name="form" id="form" method="post" >
<?php
//session_start();
include("db.php");
$result = mysql_query("select state,id,status from states ");
while($info1=mysql_fetch_assoc( $result))
{
echo $info1['city'];
echo "<br>";
/*echo "<br>";
echo "company Name:".$info1['company_name'];
echo "<br>";
echo "salary:".$info1['maxsalary'];
echo "<br>";
//echo $info1['company_name'];*/
?>
<label><?php echo $info1['state']; ?></label>
<input type="radio" <?php if($info1['status']=="yes"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="yes">Yes
<input type="radio" <?php if($info1['status']=="no"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="no">no
<br/>
<?php } ?>
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I think you need to write
$vall=yes.$rr1['id'];
to
$vall="yes".$rr1['id'];
Thanks

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>

more than 1 form at one page

I've problem with multiple form at one page. At page index I include 4 forms
include('./poll_1a.php');
include('./poll_2a.php');
include('./poll_3a.php');
include('./poll_4a.php');
The form code at every poll page is the same. I include some unique markers ($poll_code) for every scripts but the effect is when I use one form - the sending variable are received in the others. But I would like to work each form individually.
The variable $poll_code is unique for every script -> 1 for poll_1, 2 for poll_2 etc.
The same situation is with $cookie_name
$cookie_name = "poll_cookie_".$poll_code;
than, as I see, cookies have different names.
$poll_code = "1"; // or 2, 3, 4
?>
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="<?php echo $poll_code; ?>">
<input type="hidden" name="poll_cookie_<?php echo $poll_code; ?>" value="<?php echo $poll_code; ?>">
<table>
<?php
//print possible answers
for($i=0;$i<count($answers);$i++){
?><tr><td style="\text-allign: left;\"><input type="radio" name="vote_<?php echo $poll_code; ?>" value="<?php echo $i; ?>"> <?php echo $answers[$i]; ?></td></tr><?php
}
echo "</table>";
echo "<br>";
if ($_COOKIE["$cookie_name"] == $poll_code ) {
echo "<br> nie można głosować ponownie ...";
} else {
?>
<p><input type="submit" name="submit_<?php echo $poll_code; ?>" value="głosuj !" onClick="this.disabled = 'true';"></p>
<?php
}
?>
</form>
</p>
Q: how to make this forms to work individually at one page?
//------------------- EDIT
the receiving part of the script
$votes = file($file);
$total = 0;
$totale = 0;
$poll_cookie = 0;
if (isset($_POST["vote_$poll_code"]) && isset($_POST["poll_cookie_$poll_code"])) {
$vote = $_POST["vote_$poll_code"];
$poll_cookie = $_POST["poll_cookie_$poll_code"];
}
//submit vote
if(isset($vote)){
$votes[$vote] = $votes[$vote]+1;
}
//write votes
$handle = fopen($file,"w");
foreach($votes as $v){
$total += $v;
fputs($handle,chop($v)."\n");
}
fclose($handle);
Of course, the $file have the unique declaration too (at top of the script, under the $poll_code declaration).
$file = "poll_".$poll_code.".txt";
I think the issue might be that <?php echo $poll_code; ?> is outside the loop so maybe that it's always using the same value assigned to it, maybe put it inside the loop

Categories