How can i save a session in php so that when i click value in td tag it will save a session and redirect into another page?
this is my code:
<?
$sql = "SELECT * from members";
$result = mysql_query($sql);
while($row1 = mysql_fetch_array($result)){
echo '<tr><td><img src='. $row1['propict'] .' width="70" height="70" /></td><td name="frnd"><a href="fprofile.php">' . $row1['name'] . '</td>';
}
?>
what i want here is when i click name that shows from td i will be redirected to fprofile.php that contain the information base on the name i clicked.
please help me guys to do this i really need to know this information.
You have 2 ways of doing the same :-
1). You can follow this approach also, but the user name will be displayed in the url.
Like:-
http://googl.com/aba.php?name=dinesh
<?
$sql = "SELECT * from members";
$result = mysql_query($sql);
while($row1 = mysql_fetch_array($result)){
echo '<tr>
<td><img src='. $row1['propict'] .' width="70" height="70" /></td>
<td name="frnd" onClick="showUserInfo('$row1[name]')"><a href="fprofile.php">' . $row1['name'] . '</td>';
}
?>
Js function write just below of you code..
function showUserInfo(name) {
window.location= <YOUR BASE URL>+'user_info.php?name='+name;
}
2). The name can also be set in the SESSION by AJAX also. This is a bite long approach but the name will not be displayed in the url.
You need to follow the following steps.
<?
$sql = "SELECT * from members";
$result = mysql_query($sql);
while($row1 = mysql_fetch_array($result)){
echo '<tr>
<td><img src='. $row1['propict'] .' width="70" height="70" /></td>
<td name="frnd" onClick="showUserInfo('$row1[name]')"><a href="fprofile.php">' . $row1['name'] . '</td>';
}
?>
JS function:---
Write the name in session in set_name_in_session.php file, then redirect the control to user_info.php page. There read the user name in user_info.php page and fetch the user details..
function showUserInfo(user_name) {
$.ajax({
url: <BASE_URL_OF_YOUR_SITE>+'set_name_in_session.php', //SET THE SESSION IN THIS FILE
data: {name: user_name},
method:'post',
success:function(response) {
window.location = <BASE_URL_OF_YOUR_SITE>+"user_info.php";
}
});
}
<?
$sql = "SELECT * from members";
$result = mysql_query($sql);
while($row1 = mysql_fetch_array($result)){
echo "<tr><td><img src='. $row1['propict'] .' width='70' height='70' /> </td>";
echo "<td name='frnd'><a href='fprofile.php?name=". $rows['name']. "'>" .
$row1['name'] . '</td>';
}
?>
and in fprofile.php
$_SESSION['name']= $_GET['name'];
now the clicked name is in the variable of $_SESSION['name']
just tell me if there is a syntax error
Update the link so that it passes the name you want to store in the session.
<a href="fprofile.php?name=' . $row1['name'] . '">' .$row1['name'] . '</td>
In fprofile.php, detect if $_GET['name'] has been set, and store it in the session.
if (isset($_GET['name']) && !empty($_GET['name'])) {
$_SESSION['name'] = $_GET['name'];
}
This assumes you are auto starting your session.
You can use $_REQUEST instead of $_GET to handle POST and GET requests similarly.
Related
here is my code. actually i am displaying some data from mysql on the page and creating dynamic link.i want started a session with session_start() in the very begining of code before starting any code. i want to store the value of the link that is to be display on other pagepage..
page1.php
<a style="color:#F00; font-family:Arial, Helvetica, sans-serif; margin-left:33px; font-weight:bold">
No. of registered students:
</a>
<table border='1' align="center" style="font-size:14px" width="95%" cellspacing="3" class="db_table">
<tr class="db_table_tr" >
<th class="db_table_th" name="submit">USN</th>
</tr>
<?php
include('includes/login_connection.php');
$query = "select p.usn, p.name from personal_details p, course_codes c where p.usn = c.usn order by p.usn";
$run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$num = mysql_numrows($run);
echo $num;
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['usn'] . "" . "</td>";
echo "<td>" . $row['name'] . " </td>";
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
}
echo "</tr>";
mysql_close($bd);
?>
</table>
page2.php
<?php
session_start();
if(isset($_SESSION['session_usn']))
{
$_POST['usn'] = $_SESSION['session_usn'];
echo $_POST['usn'];
}
?>
You need to provide a fall-back, in case the URL provided does not contain the proper variables in the $_GET section.
You have:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
You should do something else if $_GET['submit'] isn't set:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
} else {
$_SESSION['session_usn'] = "unset";
// or set a warning flag like "unset"
}
You should be feeding your php file a url like:
http://yoururl.com/page1.php?usn='333'
Where 333 is the value you want to store.
I'm having problems with showing data from a selected row into another div.
Meaning I'm showing eg forename and lastname, when the user clicks on the name (as a link), I would like to show more data from the Id chosen, and I would like it to be shown in a div box below.
See the site here: http://kristoff.it/onlinecoaching/
And my code:
<div class="greenBox1">
<h1>1 - VÆLG DIN ONLINE COACH</h1>
<div class="whiteBox1">
<?php
$sql = "SELECT * FROM coach ";
$coachId = $row["coachId"];
$fornavn = $row["fornavn"];
$efternavn = $row["efternavn"];
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
{
echo '<table border="0" align="left" height="100">';
echo '<tr>';
echo '<td width="95" rowspan="2" align="center" valign="middle"><img src="' . $row['imgUrl'] . '" width="85" height="85" alt="' . $row['imgAlt'] . '"/>' . '</td>';
?>
<!-- here I'm trying to write the id of the selected name to the div box below -->
<?
echo '<td><h2>' . $row['fornavn'] . $row['efternavn'] . '</h2></td>';
echo '</tr>';
echo '<tr>';
echo '<td valign="top" width="190"><p>' . $row['beskrivKort'] . '<br></p></td>';
echo '</tr>';
echo '</table>';
}
?>
</div>
</div>
<div class="greenBox2">
<h1>2 - BOOK TID I COACHENS KALENDER</h1>
<div class="whiteBox2" id="whiteBox2">
<!-- here would like the more data to show -->
</div>
</div>
Regards Maria
In your website, your URLs are empty. You need to put your ID.
echo '<td><h2>' . $row['fornavn'] . $row['efternavn'] . '</h2></td>';
With that URL, you will send the ID as a GET variable.
You must then request your data if the ID is set.
<div class="whiteBox2" id="whiteBox2">
<?php
if (isset($_GET['id']))
{
$sql = "SELECT * FROM coach WHERE coachId=" . (int)$_GET['id'];
// Do your stuff...
echo "Hello";
}
?>
<!-- here would like the more data to show -->
</div>
I have put together a basic order list for admin users in php for checking order contents placed by logged in users.
The aim of this script is to retrieve the order details (item, quantity, price) as well as the user’s first name and surname (where ‘Order for:’ is).
The script below does everything ok in that it retrieves the order (and orders if there are more than one) and it’s/their item, quantity and price.
However, it doesn’t display the user’s name and surname.
I know the problem is that where I am trying to display the name is outside the while loop but Im a little stuck in where it should sit. Any suggestions? Code is below:
<?php
$page_title = 'View Individual Order';
include ('includes/header.html');
// Check for a valid user ID, through GET or POST.
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
{ // Accessed through view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
{ // Form has been submitted.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
include ('./includes/header.html');
exit();
}
?>
<h1>Order Details</h1>
<?php
require_once ('database.php'); // Connect to the db.
// Retrieve the user's, order and product information.
$query = "SELECT us.users_id, us.users_sales_id, us.users_first_name, us.users_surname, us.users_dealer_name,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price,
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
$result = mysql_query ($query) or die(mysql_error());
if (mysql_num_rows($result)) { // Valid user ID, show the form.
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
while($row = mysql_fetch_array($result, MYSQL_NUM)) { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
}// end of WHILE loop
echo '</table>
<p> Here:</p>
<br><br>
<p> << Back to Orders</p>
<p> </p>
<p> </p>
<p> </p>
';
} else { // Not a valid user ID.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close(); // Close the database connection.
?>
<p>Footer here</p>
<?php
include ('./includes/footer_admin_user.html'); // Include the HTML footer.
?>
One way you could do it is grab the row first, and then use a do/while loop instead of just a basic while loop. Like this:
if (mysql_num_rows($result)) { // Valid user ID, show the form.
/*********** I added this line ***********/
$row = mysql_fetch_array($result, MYSQL_NUM);
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
/*********** I changed this from a while loop to a do-while loop ***********/
do { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
} while($row = mysql_fetch_array($result, MYSQL_NUM)); // end of WHILE loop
It looks like the problem is when you're trying to display the user's name:
echo '<p>Order for:<strong>'.$row[2].' '.$row[3]
The $row variable doesn't exist yet. Not seeing your database or the result from your database query, my guess is that the user's name is repeated next to every single item in their order, so it might be as simple as just starting the WHILE loop, and checking to see if you've printed their name yet:
$lastUser = NULL;
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
if ($row[0] !== $lastUser) {
if (isset($lastUser)) {
// finish up the report for the previous user
}
// echo the stuff for the current user's name
$lastUser = $row[0];
}
// go on echo-ing their order information
}
// after the while loop is over,
// close up the last user's report
But like I said, this is just a guess, and might be totally off.
The problem is that you tried to access $row[2] and $row[3] before mysql_fetch_array(). Since you are already echo'ing HTML tags, why don't you "buffer" your output first like this?:
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
$order = '<tr bgcolor="' . $bg . '">
<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
$orders[$row[2] . " " . $row[3]][] .= $order;
}
Then do a second foreach loop for the $orders
foreach($orders as $name => $orderList)
{
echo "Order for: $name";
echo "<table ...>";
foreach($orderList as $order)
{
echo $order;
}
echo "</table>";
}
I seem to be having a problem inserting a <div> element in the correct area. I want to push a button, in this case "more info" and display more information under the first div area using jquery.
example:
<div id="Main-info">
<div id="more-info">
</div>
</div>
The problem I'm having is the "Main-info" area is calling a php script and grabbing info from the database and displaying it in a table. The second php file I have loads in the "more-info" div so when I push the more info button the second php files goes under the entire first div but i want it to go under the specific information that I click.
A better example of what I am trying to accomplish is like the wefollow.com site, where you push the more info button and more info display right under the info you click. In my case the additional info goes under the entire table at the buttom.
Here is the code:
index.php:
<?php
include("buy.functions.php");
?>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="listing.js"></script>
<div id="article-area">
<h1>Welcome</h1>
<div id="output-listings">
<div id="more-information">
</div>
<?php outputListingsTable(); ?>
</div><!--end output-listings-->
buy.functions.php:
<?php
function outputListingsTable()
{
$mysql = new mysqli('localhost', 'root', 'root', 'ajax_demo') or die('you\'re dead');
$result = $mysql->query("SELECT * FROM explore") or die($mysql->error);
if($result)
{
echo "<table> \n";
while($row = $result->fetch_object())
{
$id = $row->id;
$siteName = $row->site_name;
$siteDescription = $row->site_description;
$siteURL = $row->site_url;
$sitePrice = $row->site_price;
echo "<div id=\"" . $id . "\"> \n";
echo " <tr> \n";
echo " <td>" . $siteName . "</td> \n";
echo " <td>" . $siteURL . "</td> \n";
echo " <td><a id=\"" . $id . "\" class=\"more-info\" href=\"#\">More info</a></td> \n";
echo " </tr> \n";
echo "</div> \n";
}
echo "</table> \n";
}
}
?>
getinfo.php:
<?php
function outputDescriptionTable($id)
{
$mysql = new mysqli('localhost', 'root', 'root', 'ajax_demo') or die('you\'re dead');
$result = $mysql->query("SELECT * FROM explore WHERE id=" . $id) or die($mysql->error);
if($result)
{
echo "<table> \n";
while($row = $result->fetch_object())
{
$siteName = $row->site_name;
$siteDescription = $row->site_description;
$siteURL = $row->site_url;
$sitePrice = $row->site_price;
echo "<div id=\"more-information\"> \n";
echo " <tr> \n";
echo " <td>" . $siteDescription . "</td> \n";
echo " <td>" . $sitePrice . "</td> \n";
echo " </tr> \n";
echo "</div> \n";
}
echo "</table> \n";
}
}
?>
<?php $id = $_GET['id']; ?>
<?php outputDescriptionTable("$id"); ?>
listing.js:
$(function(){
$("tr").hover(function(){
$(this).addClass("hover");
}, function() {
$(this).removeClass("hover");
});
$('.more-info').click(function() {
$('#more-information').show().load('getinfo.php?id=' + $(this).attr('id'));
return false;
});
});
jQuery's closest should solve your problem. Something like:
$('.more-info').click(function(e) {
$(e.target).closest('.more-information').show().load('getinfo.php?id=' + $(this).attr('id'));
return false;
});
From the doc:
Closest works by first looking at the
current element to see if it matches
the specified expression, if so it
just returns the element itself. If it
doesn't match then it will continue to
traverse up the document, parent by
parent, until an element is found that
matches the specified expression. If
no matching element is found then none
will be returned.
Also, your 'more-information' divs should have the class more-information, as opposed to the ID, as IDs should be unique, else strange things might happen.
I am new to php.
I made a member registration on login page and adm too. So inside admin I wanted to get the list of the members and delete the members I dont want. So I took the a code from a sample code for phone book from http://localhost/xamp and editted it to my requirement I am able to retrieve the members but unable to delete the members. See the code below:
<?php
require_once('auth.php');
require_once('../config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
?>
<html>
<head>
<meta name="author" content="Kai Oswald Seidler">
<link href="../loginmodule.css" rel="stylesheet" type="text/css">
<title></title>
</head>
<body>
<p>
<h2><?php echo "User list"; ?></h2>
<table border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#f87820">
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib1']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib2']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib3']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="50" height="6"><br><b><?php echo $TEXT['phonebook-attrib4']; ?></b></td>
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
</tr>
<?php
$firstname=$_REQUEST['firstname'];
$lastname=$_REQUEST['lastname'];
$phone=$_REQUEST['phone'];
if($_REQUEST['action']=="del")
{
$result=mysql_query("DELETE FROM members WHERE member_id={$_REQUEST['member_id']}");
}
$result=mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY lastname");
$i = 0;
while($row = mysql_fetch_array($result)) {
if ($i > 0) {
echo "<tr valign='bottom'>";
echo "<td bgcolor='#ffffff' height='1' style='background-image:url(img/strichel.gif)' colspan='6'></td>";
echo "</tr>";
}
echo "<tr valign='middle'>";
echo "<td class='tabval'><img src='img/blank.gif' alt='' width='10' height='20'></td>";
echo "<td class='tabval'><b>".$row['lastname']."</b></td>";
echo "<td class='tabval'>".$row['firstname']." </td>";
echo "<td class='tabval'>".$row['member_id']." </td>";
echo "<td class='tabval'><a onclick=\"return confirm('".$TEXT['userlist-sure']."');\" href='userlist.php?action=del&member_1d=".$row['member_id']."'><span class='red'>[".$TEXT['userlist-button1']."]</span></a></td>";
echo "<td class='tabval'></td>";
echo "</tr>";
$i++;
}
echo "<tr valign='bottom'>";
echo "<td bgcolor='#fb7922' colspan='6'><img src='img/blank.gif' alt='' width='1' height='8'></td>";
echo "</tr>";
?>
</table>
</body>
</html>
I haven't editted it that properly and the looks in all.
Please help me in making it able to delete the members also.
I didn't understand what .$TEXT['userlist-button1'].,'".$TEXT['userlist-sure']. variables are?
I also want to include an approved and disapproved radio button in table for each members.
How can I do that?
Please if you can help me.
This should be a POST via a FORM not a href link (GET).
$TEXT is obviously an array holding the text you want printed.
You need to replace &member_1d in the href with a real & and a real i as &member_id.
$TEXT is an array contaning all the language strings for the selected language.
You find the strings defined unter /lang/yourlanguage.php
In general this is not a very good example to start coding with IMO.
But I think your app may start working, if you make sure, the language files and other include files are available and you change this &member_1d with &member_id
An example of a list of members with delete links:
$query = mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY lastname");
if(mysql_num_row($query)!= 0){ //only continue if there are members in the database
while($row = mysql_fetch_assoc($query)){ //loop through each row in the database
$member_id = $row['member_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
echo '<p>' . $firstname . ' - ' delete '</p>';
}
}
A simple script on delete_member.php to delete the member from the database.
if(isset($_GET['id'])){
$member_id = $_GET['id'];
$query = mysql_query("DELETE FROM members WHERE member_id='$member_id'");
echo '<p>This user was deleted from database</p>';
}
This code is only basic to give an example.
I would however prefer to use a simple form and $_POST for something like this instead of using $_GET which is very vulnerable in this kind of instance.
After getting the list of members use a form with input field to type the id you want to delete.