Issue display checkbox entity on other page - php

I have two files Add_Services.html and add_services.php.
My database is named as 'ecc'.
Database 'ecc' has a table named 'addservices'.
Table 'addservices' has fields 'id', 'name' and 'cost'.
Add_Services.html
Add_Services.html has a button which onClick opens a pop-up window.
Add_Services.html
<html>
<input type = "button" value = "Add Services"
onClick="MyWindow=window.open('add_services.php',
'MyWindow',width=600,height=300);
return false;">
</html>
add_services.php
add_services.php display the checkbox list extracted from database 'ecc' of table 'addservices'. Pop-up is displaying two fields from table addservices and those are 'cost' and 'name'.
add_services.php
<html>
<body>
<?php
$con = mysqli_connect("localhost", "root", "", "ecc");
if(!$con){
echo 'Not connected';
}
if(!mysqli_select_db($con,'ecc'))
{
echo 'db not selected';
}
$sql = "SELECT name, cost FROM addservices";
$records = mysqli_query($con, $sql);
$row=mysqli_fetch_array($records,MYSQLI_ASSOC);
?>
<table width ="800" border="1" cellspacing="1">
<tr>
<th>
Select
</th>
<th>
Name
</th>
<th>
Cost
</th>
</tr>
<form action="" method = "POST">
<?php
while($row = mysqli_fetch_assoc($records)){
echo "<tr>";
$n=$row['name'];
$t=$row['cost'];
echo "<td>";
echo "<input type='checkbox' name='chkbox[]' value='{$row["name"]}'>";
echo "</td>";
echo "<td>";
echo "$n";
echo "</td>";
echo "\t";
echo "<td>";
echo "{$row["cost"]}";
echo "</td>";
echo "<br>";
echo "</tr>";
}
?>
</table>
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
while(!empty($_POST['chkbox'])){
echo "chkbox";
}
}
?>
</body>
</html>
My Issue: I am to display the checked content of check list on page Add_Services.html
It would be grate if some one rewrite it.

The issue is : you have not defined name of Submit Button
Change Incorrect Code :
<input type="submit" value="Submit">
To Below Corrected Code :
<input type="submit" value="Submit" name="submit">
And, Also Instead of printing values of chkbox[] using while loop, Just use a simple foreach loop.
Note : Your code will go to infinite loop, as $_POST['chkbox'] will always have value
So, Change Below Code :
while(!empty($_POST['chkbox'])){
echo "chkbox";
}
To Below corrected code :
foreach($_POST['chkbox'] as $value)
echo $value."\n";

Related

HTML form in while loop, how to transfer data from each item?

I have tried to solve it and look around but not even sure what I should be searching for.
I have made a product grid through a while loop, in the loop with each product and input-tag has been used for users to mark how many items of each product is wanted.
The product grid
However I have trouble distinguishing the value of each input field and what "name" it should be stored under to be able to retrieve it when running the second script of processing the order? I also need to be able to connect an id with each value.
The code for the grid:
I know I need to make a unique name in the input name, however how and which makes sense?
<div id="content">
<h1>Products</h1>
<form action="processorder.php" method="post">
<table align="center">
<?php
$db = include "connect2db.php";
mysqli_set_charset($db,"utf8");
$query = 'SELECT * FROM products_josie';
$result = $db->query($query);
$count = 0;
while($res=$result->fetch_assoc())
{
if($count==3)
{
echo '</tr>';
$count = 0;
}
if($count==0)
echo '<tr>';
echo '<td>';
?>
<a href="productsdetails.php?clickedid=<?php echo $res['product_id']?>">
<img src="products/<?php echo $res['photo']; ?>" width="200" height="150"/>
</a>
<br/>
<?php
echo '<p>';
echo $res['product_name'];
echo '</br>';
echo 'DKK ';
echo $res['price'];
echo '</p>';
echo '<p>';
echo '<input type="number" name="amount" min="0"';
echo '</p>';
$count++;
print '</td>';
}
if($count>0)
print '</tr>';
?>
</table>
<input type="submit" value="Submit Order">
</form>
</div>
I think what you are looking for is how to get the amounts for different products. If so, something like this might do it:
$prod = $res['product_name'];
echo '<input type="number" name="amount[$prod]" min="0"';

How to delete multiple rows from mysql database with checkbox using PHP?

I try to delete my data in "admin" database, but the delete button does not function.
This is my top part
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="admin"; // Database name
$tbl_name="admin"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
This is my checkbox code
<tbody>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['course_code']; ?></td>
<td><?php echo $rows['course_name']; ?></td>
<td><?php echo $rows['lecture_id']; ?></td>
<td><input name="checkbox[]" type="checkbox"
id="checkbox[]" value="<?php echo $rows['course_code'];?>"></td>
<td><form>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
and, this is my button code
<input type='button' id="delete" value='Delete' name='delete'>
This is my php function code
<?php
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE course_code='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>
include all the input elements within your <form> tags: <form> all inputs are here </form>
update:
<input name = "checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['course_code'];?>">
to (id doesn't matter here):
<input name="checkbox[]" type="checkbox" value="<?php echo $rows['course_code'];?>"/>
and your button code:
<input type='button' id="delete" value='Delete' name='delete'>
to
<input type="submit" value="Delete"/>
set opening <form> tag to <form action="delete.php" method="post">
Note:
I assume below codes are in delete.php file. if not replace "delete.php" with that name in above opening form tag.
your delete.php file:
<?php
$cheks = implode("','", $_POST['checkbox']);
$sql = "delete from $tbl_name where course_code in ('$cheks')";
$result = mysql_query($sql) or die(mysql_error());
mysql_close();
?>
Note:
Since mysql_ will deprecate on future, better is use mysqli extension. But before use that, you have to enable it on your server. mysqli is a part of php and newer version of php has it but not enabled. To enable this, view php info page and find the path of php.ini file in "Loaded Configuration File" row on that page.
You can see php info page by loading below php file in the browser:
<?php
phpinfo();
?>
open that php.ini file in a text editor and un-comment or add a line extension=php_mysqli.dll at the extensions list there.
also search for "extension_dir" and open the directory it says and make sure php_mysqli.dll file is there.
(you may have .so extension if you not use windows OS)
Then restart your server and you are done!
By Fred -ii-
Using mysqli_ with prepared statements is indeed a better and
safer method. However, some will even suggest PDO, but even PDO
doesn't have some of the functionalities that mysqli_ offers;
strangely that. Even PDO needs sanitization. Many think that using PDO will solve injection issues, which is false.
-Thanks Fred.
try this code. it is working well.
connection.php
<?php $hostname_conection = "localhost"; /* this is the server name(assigned to variable) which is localhost since it runs on local machine */
$database_conection = "company"; /* this is the database name( assigned to variable)*/
$username_conection = "root"; /* user name (assigned to variable)*/
$password_conection = ""; /*password (assigned to variable) */
$conection = mysql_connect($hostname_conection, $username_conection, $password_conection) or trigger_error(mysql_error(),E_USER_ERROR); /* Mysql_connect function is used to conncet with database it takes three parameters server/hostname, username,and password*/
mysql_select_db($database_conection,$conection) or die(mysql_error("could not connect to database!")); /* Mysql_select is used to select the database it takes two parameters databasename and connection variable in this case $conection */
?>
multiple_delete.php
<?php require_once('conection.php'); ?>
<?php
in
/* now to display the data from the database which we inserted in above form we */ /* we make the query to select data from the table EMP */
$display = "select * from test_mysql";
$result = mysql_query($display, $conection) or die(mysql_error()); /* the query is executed and result of the query is stored in variable $result */
if ($result == FALSE) {
die(mysql_error()); /* displays error */
} ?> <h1 align="center"> Displaying Recods in Table </h1>
<form method="get" action="" id="deleteform" >
<table width="245" border="1" align="center">
<tr>
<td width="51">
<input type="submit" name="delete" id="button" value="delete" onclick="document.getElementById('deleteform').action = 'delete.php';document.getElementById('deleteform').submit();"/> <!--- here on clicking the button the form is submitted and action is set to delete.php Here we have used javaScript document refers to this whole page and now we can access any tag that has its id with help of getElementById() method and after the we specify the operation we want to perform in this case action and submit. --->
</td>
<td width="50">id</td>
<td width="55">name</td>
<td width="47">lastname</td>
</tr>
<?php
while ($rows = mysql_fetch_array($result))
{ /* here we make use of the while loop which fetch the data from the $result int array form and stores in $row now we can display each field from the table with $row[‘field_name’] as below */
?>
<tr>
<td>
<input type="checkbox" name="empids[]" value="<?php echo $rows['id']; ?>" /> <!--here with each checkbox we send the id of the record in the empids[] array --->
</td>
<td>
<?php echo $rows['id'] ?>
</td>
<td>
<?php echo $rows['lastname'] ?>
</td>
<td><?php echo $rows['name'] ?></td>
<?php } ?>
</tr>
</table>
</form> ?>
</body>
</html>
delete.php
<?php
require_once('conection.php');
?>
<?php
if (isset($_GET['delete'])) /* checks weather $_GET['delete'] is set*/
{
if (isset($_GET['empids'])) /* checks weather $_GET['empids'] is set */
{
$checkbox = $_GET['empids']; /* value is stored in $checbox variable */
if (is_array($checkbox))
{
foreach ($checkbox as $key => $your_slected_id) /* for each loop is used to get id and that id is used to delete the record below */
{
$q="DELETE FROM test_mysql WHERE id=$your_slected_id "; /* Sql query to delete the records whose id is equal to $your_slected_id */
mysql_query($q,$conection) ; /* runs the query */
}
header("location:multiple_delete.php"); /* Goes back to index.php */
}
} else
{
echo" you have not selected reords .. to delete";
}
} ?>
$sql = "SELECT * FROM blacklist";
$result = $link->query($sql);
$count=mysqli_num_rows($result);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{
echo "<table>";
echo "<th>";
echo "<td>" . "ID: " . $row["id"]."</td>";
echo "<td>" . " Dial Target: " . $row["dial_target"]."</td>";
echo "<td>" . " Destination: " . $row["pozn"]."</td>";
echo "<td>" . " Date: " . $row["block_date"] . "</td>";
echo "<td>" . "<div class='background' style='position: relative; top:8px;'>" . "<form>" . "<input action='index.php' method='post' type='checkbox' name='chechbox[]' value='".$row["id"]."'/>" ."</form>" . "</div>" . "</td>";
echo "</th>";
echo "</table>";
echo "</br>";
}
}
else
{
echo "0 results";
}
if(isset($_POST['Delete']))
{
for($i=0;$i<$count;$i++)
{
$del_id = $checkbox[$i];
$del = "DELETE FROM blacklist WHERE Delete='$del_id'";
$result = $link->query($del);
}
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
<!-- DELETE BUTTON -->
<form>
<input type='Submit' id="Delete" value='Delete' name='Delete'/>
</form>
<?php
$args1 = array(
'role' => 'Vendor',
'orderby' => 'user_nicename',
'exclude' => $user_id.',1',
'order' => 'ASC'
);
$subscribers = get_users($args1); foreach ($subscribers as $user) {
$fvendorck = $wpdb->get_row("select * from wp_vandor where parent_id = '".$user_id."' and child_id = '".$user->id."'");
$isfavvendor = $fvendorck->child_id;
if(!empty($isfavvendor)) {
?>
<li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" checked=""/><?php echo $user->headline; ?></li>
<?php }else{ ?>
<li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" /><?php echo $user->headline; ?></li>
<?php } }?>
</ul>

how to print the checklist array value

I want to print the checklist array value, here checklist is dynamically created for each row of Food table. How I can do that ? If I click submit no value is submitted I guess so, How to use submit button? In fact where to use ..plz help me
<?php // File: anyco.php
require('anyco_ui.inc.php');
// Create a database connection
$conn = oci_connect('system','123','localhost/orcl');
ui_print_header('FoodItemList');
//session_start();
//$cid=$_SESSION['cid'];
//do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item');
ui_print_footer(date('Y-m-d H:i:s'));
function do_query($conn, $query)
{
$stid = oci_parse($conn, $query);
$r = oci_execute($stid,OCI_DEFAULT);
print '<table border="1">';
print '<tr>';
print '<td>Food_ID<td>Food_Name<td>Price(tk)<td>Dvailable_day<td>Avaliable_time<td>Discount<td>Dis_start date<td>Dis_finish date<td>selected item<td>quanity';
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
print '<tr>';
$num=1;
$val="";
foreach ($row as $item)
{
if($num==1)
{
$val = $item;
$num=2;
}
print '<td>'.($item!== null ? htmlentities($item) : ' ').'</td>';
}
//echo$val;
echo '<td><input type="checkbox" name = "invite[]" value="$val>" </td>';
echo '<td><input type="number" name = "name[]" ></td>';
print '</tr>';
}
print '</table>';
}
if(isset($_POST['submit']))
{
if (is_array($_POST['invite']))
{
foreach($_POST['invite'] as $key=>$name)
{
echo $key, '=>', $name,'<br/>';
//Here $key is the array index and $name is the value of the checkbox
}
}
}
?>
<html>
<style>
body
{
background:orange;
}
</style>
<body text="green">
<br><br>
<form method="post">
<?php do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item'); ?>
<input type ="submit" value="submit" name="submit"><br><br>
</form>
</body>
</html>
Your form doesn't have any inputs inside it because you are calling your function way before you print out the form tags.
This line needs to move to within the form:
do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item');
So that the code looks like this, and the whole table with its inputs is printed inside the form tags. I also added a name for the submit button:
<form method="post">
<?php do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item'); ?>
<input type ="submit" value="submit" name="submit"><br><br>
</form>
Finally, when you echo values into your inputs, there are two syntax errors in this line:
echo '<td><input type="checkbox" name = "invite[]" value="$val>" </td>';
You can echo a variable into a string in several ways - here are two. 1) if the variable is between single quotes, PHP will automatically convert the variable to its value. In this case, it's double quotes, so no go. 2) You start your echo command with single quotes, so you can break out of the string with a single quote, concatenate the variable with a period, and then break back in to the string with another single quote, like this:
echo '<td><input type="checkbox" name = "invite[]" value="' . $val . '"> </td>';

php checkbox from mysql

I've managed to pull records from a mysql database using php with a checkbox to select. I'm struggling to make the selected records (with the checkboxes) appear on a new page. Here is my code so far:
<?php
include('connect.php');
$query = 'SELECT * FROM grades';
if ($r = mysql_query($query)) {
print "<form>
<table>";
while ($row = mysql_fetch_array($r)) {
print
"<tr>
<td>{$row['InstitutionName']}</td>
<td>{$row['InstitutionAddress']}</td>
<td>{$row['SubjectArea']}</td>
<td><input type='checkbox' name='check[$row{['GradeID']}] value='check' /></td>
</tr>";
}
print "</table>
</form>";
$checkbox[] = isset($_POST['checkbox']) ? true : false;
} else {
print '<p style="color: blue">Error!</p>';
}
?>
<html>
<form action="check2.php" method="POST">
<input type='submit' name='Submit' value='Submit'>
</html>
And on the page where I want the selected records to display, I have:
<?php
if(isset($checkbox))
{
foreach($checkbox as $value)
{
echo $value."<br>"; //it will print the value of your checkbox that you checked
}
}
?>
Any assistance would be appreciated!
Put checked="checked" just like you have put value="check":
<td><input type='checkbox' name='check[$row{['GradeID']}] value='check' checked="checked" /></td>
Edit:
Probably I misunderstood you. What do you expect to see?
First, you should make the form to perform POST request instead of GET (default):
print "<form method='POST' action='THE_FILE_YOU_PRINT_RESULTS.php'>"
Then in "THE_FILE_YOU_PRINT_RESULTS.php" do a print_r($_POST); to see what you get and how to display it.
A better way to do this would be to name your checkboxes check[]. Each checkbox value should then be the ID (rather than check).
Then on your results page, just loop through each instance of check[] and print the value out.
check[$row{['GradeID']}]
Seems incorrect to me, should it be:
check[{$row['GradeID']}]
Notice the moved opening curly bracket to before the $
Sorry if this is wrong haven't used PHP in long time but it stood out for me
The are a couple of error in the form print function.
You have to put the action on the first form, not on the second, you have to change the way how you print the checkbox as well.
Try to print the form in this way:
<?php
include('connect.php');
$query = 'SELECT * FROM grades';
if ($r = mysql_query($query)) {
print "
<form action=\"check2.php\" method=\"POST\">
<table>";
while ($row = mysql_fetch_array($r)) {
print
"<tr>
<td>{$row['InstitutionName']}</td>
<td>{$row['InstitutionAddress']}</td>
<td>{$row['SubjectArea']}</td>
<td><input type='checkbox' name='check[".$row['GradeID']."] value='".$row['GradeID']."' /></td>
</tr>";
}
print "</table>
<input type='submit' name='Submit' value='Submit'>
</form>";
$checkbox[] = isset($_POST['checkbox']) ? true : false;
} else {
print '<p style="color: blue">Error!</p>';
}
?>
And print the checked box reading the _REQUEST array:
<?php
if(isset($_REQUEST["check"]))
{
foreach($_REQUEST["check"] as $key=>$value)
{
echo $key."<br>"; //it will print the value of your checkbox that you checked
}
}
?>
This should work.

HTML Radiobutton form not POSTing

I have written a small HTML form and added it to the page. My goal is for it to POST the value of the Checked button to a PHP page which I have also written. The PHP page is not getting the value for some reason. I am also not getting any PHP errors. The codes are below.
form.php
<form action="http://www.zbrowntechnology.com/InsaneBrain/quiz.php" method="POST">
<font color="white" size="3">
<?php
$con = mysql_connect("HOST", "USER", "PASS");
if(!$con) {
die('Unable to connect to MySQL: '.mysql_error());
}
mysql_select_db("zach_insaneB", $con);
$result = mysql_query("SELECT Name FROM quiz");
while($row = mysql_fetch_assoc($result)) {
$qname = $row['Name'];
echo "<input type='radio' name='button1' id='$qname'>";
echo "<label for='$qname'><font color='white'/>$qname</font></label>";
}
?>
</font>
</div>
</div>
<div id="Oobj12">
<div id="Gcode234" class="dfltc">
<input type="image" src="http://www.zbrowntechnology.com/InsaneBrain/begin.png" alt="Begin" />
</form></div>
</div>
getdata.php
<?php
$data = $_POST['button1'];
echo $data;
?>
Actually, I see the problem...you don't actually have a value in the radio button. You need something like:
echo "<input type='radio' name='button1' id='$qname' value='$some_value'>";

Categories