displaying html checkbox multiple checkbox content in php page using loop - php

//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

Related

Getting a value from a database and echoing from another file

I am trying to Select a row from a database table and display the variables in another PHP file. Right now I have to trying just one variable, but will change to outputting a table later on. The error is an Undefined Variable:output.
I have tried using session but it prints out the array and doesn't go all the way through. I really just want the variable so that I can put them into a table easier.
My store file, which shows a search bar and where I am trying to display the results, under the search bar.
<?php
require "header.php";
?>
<main>
<form action="includes/itemsearch.php" method="get">
Please enter the Item you are looking for:<br>
<input type="text" name="item" placeholder="Search for an item...">
<br><br>
<button type="submit" name="search-submit">Search</button>
</form>
<?php echo $output ?>
</main>
<?php
require "footer.php";
?>
My item search file:
<?php
if (isset($_GET['search-submit'])) {
require 'connect.php';
$item = $_GET['item'];
$output = "";
$sql = "SELECT * FROM Product";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)){
$pName =$row['Product_Name'];
$manufacture=$row['Manufacture'];
$quantity=$row['Quantity'];
$price=$row['Price'];
$description=$row['Description'];
echo"<p>$pName</p><br />";
}
header("Location: ../store.php?");
}
I currently get an undefined variable error.
use post method instead of get
<?php
if (isset($_POST['search-submit'])) {
}
?>
$output is undefined because you initialize it on click of submit button so <?php echo $output ?> is executing first then it gets initialized.
try
<?php
if (isset($_GET['search-submit'])){
echo $output;
}
?>
to get output just require search file in store.php
// store.php
<?php
require "header.php";
require "search.php";
?>
<main>
<form action="" method="get">
Please enter the Item you are looking for:<br>
<input type="text" name="item" placeholder="Search for an item...">
<br><br>
<button type="submit" name="search-submit">Search</button>
</form>
<?php echo $output ?>
</main>
<?php
require "footer.php";
?>
this is the final code
// search.php
<?php
if (isset($_GET['search-submit'])) {
require 'connect.php';
$item = $_GET['item'];
$output = "";
$sql = "SELECT * FROM Product";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)){
$pName =$row['Product_Name'];
$manufacture=$row['Manufacture'];
$quantity=$row['Quantity'];
$price=$row['Price'];
$description=$row['Description'];
$output .= "<p>$pName</p><br />";
}
}
?>
// store.php
<?php
require "header.php";
require "search.php";
?>
<main>
<form action="" method="get">
Please enter the Item you are looking for:<br>
<input type="text" name="item" placeholder="Search for an item...">
<br><br>
<button type="submit" name="search-submit">Search</button>
</form>
<?php echo isset($_GET['search-submit']) ? $output : '' ?>
</main>
<?php
require "footer.php";
?>

PHP Form cant output value

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.

How to submit values from form

I have a form that is being created dynamically
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{ ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<?}?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
</form>
But i a not able to understand how to carry the value of input to the addtable.php page
Can anyone please tell how to submit these values from this form
In your addtable.php, you need to print_r $_POST and see what inputs you're getting.
// addtable.php
echo "<pre>";
print_r($_POST);
print_r($_FILES); // If you're expecting a file input
Also, you should modify your HTML a little:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_assoc($result)) { ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<? } ?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button> <!--Place submit button outside loop -->
<?php } ?>
I think there is issue in submit button, you looping submit button with same name, try to put submit button out of while loop.
One of the reasons your PHP is not functioning correctly is because your PHP block, is not placed inside of PHP tags.
Your code should look like this:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php // added the PHP tags
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
?> // escaped the php tag
<input type="text" placeholder="<?= $row['heading']; ?>" name="<?= $row['heading']; ?>" />
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
}
?>
Notice how I also escaped PHP when I wanted to display any HTML. Of course, you could simply echo these out, but you can do this too! Personal preference, really.
Another little alteration I made was, rather than using <?php echo ... ?> you can simply use <?= $row['...']; ?>. It is faster and cleaner too!
In your addtable.php:
<?php
$inputValue = $_POST['input_name'];
//the $inputValue is the input value that you posted from form.
echo $inputValue;
?>
To receive data in addtable.php you have to get them by name property of inputs in the form. So you have to define the names of inputs or to retrieve them again from db in addtable.php
try this ,
addtable.php
<?php
foreach ($_POST as $key => $value){
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
?>
i hope it will be helpful.
You can create array of heading and access it in php:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<input type="text" placeholder="<?php echo $row['heading']; ?>" name="heading[]"> />
<?
}
?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
?>
</form>
In addtable.php:
$headingArray = $_POST['heading']; // here you will get all the heading in array format

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 display radio button value using PHP

So I have a form that users fill out with some radio buttons. The values from the radio buttons get passed to MySQL. I now want to pull those values from the database, display them in a table on a different page, and apply different styles to them with span tags.
Here's the code from the form:
<input class="radio_style" type="radio" name="job_type" value="fulltime"/>Full-time<br/>
<input class="radio_style" type="radio" name="job_type" value="parttime"/>Part-time<br />
Here's the code for the page where I want to display it:
<div class='job_type_div'>
<?php if($job_type=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php if($job_type=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
<?php } ?>
</div>
So ideally, the "fulltime" value will have one style and the "parttime" value will have another style. But when I try running this code, nothing happens. I'm definitely connecting to the database correctly. And the row name is properly labelled "job_type". Any ideas on where I might be going wrong? Any help would be greatly appreciated :)
First of all, your form should be something like so:
<form action="page_you_want_to_display.php" method="POST">
<label for="type">Job Type:</label>
<label for="fulltime">
<input class="radio_style" id="fulltime" name="job_type" type="radio" value="fulltime">
Fulltime
</label>
<label for="parttime">
<input class="radio_style" id="parttime" name="job_type" type="radio" value="parttime">
Part Time
</label>
<input name="submitted" type="submit" value="Submit">
</form>
The page you want to display on should look something like this:
if(isset($_POST["submitted"])){
$job_type = $_POST['job_type'];
echo '<div class="job_type_div">';
if($job_type=='fulltime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='fulltime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="fulltime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
} elseif ($job_type=='parttime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='parttime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="parttime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
}
echo '</div>';
}
and CSS:
.fulltime {
margin:0px;
padding:5px;
width:300px;
background:#9C0;
color:#fff;
}
.parttime {
margin:0px;
padding:5px;
width:300px;
background:#069;
color:#fff;
}
Tested:
Hope this helps
may be problem in your php. Is there some logic?
$job_type=null;
if($job_type=='fulltime'){
...
if($job_type=='parttime'){
...
}
}
did you set $job_type variable? May be you need something like this:
<div class='job_type_div'>
<?php if($row['job_type']=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php } elseif($row['job_type']=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
</div>
I don't believe that the conditions will work the way you implemented it, try doing it like this:
<?php
echo "<div class='job_type_div'>";
if($job_type=='fulltime') {
echo "<span class='job_type_style'>"
//etc...
While you fetching your array from Data Base you need to use MYSQL_BOTH, to fetch columns by Name.
mysql_fetch_array($res, MYSQL_BOTH)
So you should have something like this:
$job_type_query = "SELECT * FROM `job`; ";
$res = mysql_query($job_type_query) or die(mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_BOTH))
{
echo $row['job_type'];
}
form.php
if you initially set one 'selected' in your form, dont need to check if its set, simple set db like so:
...
mysql_query("UPDATE X SET (job_type='{$_GET['job_type']}') WHERE Y");
...
display.php
As you will probably be making a stylesheet, reference the selectors with the job_type labels, which you put in your database
while($row = mysql_fetch_assoc($resultsource))
echo "<div class='job_type_div'> <span class='{$row['job_type']}_style'>{$row['job_type']}</span>";

Categories