PHP Delete Data with checkbox - php

So im kinda trying to delete row from my mysql database using checkboxes. Here is my code. Would be glad if someone could write me down simple delete code. Tryed many but failed, stuck for hours with this :(
<html>
<head>
<title>Admin, User</title>
</head>
<body>
<?php include 'connect.php';?>
<?php include 'functions.php';?>
<?php include 'title_bar.php';?>
<h3>Sukurti Nauja uzduoti: </h3>
<form method='post'>
<?php
if(isset($_POST['submit2']))
{
$pav = $_POST['pav'];
$uzduotis = $_POST['uzduotis'];
if(empty($pav) or empty($uzduotis)){
echo "<p>Privalomi visi langai!</p>";
}
else {
$sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
}
if($database->query($sql) == TRUE)
{
header('location:kurtisalinti.php');
}
else {
echo "<p>Klaida!</p>";
}
}
?>
<p>Uzduoties pavadinimas:
<p><input type='text' name='pav' />
<p>Uzduotis:
<p><textarea name='uzduotis'></textarea>
<p><input type='submit' name='submit2' value='Sukurti Uzduoti' />
</form>
</p></p></p></p>
</form>
<h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
<?php
$query = mysqli_query($database,"SELECT uzid,pav,uzduotis FROM uzduotys");
$count=mysqli_num_rows($query);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Pavadinimas</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Aprasymas</strong></td>
</tr>
<?php
while($rows = mysqli_fetch_array($query)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" value="<?php echo $row['uzid']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['uzid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['pav']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['uzduotis']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Istrinti Uzduotis"></td>
</tr>
<?php
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

Welcome to StackOverflow! Generally as a rule of thumb asking to write you some code doesn't get you very far, however I know that some people learn better from seeing it implemented and being able to modify off of that.
There is a number of ways you can do this. Using something like jquery and ajax you can post your requests without having to reload the page. Keep in mind what I have below may not fully work for you since taking the time to do this much already without a database or connection should give you an idea of what you're looking to do. Good luck!
CAUTION: PSUEDO CODE (This may not fully work and is not intended for production) The idea is to show you an approach to achieving what you are looking for.
<?php
include('connect.php');
include('functions.php');
include('title_bar.php');
if(isset($_POST['submit-uzduotis']))
{
$pav = $_POST['pav'];
$uzduotis = $_POST['uzduotis'];
if(empty($pav) or empty($uzduotis))
{
$result = "<span>Privalomi visi langai!</span>";
}
else
{
$sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
}
if($database->query($sql) == true)
{
header('location:kurtisalinti.php');
}
else
{
$result = "<span>Įvyko klaida!</span>";
}
}
if(isset($_POST['remove']))
{
$id = mysql_real_escape_string($_POST['remove']);
mysqli_query($database, "DELETE FROM uzduotys WHERE uzid = $id");
}
function showTableData()
{
$query = mysqli_query($database, "SELECT uzid, pav, uzduotis FROM uzduotys");
$count = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query))
{
echo '<tr id="'. $row['uzid'] .'">
<td>
<input type="checkbox" name="checkbox[]" value="'. $row['uzid'] .'">
</td>
<td>'. $row['uzid'] .'</td>
<td>'. $row['pav'] .'</td>
<td>'. $row['uzduotis'] .'</td>
<td>
<button id="remove-single" data-id="'. $row['uzid'] .'">REMOVE</button>
</td>
</tr>';
}
}
?>
<html>
<head>
<title>Admin, User</title>
<style>
.uzduotis-table {
background-color: #ccc;
border-collapse: collapse;
}
.uzduotis-table thead > td {
background-color: #333;
font-weight: bold;
}
.uzduotis-table td {
padding: 3px;
text-align: center;
background-color: #fff;
}
label {
display:block;
position:relative;
font-weight: bold;
}
</style>
</head>
<body>
<h3>Sukurti Nauja uzduoti: </h3>
<form method='post'>
<?php echo $result; ?>
<div>
<label>Uzduoties pavadinimas:</label>
<input type='text' name='pav' />
</div>
<div>
<label>Uzduoties:</label>
<textarea name='uzduotis'></textarea>
</div>
<div>
<input type='submit' name='submit-uzduoties' value='Sukurti Uzduoti' />
</div>
</form>
<h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
<table class="uzduotis-table">
<thead>
<tr>
<td>#</td>
<td>Id</td>
<td>Pavadinimas</td>
<td>Aprasymas</td>
</tr>
</thead>
<tbody>
<?php showTableData(); ?>
<tr>
<td colspan="5">
<button id="remove-selected" data-id="'. $row['uzid'] .'">REMOVE SELECTED</button>
</td>
</tr>
</tbody>
</table>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
// Remove Single (Button)
$('body').on('click', '#remove-single', function() {
remove($(this).data('id'));
});
// Remove All Checked
$('body').on('click', '#remove-selected', function() {
$('.uzduotis-table tr').filter(':has(:checkbox:checked)').each(function() {
remove(this.id);
});
});
function remove(id)
{
console.log("Remove: " + id);
alert('Removing: ' + id);
$.ajax({
type: "POST",
url: '<?php echo $url; ?>',
data: { remove: id },
success: function (data) {
$('#' + id).remove();
}
});
}
})
</script>
</body>
</html>
I cleaned up a bit of the html and styles, just made it easier for me to read. Also I would recommend looking into a library such as PDO to handle your database queries as there currently isn't much in place to protect or safe guard against sql injection.

Related

Inserting multiple rows with PHP

After importing a csv file, I'm trying to import the data with an insert statement, which is sending around 250 rows for one table.
The inserting, and code is working very well, but I had to print the insert query and the insert just worked till the row number 100.
PHP screen, which I import csv file data to the screen:
if(#$_FILES[file][type] == 'text/csv' || #$_FILES[file][type] == 'text/comma-separated-values' || #$_FILES[file][type] == 'application/vnd.ms-excel'){
if($_POST['FUNCAO'] == 'FORM_IMPORT')
{
error_reporting(E_ALL);
##### UPLOAD FILE
$uploaddir = 'gap_files/';
#$filename="gap_files/".$_FILES[file][name];
#$uploadfile = $filename;
$count = 0;
$id_linha = 0;
$itens_po = '';
$pos_to_import = '';
if (!#move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
print "<pre>";
print_r($_FILES);
print "$php_errormsg</pre>";
}
if(#$_FILES[file][name])
{
print '
<html>
<head>
<title>PO</title>
<link rel="stylesheet" type="text/css" href="../_includes/css/padrao.css" >
<script language="JavaScript" type="text/javascript" src="../_includes/js/padrao.js"></script>
<script language="JavaScript" type="text/javascript" src="../_includes/js/po.js"></script>
<script language="JavaScript" type="text/javascript" src="../_includes/js/label.js"></script>
</head>
<body>
<div id="div_report" style="background-color:#2B2B2B; position:fixed; top:0px; width:200%; height:200%; display:none; opacity:0.35; filter:alpha(opacity=35);"> </div>
<div id="div_link_color" style="display:none; position:fixed; margin-left: -300px; margin-top: -240px; left:50%; top:50%; width:600px; height:280px; background-color:#525252; text-align:center; z-index: 1000; text-align:center; border-radius: 0.4em; ">
<center>
<table cellpadding="3" cellspacing="0">
<tr>
<td colspan="3" class="REPORT_EDIT_TITLE" colspan="3" align="center" id="title_box_comment"></td>
</tr>
<tr>
<td colspan="3">
<iframe style="width:587px; height:230px; background-color:#EAF0FA; text-align:center; text-align:center; border-radius: 0.4em; overflow:auto; border:none;" id="frame_link_color" name="frame_link_color"></iframe>
</td>
</tr>
<tr>
<td colspan="3" align="center"><input type="button" name="comments" id="pre_production_comm" onclick="close_link_color();" value="Close" class="button" style="width:60px; height:30px;"></td>
</tr>
</table>
</center>
</div>
<div width="100%" id="FRAMESBOTTON">
<table cellpadding="1" cellspacing="0">
<tr>
<td class="botton-apple"><img src="../img/user.png" /> <b>Information</b> </td>
<td class="botton-apple" onclick="seleciona_div_po(3)"><img src="../img/help.png" /> <b>Help</b> </td>
<td class="botton-apple" onclick="seleciona_div_po(9)"><img src="../img/application.png" /> <b>Import Label</b> </td>
</tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="titlestyle" colspan="6" align="center"><b>Labels to import</b></td>
</tr>
</table>
</div>
';
$po = '';
$line=1;
$po_show = '';
$itens_po = '';
$factory_ids = '';
$wrong_price = '';
$import_error = 0;
$invoice_port = '';
$invoice_number = '';
$number_of_invoices = 0;
$invoice_gross_weight = '';
$invoice_total_cartons = '';
$total_pairs_invoice = 0;
$total_amount_invoice = 0;
$total_amount_discount_invoice = 0;
$tr_check_po = '';
$filename="gap_files/".#$_FILES[file][name];
$handle = fopen("$filename", "r");
$actual_po = '';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
foreach($data as $varname => $value)
{
if($value){
$data[$varname]=mysql_real_escape_string($value);
}
else{
$data[$varname]="NULL";
}
}
$check_if_inserted_invoice = mysql_query("select * from po_label where po_label.po = '".$data[3]."'");
if(mysql_num_rows($check_if_inserted_invoice) == 0){
if($line != 0){
$count++;
$itens_po .= '
<input type="hidden" name="iten_'.$count.'" value="'.$line.';'.$data[3].';'.$data[26].';'.$data[15].';'.$data[4].';'.$data[8].';'.$data[0].';'.$data[23].';'.$data[1].';"/>
<tr style="background-color:#ECEBEB;" onmouseover="ButtonRegSet(this,1)" onmouseout="ButtonRegSet(this)">
<td class="fontrecord" align="center"><input type="hidden" name="carton_number_'.$line.'" value="'.$line.'" readonly>'.$line.'</td>
<td class="fontrecord" align="center"><input type="hidden" name="po_'.$line.'" value="'.$data[3].'" readonly>'.$data[3].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="carton_'.$line.'" value="'.$data[26].'" readonly>'.$data[26].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="EANCode_'.$line.'" value="'.$data[15].'" readonly>'.$data[15].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="size_'.$line.'" value="'.$data[4].'" readonly>'.$data[4].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="pairs_'.$line.'" value="'.$data[8].'" readonly>'.$data[8].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="customer_style_'.$line.'" value="'.$data[0].'" readonly>'.$data[0].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="material_'.$line.'" value="'.$data[23].'" readonly>'.$data[23].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="colors_'.$line.'" value="'.$data[1].'" readonly>'.$data[1].'</td>
</tr>';
}
$line++;
}
}
fclose($handle);
}
$number_of_invoices++;
print '
<form method="post" action="../_registers/po_import_label.php">
<input type="hidden" maxlength="30" name="FUNCAO" value="insert_import_po" readonly>
<input type="hidden" maxlength="30" name="itens_form" value="'.$line.'" readonly>
<center>
</br>
<table class="round_sub_information_title_close" width="90%">
<tr>
<td colspan="8" class="round_sub_information_title_close" style="background-color:#FFFFFF;">
<div id="id_itens_invoices_'.$number_of_invoices.'" style="display:block;">
<center>
<table cellpadding="0" cellspacing="1" style="min-width:99%; width:99.8%;">
<tr class="line_grid">
<td class="fontrecord" align="center">Carton Number</td>
<td class="fontrecord" align="center">PO</td>
<td class="fontrecord" align="center">Carton Code</td>
<td class="fontrecord" align="center">Case Code</td>
<td class="fontrecord" align="center">Size</td>
<td class="fontrecord" align="center">Quantity</td>
<td class="fontrecord" align="center">Style</td>
<td class="fontrecord" align="center">Material</td>
<td class="fontrecord" align="center">Color</td>
</tr>
'.$itens_po.'
</table>
</center>
</div>
</td>
</tr>
<tr>';
print '
<tr><td class="round_sub_information_title_close" style="background-color:#FFFFFF;"><input type="submit" value="Save"/></td></tr>';
print '</tr>
</table>
</center>
</form>';
}
}
File: "../_registers/po_import_label.php"
<?php
session_start();
if(!$_SESSION["uid"]){
die(header("location: ../login.php"));
}
include('../_library/config.php');
include('../_library/opendb.php');
include("../_library/functions.php");
$s = "insert into po_label (carton,
carton_number,
po,
size,
pairs,
colors,
material,
customer_style,
EANCode) values";
for($i=1;$i<$_POST['itens_form'];$i++)
{
$s .="('".$_POST['carton_'.$i]."',
'".$_POST['carton_number_'.$i]."',
'".$_POST['po_'.$i]."',
'".$_POST['size_'.$i]."',
'".$_POST['pairs_'.$i]."',
'".$_POST['colors_'.$i]."',
'".$_POST['material_'.$i]."',
'".$_POST['customer_style_'.$i]."',
'".$_POST['EANCode_'.$i]."'),";
}
$s = rtrim($s,",");
print $s;
return mysql_query($s) ? 'INSERTED.' : 'ERROR: '.mysql_error();
include '../library/closedb.php';
?>
After import to the screen, I call the insert function to send the data to the database, it is when I got the error.
insert into po_label (carton, carton_number, po, size, pairs, colors, material, customer_style, EANCode) values (...),('10355000000098','98','0355CC0001','7',' 6','CAMEL','SOFT NUBUCK','CC-CRYSTALL','191644000270'),('10355000000099','99','0355CC0001','7',' 6','CAMEL','SOFT NUBUCK','CC-CRYSTALL','191644000270'),('10355000000100','100','0355CC0001','7',' 6','','SOFT NUBUCK','CC-CRYSTALL','191644000270'),('','','','','','','','',''),('','','','','','','','',''),('','','','','','','','',''),('','','','','','','','',''),(...)
Adding an observation: Before I updated the files in my official system, I tried and test it in the Wamp server, where everything works well.
If i have only 100 rows, the insert works well
If i have more than 100 rows, you can check in the bottom that after the row 100 the insert becomes empty
1) Please confirm whether all values coming without spaces by following function
print_r()
2) Use the trim() function to every single data passed in insert query
INSERT INTO TBL_NAME VALUES(
'".trim($value[0])."',
'".trim($value[1]))";
May it will help
I think you should improve your below code with given
// Your code
for($i=1;$i<$_POST['itens_form'];$i++)
{
$s .="('".$_POST['carton_'.$i]."',
'".$_POST['carton_number_'.$i]."',
'".$_POST['po_'.$i]."',
'".$_POST['size_'.$i]."',
'".$_POST['pairs_'.$i]."',
'".$_POST['colors_'.$i]."',
'".$_POST['material_'.$i]."',
'".$_POST['customer_style_'.$i]."',
'".$_POST['EANCode_'.$i]."'),";
}
for($i=1;$i<$_POST['itens_form'];$i++)
{
// Remove POST for columns in which you have allowed null in data table
if($_POST['carton_'.$i] != '' && $_POST['carton_number_'.$i] != '' && $_POST['po_'.$i] != '' && $_POST['size_'.$i] != '' && $_POST['pairs_'.$i] != '' && $_POST['colors_'.$i] != '' && $_POST['material_'.$i] != '' && $_POST['customer_style_'.$i] != '' && $_POST['EANCode_'.$i] != '')
{
$s .="('".$_POST['carton_'.$i]."',
'".$_POST['carton_number_'.$i]."',
'".$_POST['po_'.$i]."',
'".$_POST['size_'.$i]."',
'".$_POST['pairs_'.$i]."',
'".$_POST['colors_'.$i]."',
'".$_POST['material_'.$i]."',
'".$_POST['customer_style_'.$i]."',
'".$_POST['EANCode_'.$i]."'),";
}
}
UPDATE
As per the given comment, you should increase the length of URI. Please check below Increase your URI limit in your apache server.
but the best way to insert bulk data is mysqldump please see below answer for more information regarding mysqldump using PHP Answer

how to add jquery confirmation popup in codeigniter

im developing a project my own and i use codeigniter as my framework, i'm pretty begginer to php and codeignier. i created a confirm box in jquery ,but it's and html alert. so how to do that? don't laugh at me guys. thanks.
view
<?php
$_instance = get_instance();
$attributes = array('id' => 'main_form');
?>
<?php echo form_open('registration/reset',$attributes);?>
<style>
span{
color: red;
}
#main_tb{
margin-top:100px;
}
</style>
<table cellspacing='10' id>
</table>
<div id='main_form'>
<form id="main_form" name="form">
<table CELLSPACING=2 CELLSPACING=10 id='main_tb' >
<tr>
<td></td>
<td id="error"></td>
</tr>
<tr>
<td>User Name</td>
<td> <select class="cmb" style="width: 200px;" id="cmb_user" name="cmb_user"> </select></td>
</tr>
<tr>
<td></td>
<td><?php echo $this->session->flashdata('reset_error'); ?></td>
</tr>
<tr>
<td></td>
<td> <input type="submit" id="save" value="reset password"/></td>
</tr>
</table>
</form>
</div>
<?php echo form_close();?>
script
$j('#main_form').submit(function() {
// event.preventDefault();
var status = confirm("Are you sure? This cannot be undone");
if(status == false){
return false;
}
else{
return true;
}
});
controller
function reset(){
$password =$_POST['cmb_user'];
echo $username = $this->input->post('cmb_user');
$data_array = array(
'id_user' => $this->input->post('cmb_user'),
);
// // echo 'sdsd';
// echo $_POST['cmb_user'];
//
$this->load->model('registration/registration_model');
$this->registration_model->reset_pass($data_array);
$this->session->set_flashdata('reset_error', ' <br><span style="font-size: 10px;background-color: #FFFFFF;color:#ff0000;border:solid 1px #ff99cc;padding:2px;border-radius: 5px 5px 5px 5px">Reset Successfull</span>');
redirect('/registration/admin_reset');
}
this function works great. but i want to add jquery fancy popup like sweet alert. thank's again..
Add the following code into your view
<script src="https://cdn.rawgit.com/t4t5/sweetalert/master/dist/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/t4t5/sweetalert/master/dist/sweetalert.css">
<script language="javascript">
sweetAlert("Oops...", "Something went wrong!", "error");
</script>

Cannot fix the select/delete function. PHP/MySQL/JavaScript

guys I have the following script for adding, editing and deleting content in mysql, and showing it at index.php file. So here is my index.php file:
<script>function goDel()
{
var recslen = document.forms[0].length;
var checkboxes=""
for(i=1;i<recslen;i++)
{
if(document.forms[0].elements[i].checked==true)
checkboxes+= " " + document.forms[0].elements[i].name
}
if(checkboxes.length>0)
{
var con=confirm("Are you sure you want to delete");
if(con)
{
document.forms[0].action="delete.php?recsno="+checkboxes
document.forms[0].submit()
}
}
else
{
alert("No record is selected.")
}
}
function selectall()
{
// var formname=document.getElementById(formname);
var recslen = document.forms[0].length;
if(document.forms[0].topcheckbox.checked==true)
{
for(i=1;i<recslen;i++) {
document.forms[0].elements[i].checked=true;
}
}
else
{
for(i=1;i<recslen;i++)
document.forms[0].elements[i].checked=false;
}
}
</script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>
<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td><hr size="1" noshade></td>
</tr>
<tr>
<td>
<form action="" method="post" name="" id="">
<table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All </td>
<td colspan="3" align="center">Add New Branch </td>
</tr>
<tr>
<td><strong>Delete</strong></td>
<td><strong>Branch Name </strong></td>
<td><strong>Short Name </strong></td>
<td><strong>Update</strong></td>
</tr>
<?
include("conn.php");
$sql="select sn,branchname,shortname from $branch order by sn";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
?>
<tr>
<td><input name="<? echo $row['sn']; ?>" type="checkbox" class="check"></td>
<td><? echo $row['branchname']; ?></td>
<td><? echo $row['shortname']; ?></td>
<td>Update</td>
</tr>
<? } ?>
</table>
</form></td>
</tr>
</table>
Here is my delete.php file:
<?php
include("conn.php");
$recsno=$_GET["recsno"];
$data=trim($recsno);
$ex=explode(" ",$data);
$size=sizeof($ex);
for($i=0;$i<$size;$i++) {
$id=trim($ex[$i]);
$sql="delete from $branch where sn='$id'";
$result=mysql_query($sql,$connection) or die(mysql_error());
}
header("location: index.php");
?>
The problem is when I check 1 row, and click delete, it is deleting all of the rows, like I've clicked select all (which I didn't do). Thank you in advance.
It seems Delete query not getting value of $id, you have to store values in $id.
just GET id values
$id = $_REQUEST["sn"];
then your delete query
$sql="delete from $branch where sn='$id'";

Passing value inside JavaScript function and saving it into a PHP variable

I am creating an application like that of Facebook photo albums having comments.
I have a webpage, which shows the pics inside an album, when a pic is clicked, a lightbox opens up, and the caption and comments on the pic will be shown. Here's the pictures.php code
<?php
session_start();
ob_start;
include('connection.php');
?>
<html>
<head>
<title>Photos</title>
<style type="text/css">
.wraptocenter
{
width: 200px;
height: 150px;
overflow: hidden;
}
.wraptocenter img
{
vertical-align: top;
}
.black_overlay
{
display: none;
position: fixed;
top: 0%;
left: 0%;
width: 100.7%;
height: 100%;
background-color: black;
z-index: 1001;
-moz-opacity: 0.8;
opacity: .80;
filter: alpha(opacity=80);
}
.white_imagebox
{
display: none;
position: fixed;
top: 5%;
left: 6%;
width: 900px;
height: 500px;
padding: 0px;
border: 0px solid #a6c25c;
background-color: white;
z-index: 1002;
overflow: visible;
}
</style>
<script type="text/javascript" src="AJAX/AjaxInsertPicComment.js"></script>
<script>
function showpic(image_path,image_id,album_id,pic_caption)
{
document.getElementById('AlbumDiv').style.display = "block";
document.getElementById('fade').style.display = "block";
document.getElementById('image').src = image_path; // this line added
img = new Image();
img.src = image_path;
document.getElementById('t_albumid').value = album_id;
document.getElementById('t_imageid').value = image_id;
document.getElementById('t_albid').value = album_id;
document.getElementById('t_picid').value = image_id;
document.getElementById('albumid').value = album_id;
document.getElementById('imageid').value = image_id;
document.getElementById('t_imagepath').value = image_path;
document.getElementById('caption_holder').value = pic_caption;
if(img.width > 500 && img.height > 450)
{
if(img.width > img.height)
{
document.getElementById('image').style.width = "500px";
document.getElementById('image').style.height = 'auto';
delete img;
return false;
}
else
{
document.getElementById('image').style.height = "450px";
document.getElementById('image').style.width = 'auto';
delete img;
return false;
}
}
else if(img.height > 450 && img.width < 500)
{
document.getElementById('image').style.height = "450px";
delete img;
return false;
}
else if(img.height < 450 && img.width > 500)
{
if(img.width > img.height)
{
document.getElementById('image').style.width = "500px";
document.getElementById('image').style.height = 'auto';
delete img;
return false;
}
else
{
document.getElementById('image').style.height = "450px";
document.getElementById('image').style.width = 'auto';
delete img;
return false;
}
}
else if(img.width < 500 && img.height < 450)
{
if(img.width > img.height)
{
document.getElementById('image').style.width = "500px";
document.getElementById('image').style.height = 'auto';
delete img;
return false;
}
else
{
document.getElementById('image').style.height = "450px";
document.getElementById('image').style.width = 'auto';
delete img;
return false;
}
}
delete img;
alert("humm");
return false;
}
</script>
</head>
<body>
<div id="photo_holder">
<table width="1000px" cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td width="50px" align="center"/>
<td align="center" colspan="2" background="Images/header_menu.png" style="padding-right:2px;">
<?php include('header.php');?>
</td>
<td width="50px" align="center"/>
</tr>
<tr>
<td width="50px" align="center" ></td>
<td width="600px" align="center">
<?php
$album_id=$_REQUEST['txt_albumid'];
echo $album_id;
/* how many columns */
$column_number='3';
/* html table start */
?>
<div id="photo_container" align="center" width="600px">
<table border="1px" cellspacing="5" cellpadding="0" align="left">
<?php
$sql="SELECT * FROM candidate_pics WHERE album_id='$album_id'";
$result=mysql_query($sql,$con);
// $row=mysql_fetch_array($result);
$recordcounter=1; /* counts the records while they are in loop */
while($row=mysql_fetch_array($result))
{
/* decide if there will be new Table row (<TR>) or not ... left of division by column number is the key */
if($recordcounter%$column_number==1)
{
echo "<tr>";
}
?>
<td align="center" width="200px">
<div class="wraptocenter" align="center">
<?php $_SESSION['pic_id']=$row[pic_id];?>
<a href="javascript:void(0)"
onClick="showpic('<?php echo $row[pic_path];?>','<?php echo $row[pic_id];?>',
'<?php echo $row[album_id];?>','<?php echo $row[pic_caption];?>')";>
<img src="<?php echo $row[pic_path];?>"/></a>
</div>
</td>
<?php
/* decide if there will be end of table row */
if($recordcounter%$column_number==0)
{
echo "</tr>";
}
$recordcounter++; /* increment the counter */
}
if(($recordcounter%$column_number)!=1){ echo "</tr>"; }
?>
</table>
</div>
</td>
<td width="300px" align="center" >
<div id="photo_uploader">
<form method="post" action="photo_upload.php" enctype="multipart/form-data">
<table width="300px" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" width="100px">Upload Pic::</td>
<td align="center" width="200px">
<input type="file" name="file" id="file" style="width: 200px;"/>
</td>
</tr>
<tr>
<td align="center" width="100px"/>
<td align="center" width="200px">
<input type="submit" name="submit_pic" id="submit_pic" value="Click to upload" style="width: 200px;"/>
<input name="txt_albumid" type="hidden" value="<?php echo $album_id;?>"/>
</td>
</tr>
</table>
</form>
</div>
</td>
<td width="50px" align="center" ></td>
</tr>
</table>
</div>
<div id="AlbumDiv" class="white_imagebox">
<table align="center" cellpadding="0" cellspacing="0" border="0" width="900px">
<tr>
<td colspan="2" height="25px">
<div id="close">
<a href="javascript:void(0)"
onclick="document.getElementById('AlbumDiv').style.display =
'none';document.getElementById('fade').style.display='none'">
<img src="images/close-icon.png" style="border-style: none; border-color: inherit;
border-width: 0px; height: 17px; width: 16px;" align="right" /></a>
</div>
</td>
</tr>
<tr>
<td width="600px" align="center">
<table width="600px" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="50px" align="center"/>
<td align="center" width="500px">
<div id="image_holder" style="width: 500px; height: 450px;
background-color:#666666;">
<input type="image" id="image" name="image"/>
</div>
</td>
<td width="50px" align="center"/>
</tr>
<tr>
<td/>
<td>
<input type="text" id="caption_holder" name="caption_holder"
style="width:500px;"/>
</td>
<td/>
</tr>
</table>
</td>
<td width="300px" align="center">
<table width="300px" cellpadding="0" cellspacing="0" border="0" align="top">
<tr>
<td align="center" width="100px">
<form method="post" action="photo_delete.php">
<input type="submit" name="delete" id="delete" value="Delete"
class="button" style="width: 100px;"/>
<input type="hidden" name="t_imageid" id="t_imageid"/>
<input type="hidden" name="t_imagepath" id="t_imagepath"/>
<input type="hidden" name="t_albumid" id="t_albumid"/>
</form>
</td>
<td align="center" width="100px">
<form>
<input type="button" name="edit" id="edit" value="Edit" class="button"
style="width: 100px;"
onclick="document.getElementById('pic_caption').style.visibility = 'visible';
document.getElementById('enter_caption').style.visibility = 'visible';"/>
</form>
</td>
<td align="center" width="100px">
<form method="post" action="">
<input type="submit" name="cover_pic" id="cover_pic" value="Set CoverPic" class="button"
style="width: 100px;"/>
<input type="hidden" name="t_albumname" id="t_albumname"/>
<input type="hidden" name="t_imagename" id="t_imagename"/>
</form>
</td>
</tr>
<tr>
<td height="100px" colspan="3">
<form method="post" action="photo_edit.php">
<input type="text" name="pic_caption" id="pic_caption" style="visibility:hidden;"/>
<input type="submit" name="enter_caption" id="enter_caption" style="visibility:hidden;"/>
<input type="hidden" name="t_picid" id="t_picid"/>
<input type="hidden" name="t_albid" id="t_albid"/>
</form>
</td>
</tr>
<tr>
<td height="300px" colspan="3">
<div id="" style="overflow-y:scroll; height:300px;">
<table width="282px" cellpadding="0" cellspacing="0" border="0"
align="center">
<tr>
<td>
<?php
echo $_SESSION[pic_id];?>
<div id="message_report">
</div>
</td>
</tr>
<tr>
<td>
<form method="post" action=
"javascript:get(document.getElementById('pic_comment_form'));" name=
"pic_comment_form" id="pic_comment_form">
<table width="280px" cellpadding="0" cellspacing="0" border="0"
align="center" >
<tr>
<td width="100px">
<input type="text" name="comment" id="comment" style="width:100px"/>
</td>
<td width="80px"/>
<td width="100px">
<input type="submit" name="comment_enter" id="comment_enter" style="width:100px"/>
</td>
<tr>
<td>
<input type="hidden" name="albumid" id="albumid"/></td>
<td>
<input type="hidden" name="imageid" id="imageid"/></td>
<td>
<input type="hidden" name="user" id="user"
value="<?php echo $_SESSION[logged_user];?>"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div id="fade" class="black_overlay">
</div>
</body>
</html>
The code
<a href="javascript:void(0)"
onClick="showpic('<?php echo $row[pic_path];?>','<?php echo $row[pic_id];?>',
'<?php echo $row[album_id];?>','<?php echo $row[pic_caption];?>')";>
<img src="<?php echo $row[pic_path];?>"/></a>
pass the album_id, pic_id to the javascript functions which is then passed to the objects in the lightbox such as id="t_albumid", id="t_imageid"
I need to write a SQL query in the lightbox like this:
SELECT * FROM candidate_pics_comment WHERE pic_id='$VariableHavingPicId
but for this I must use the variable in the JavaScript image_id and pass it again to the lightbox div in the JavaScript. The code:
document.getElementById('t_imageid').value = image_id;
assigns the object, the value as image_id, but how can we assign the image_id to a php variable so that I can use it in the sql query?
I mean in the JavaScript can it be something like this:
function showpic(image_path,image_id,album_id,pic_caption)
{
<?php
$var = //assign the value image_id;?>
// rest of the code here;
}
I know it's not possible, but is there some trick to achieve this?
Moreover as I was experimenting, I did something like this:
function showpic(image_path,image_id,album_id,pic_caption)
{
<?php
$_SESSION[pic_id]="hmmm";?>
//rest codes;
}
Then when I used echo $_SESSION[pic_id] in php script, it displayed hmmm
So I was thinking can it be done something like this?
function showpic(image_path,image_id,album_id,pic_caption)
{
<?php
$_SESSION[pic_id]=//assign image_id to session variable;?>
//rest codes;
}
Besides I used AJAX application, but I saw that AJAX returns the response to the objects innerHTML, how ca it be stored in a php variable?
Update
My question is that, I am passing values from this
<a href="javascript:void(0)"
onClick="showpic('<?php echo $row[pic_path];?>','<?php echo $row[pic_id];?>',
'<?php echo $row[album_id];?>','<?php echo $row[pic_caption];?>')";>
<img src="<?php echo $row[pic_path];?>"/></a>
to the JavaScript fiunction
function showpic(image_path,image_id,album_id,pic_caption)
Now how can I assign image_id to a PHP variable which I can use in the div AlbumDiv which is used for the lightbox. If that not possible how can a session variable be assign the value of image_id, is it possible inside the JavaScript function?
ajax is the only option to get something from JavaScript to PHP.
If you are using jQuery then take a look at the docs here
Otherwise I recommend you use a Library like jQuery or jQuery itself

PHP Sessions Not working

I just can't get my variables to carry over to the next page. The variable assignment works fine on the initial page (tested), but the value goes null when going to the next. Plz Help!
Page 1:
<?PHP session_start(); ?>
<HTML>
<HEAD>
<META NAME='robots' CONTENT='noindex,nofollow'>
<TITLE>Master Chief's Gamestore</TITLE>
<STYLE type="text/css">
BODY {
color: #ffffff;
background: url('~/halo-reach.jpg');
background-repeat: no-repeat;
background-position: top;
background-color: black;
}
</STYLE>
</HEAD>
<body vLink='#3366CC' link='#3366CC' alink='#3366CC'>
<br/><br/><br/><br/><br/>
<table width='100%' height='80%'>
<tr>
<td align='center' valign='middle'>
<table width="50%" cellspacing="0" cellpadding="25" border="0">
<form method="post">
<tr bgcolor="#666633">
<th id="header" align="left" colspan="3">
<DIV align="center">
<font face="Verdana, Arial, Helvetica" color="white" size="3">
Master Chief's Gamestore </font>
</DIV>
</th>
</tr>
<tr bgcolor="#A3A385">
<td width="100%" valign="middle" colspan="2">
<font face="Verdana, Arial, Helvetica" size="2">
<div align="center"><br/><br/>
Username:&nbsp
<input type="text" name="username">&nbsp
Password:&nbsp
<input type="password" name="password"><br/>
<br/><input type="submit" name="act" value="Login"><br/>
<input type="submit" name="act" value="Register"><br/>
<input type="submit" name="act" value="Main"><br/> <br/><br/>
</div>
</font>
</td>
</tr>
<?PHP
if ($_POST['act'] == "Login") {
mysql_connect("~", "~", "~") or die("unable to connect to server");
mysql_select_db("~") or die("unable to select database");
$user = $_POST['username'];
$pass = $_POST['password'];
$query = "select * from users where user like '%" . $user . "%' and pass like '%" . $pass . "%';";
$result = mysql_query($query);
$rows = mysql_numrows($result);
if ($rows == 0 || strlen($user) == 0) {
echo "<br />Login Failure";
} else {
$_SESSION['user'] = mysql_result($result, 0, "user");
$_SESSION['id'] = mysql_result($result, 0, "P_Id");
header('Location: ~/success.php');
}
}
if ($_POST['act'] == "Register") {
header('Location: ~/register.php');
}
if ($_POST['act'] == "Main") {
header('Location: ~/index.php');
}
?>
<div align="center">
<tr bgcolor="#666633">
<td align="left">
Logged in as:
<?PHP
$user = $_SESSION['user'];
if (!$user) {
$user = 'Guest';
}
echo $user;
?>
</td>
<td align="right">
Password:&nbsp
<input type="password" name="srcpw">
<input type="submit" name="dspphp" value="Display PHP">
</td>
</tr>
</div>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
Page 2:
<?PHP session_start(); ?>
<HTML>
<HEAD>
<META NAME='robots' CONTENT='noindex,nofollow'>
<TITLE>Master Chief's Gamestore</TITLE>
<STYLE type="text/css">
BODY {
color: #ffffff;
background: url('~/halo-reach.jpg');
background-repeat: no-repeat;
background-position: top;
background-color: black;
}
</STYLE>
</HEAD>
<body vLink='#3366CC' link='#3366CC' alink='#3366CC'>
<br/><br/><br/><br/><br/>
<table width='100%' height='80%'>
<tr>
<td align='center' valign='middle'>
<table width="30%" cellspacing="0" cellpadding="25" border="0">
<form method="post">
<tr bgcolor="#666633">
<th id="header" align="left" colspan="3">
<DIV align="center">
<font face="Verdana, Arial, Helvetica" color="white" size="3">
Master Chief's Gamestore </font>
</DIV>
</th>
</tr>
<tr bgcolor="#A3A385">
<td width="100%" valign="middle" colspan="2">
<font face="Verdana, Arial, Helvetica" size="2">
<div align="center">
<br/><br/>Success<br/><br/>
<input type='submit' name='main' value='Main'>
<?PHP
if ($_POST['main'] == "Main") {
header('Location: http://~/index.php');
}
if ($_POST['act'] == "Display Code") {
if ($_POST['pw'] == "pass") {
highlight_file("success.php");
}
}
?>
</div>
</font>
</td>
</tr>
<div align="center">
<tr bgcolor="#666633">
<td align="left">
Logged in as:
<?PHP
$user = $_SESSION['user'];
if (!$user)
$user = 'Guest';
echo $user;
?>
</td>
<td align="right">
Password:&nbsp
<input type="password" name="pw">
<input type="submit" name="dspphp" value="Display PHP">
</td>
</tr>
</div>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
EDIT: I've tried this sample code and it does NOT work.
Page 1:
<?php
session_start();
if (isset($_GET['link'])) {
$_session['myvariable'] = 'Hello World';
header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php');
exit;
}
?>
Click Here
Page 2:
<?php
session_start();
print 'Here is page two, and my session variable: ';
print $_session['myvariable'];
exit;
?>
But as I posted below, I have used sessions in other pages successfully on the same server. Soo frustrating....
Thanks for all the posts btw!
Make sure cookies are enabled so the second page actually re-creates the session of the first page instead of creating a new, empty one. Check out http://www.php.net/manual/en/session.idpassing.php for details.
EDIT
Another possible cause for sessions not working as expected, especially if it only affects certain pages, is that you have accidental whitespace or other output before your first 'session_start() error
It looks like you are just redirecting the user to the second page without posting anything. You would want to set hidden variables and actually submit your form to the second page. Re-directing will lose all the post variables.
If you want to send variables to the second page with a redirect, you can use session variables or a GET variable passed in with the URL to the second page.
Try using
$_SESSION['myvariable'] = 'Hello World';
in upper case

Categories