checkbox related issue in php and html - php

I want to print the check box values(only the selected value) but the following error are shown
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
localhost
Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
here is the entire php file ... i am new in php ... so plz check my code..there may b some error ...
<?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'));
// Execute query and display results
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 '<td><input type="checkbox" name="ticked[]" value="$val"></td>';
echo '<td><input type="number" name ="name[]" ></td>';
print '</tr>';
}
print '</table>';
}
if(isset($_POST['submit'])&&)
{
echo'yeeeeeeeeeeeeeeee';
$ticked = $_POST['ticked'];
foreach ($ticked as $ticked=>$value)
{
echo "values selected are : ".$value."<br />";
}
}
?>
<html>
<style>
body
{
background:orange;
}
</style>
<body text="green">
<br><br>
<form action="<?php echo $current_file;?>" method="POST">
<input type ="submit" value="Submit"><br><br>
</form>
</body>
</html>

First thing to do is remove &&
if(isset($_POST['submit'])&&)
^ remove the &&
then change this
<input type ="submit" value="Submit"><br><br>
to
<input type="submit" name="submit" value="Submit"><br><br>
because isset($_POST['submit']) is looking for a name=""
<?php
if(isset($_POST['submit']))
{
echo'yeeeeeeeeeeeeeeee';
// more of your code...
}
?>
Edit
remove
action="<?php echo $current_file;?>"
from
<form action="<?php echo $current_file;?>" method="POST">
so it should be just
<form method="POST">

Related

php: Unable to take value from the user by using submit button

This is my db : link link
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM org_insert");
echo "<!doctype html>
<html lang=\"en\">
<head>
<!-- Bootstrap CSS -->
<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\">
<title>Hello, world!</title>
</head>
<body>
<table border='1'>
<tr>
<th>below_whom</th>
<th>name</th>
</tr>";
$row = mysqli_fetch_array($result);
#echo '<pre>'; print_r($row); echo '</pre>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['below_whom'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<div class="form-group">
<label for="usr">below_whom:</label>
<input type="text" name ="below_whom" id="below_whom" class="form-control">
</div>
<div class="form-group">
<label for="usr">name:</label>
<input type="text" name ="name" id="name" class="form-control">
</div>
<form method="post">
<input type="button" name="submit" id="submit" class="btn btn-primary" value="submit"/>
</form>
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit']))
{
if($below_whom !=''||$name !=''){
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
mysqli_close($con);
echo"</body>
</html>";
?>
The text under p tag isn't getting executed, ie, the program is not going inside the if statement itself. I have rechecked the syntax, what is the problem? Is the syntax incorrect? I am pretty sure the connection with sql is correct. I have also refereed to some articles, still I am stuck here.
use post variables before if loop as shown below
if(isset($_POST['submit']))
{
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
if($below_whom !=''||$name !=''){
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
and in HTML Code add type as submit and start form tag before div as
<form method="post" action=""> and closes after input tag
<input type="submit" name="submit" id="submit" class="btn btn-primary" value="submit"/>
One of the issues that I am able to see is that Your query should be:
$query=mysqli_query("INSERT INTO `org_insert`(`below_whom`,`name`) VALUES ('$below_whom','$name')");
Hope this helps.
Change the mysqli_fetch_array to mysqli_fetch_assoc or add a parameter too
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

getting error when post select value

I have a small problem.
<select name="level_id"> is not posting.
So i get an error like : Undefined index : level_id
Exactly what can i do?
$sql1 = 'SELECT T_ABILITY.PK AS AB_PK,T_ABILITY.ABILITY_NAME AS AN,T_ABILITY_LEVEL.PK AS LE_PK,T_ABILITY_LEVEL.LEVEL_NAME AS LN
FROM T_USER_ABILITY_REL,T_ABILITY,T_ABILITY_LEVEL WHERE
T_USER_ABILITY_REL.ABILITY_FK = T_ABILITY.PK AND
T_USER_ABILITY_REL.ABILITY_LEVEL_FK = T_ABILITY_LEVEL.PK AND
T_USER_ABILITY_REL.USER_FK = '.$user_id.'
ORDER BY AN';
$stmt1 = oci_parse($conn, $sql1);
$r1 = oci_execute($stmt1);
while ($row1 = oci_fetch_array($stmt1, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<form method="post">';
echo '<tr>';
echo '<td>'.$row1["AN"].'</td>';
echo '<input type="hidden" name="ability_id" value="'.$row1["AB_PK"].'"/>';
echo '<td class="select-level">';
$sql2 = 'SELECT PK,LEVEL_NAME FROM T_ABILITY_LEVEL ORDER BY LEVEL_ORDER';
$stmt2 = oci_parse($conn, $sql2);
$r2 = oci_execute($stmt2);
echo '<select name="level_id" class="form-control selectpicker" data-container="body" data-live-search="true" data-size="5" title="Seviye Seçiniz">';
while ($row2 = oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<option '.($row2["PK"] == $row1["LE_PK"] ? 'selected="selected"' : "").' value="'.$row2["PK"].'">'.$row2["LEVEL_NAME"].'</option>';
}
echo '</select>';
echo '<button type="submit" name="update-user-ability" class="btn btn-success">Güncelle</button>';
echo '<button type="submit" name="delete-user-ability" class="btn btn-danger">Sil</button>';
echo '</td>';
echo '</tr>';
echo '</form>';
}
submit part below
if (isset($_POST["update-user-ability"])) {
$user_id = $_GET["user_id"];
$ability_id = $_POST["ability_id"];
$level_id = $_POST['level_id'];
This is because your select is empty (that is, the oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC returns no rows.)
A select, when no option are present, will not return a value to the receiving php script.
Add a default option before the loop to be sure something is passed even if your sql query returns nothing.
this script will return an empty post:
<html>
<body>
<form method="post">
<select name="select"></select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(0) { }
while this one will have the value 12 defined:
<html>
<body>
<form method="post">
<select name="select">
<option value="12">12</option>
</select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(1) { ["select"]=> string(2) "12" }
Could be that the value of level_id isn't send along, because it is a <select>. Not sure, but could be that only <input /> values are properly posted.
You could use a <input type="hidden" name="level_id_val" /> and update its value with JavaScript every time the value of <select name="level_id"> is changed (using .onchange).
Then, in PHP, use that field instead of the select: $level_id = $_POST['level_id_val'];

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>';

function running in foreach loop

thank you for taking time to look at this. I have been dealing with this annoying foreach loop. Here is what I am trying to do.
I have the first page "form.php".
On this page I have check boxes. Each check box is generated from a database table. Here is the code:
<?php
include("config.php");
$mysqli = new mysqli($host, $db_uname, $db_pass, $db);
$query = "SELECT * FROM `plugins` WHERE 1";
if ($result = $mysqli->query($query)) {
echo '<form action="test.php" method="post">
<input name="gname" placeholder="Group Name..."/>
<table width="200">
';
while ($row = $result->fetch_assoc()) {
echo '<tr><td>
<label>
<input type="checkbox" value="'.$row["plugin"].'" name="checkbox[]">
'.$row["plugin"].'
</label>
</td></tr>';
}
echo '
</table>
<select name="permplugin">
<option>Select One...</option>';
$query2 = "SELECT * FROM `permission_types` WHERE 1";
if ($result2 = $mysqli->query($query2)) {
echo '<h3>Select Permission format below</h3><hr />';
while ($row2 = $result2->fetch_assoc()) {
echo '
<option value="'.$row2["plugin_name"].'">'.$row2["plugin_name"].'</option>';
}
echo '
</select>
<br />
<input name="" type="reset"><input name="" type="submit">
</form>';
}
}
?>
Now after that it sends the checked boxes to "test.php"
here is the code for that:
<?php
if(!empty($_POST['checkbox']) || !empty($_POST['select']) || !empty($_POST['gname'])) {
echo '<h1>'.$_POST['gname'].'</h1>';
$check1 = $_POST['checkbox'];
foreach($check1 as $check) {
include "functions.php";
checkboxes($check);
}
echo '<h3>Selected Permission format below</h3><hr />';
echo $_POST['permplugin'];
} else {
echo "please select atleast one plugin.";
}
?>
The functions page code looks like this:
<?php
//all functions are here.
function checkboxes($check){
$mysqli_perm = new mysqli("localhost", "uname", "pword", "tcordero_permnodes");
$query_perm = "SELECT * FROM permission_nodes WHERE plugin = `$check`";
if ($result_perm = $mysqli_perm->query($query_perm)) {
echo $check;
/* fetch associative array */
while ($row_perm = $result_perm->fetch_assoc()) {
echo $row_perm['node'].'<br />';
}
unset($check);
}
}
When I run the test.php I get this error:
Fatal error: Cannot redeclare checkboxes() (previously declared in C:\xampp\htdocs\TPYC\functions.php:3) in C:\xampp\htdocs\TPYC\functions.php on line 15
What am I doing wrong?
You need to take the include out of the foreach loop. Try this:
include "functions.php";
foreach($check1 as $check) {
checkboxes($check);
}

php form submit - Q2

i am sorry for the dummy question. Here is my simple PHP form with two SQL tables and with the ADD submit button I would like to move people from Test1 to Test2. Many thing are fine:( only the submit button does't work therefore no feedback from Test2 table.
Revised: Submit now works great
Q2 - still don't get the checkboxs to work:( - please
Could somebody show me how to track back such an error like this please?
<?php include("db_connect.php");?>
<html>
<head></head>
<body>
<form method="post" action="moving.php">
<table border="1">
<tr>
<td>
<?php
$left='SELECT * FROM test1 ORDER BY name ASC';
$result1=mysql_query($left);
$count=mysql_num_rows($result1);
while($resulta = mysql_fetch_array($result1)) {
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $resulta['id']; ?>"/> <? echo $resulta['name']; ?>
<br />
<?php } ?>
</td>
<td><input type="submit" id="add" name="add" value="Add" /></td>
<td>
<?php
$rigth='SELECT * FROM test2,test1 WHERE test2.collect=test1.id ORDER BY test1.name ASC';
$result2=mysql_query($right);
while($resultb = mysql_fetch_array($result2)) {
echo $resultb['id'] ;
echo "<br />";
}
?>
</td>
</tr>
</table>
<?php
// Check if add button active, start this
if (isset($_POST['add'])) {
for ($i=0;$i<$count;$i++) {
$add_id = $checkbox[$i];
if ($add_id=='1') {
$sql = "INSERT INTO test2 (status, collect) VALUES(1, 1)";
$result = mysql_query($sql);
}
}
// if successful redirect to delete_multiple.php
if ($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=moving.php\">";
}
}
mysql_close();
?>
</form>
</body>
</html>
thanks:) from a beginner
where does $count come from?
Try using count($_POST['checkbox']) instead on your INSERT statement. Then you can iterate over the checkboxes using:
for ($c = 0; $c < count($_POST['checkbox']); $c++){
$checkbox = $_POST['checkbox'][$c];
...INSERT action...
}
In the sample code, you store the statement in a variable named $rigth, but you (try to) execute a statement stored in a variable named $right.
There are a couple things you can do to catch errors.
Try static code analysis; some tools can tell you if a variable is used only once (an indication it may be a typo).
Handle errors. Some functions return a special value if there's an error (False is a common one); for these functions, there is usually a related function that will return error information. Some functions will throw an exception; catch them where they can be appropriately taken care of. System error messages shouldn't be displayed to non-admin users so you don't disclose too much information.
Use an interactive debugger. For example, install the Xdebug extension on your development server (you do use a dev server, right?) and use an Xdebug compatible debugger.
the solution - not nice but it works - thanks for all the comments and help!!!
<?php include("db_connect.php");?>
<html>
<head>
</head>
<body>
<form method="post" action="test.php">
New:
<?php
$left='SELECT * FROM test1 ORDER BY name ASC';
$result1=mysql_query($left);
$count=mysql_num_rows($result1);
while($resulta = mysql_fetch_array($result1))
{
?>
<input name="checkbox_add[]" type="checkbox" id="checkbox_add[]" value="<? echo $resulta['id']; ?>"/> <? echo $resulta['name']; ?>
<br />
<?php
}
?>
</td> <td><input type="submit" id="add" name="add" value="Add" /><br /><input type="submit" id="delete" name="delete" value="Del" /></td><td>
<?php
$right='SELECT test2.id, test1.name FROM test2, test1 WHERE test1.id=test2.collect AND test2.status=1';
$result2=mysql_query($right);
while($resultb = mysql_fetch_array($result2))
{
?>
<input name="checkbox_del[]" type="checkbox" id="checkbox_del[]" value="<?php echo $resultb['id']; ?>"/>, <?php echo $resultb['id']; ?>, <? echo $resultb['name']; ?>
<br />
<?php
}
?>
</td></tr></table>
<?php
// Check if add button active, start this
if (isset($_POST['add'])) {
for ($c = 0; $c < count($_POST['checkbox_add']); $c++){
$checkbox_add = $_POST['checkbox_add'][$c];
$sql = "INSERT INTO test2 (status, collect) VALUES(1, ".$checkbox_add.")";
echo $sql;
$result = mysql_query($sql);
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
}
}
elseif (isset($_POST['delete'])) {
for ($c = 0; $c < count($_POST['checkbox_del']); $c++){
$checkbox_del = $_POST['checkbox_del'][$c];
echo date("Y-m-d");
$sql = "UPDATE test2 SET status='2', log='".date('Y-m-d')."' Where id=".$checkbox_del;
echo $sql;
$result = mysql_query($sql);
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
}
}
elseif (isset($_POST['new'])) {
$sql = "INSERT INTO test1 (status, name) VALUES(1, '".$_POST['newitem']."')";
echo $sql;
$result = mysql_query($sql);
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
}
mysql_close();
?>
</form>
</body>
</html>

Categories