Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL PHP HTML5
I'm very new to PHP and web-design, and I'm having trouble sending the value of a variable to another php.

Here is the current code:
<?php
$username = "root";
$password = "";
$database = "banners";
						
						mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die ("Não foi possível estabelecer uma ligação a Base de Dados");
$data = mysql_query("SELECT * FROM banner") or die(mysql_error()); 
echo "<table border = 1>"; 
echo "<tr>"; 
echo "<th>Cliente</th>"; 
echo "<th>Status</th>"; 
echo "<th>Visualizar</th>"; 
echo "<th>Excluir</th>"; 
echo "</tr>"; 
 
while($info = mysql_fetch_array( $data )) 
{ 
echo "<tr>"; 
echo "<td value=".$info['cliente'].">".$info['cliente'] . "</td> "; 
if($info['activo']='SIM'){echo "<td>Activo</td>"; }else {echo "<td>Desactivo</td> ";}
echo '<td><a href="visualizar.php" target="_blank"> 
<img src="imagens/search.png" /> 
</a></td>'; 
echo "<td>".$info['ficheiro'] . "</td> "; 
} 
echo "</table>"; 			
?>

The code that is underlined+bolded is supposed to send me to another page, and display the data of the entire information of the table, of the entry the user clicked on.
My problem is letting the other page know that I'm asking it to show "certain" data, or better said, sending the variable form here to another .php file.

I give an image example:
http://i.imgur.com/Og7St.png[^]

I hope I didn't make it sound so complicated.
Thanks in advance !
Posted 30-Sep-12 3:45am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use a session variable, see "PHP Sessions" at w3schools.com[^].
  Permalink  
Comments
ProgramFOX at 30-Sep-12 9:24am
   
+5!
Steven Borges at 30-Sep-12 9:34am
   
And how do I implement the session into the code?
If I understood it, it's saving the data as an array, how will the other page know exactly what I'm searching?
Sergey Alexandrovich Kryukov at 30-Sep-12 10:03am
   
If you mean any array, nothing happens, but if you mean the special thing, $_SESSION, this is the associative container which is shared between HTTP requests/responses withing the session. The HTTP protocol is stateless, but this is the way to work around: a container to store the state of the application, per session. Are you getting the picture?
--SA
Steven Borges at 30-Sep-12 10:07am
   
Well yeah, I know what it does and what it is now, my problem exactly is how to implement this into the code that is underlined+bolded in my question.
 
I inserted the $_Session before the HTML, yeah... but what now?
How can I make a button, link, with the $_Session, so that the other page can recognize it and send an SQL Query to show the details?
 
After all, I'm printing a table, and then placing a link to redirect to another page to simply show the data of a certain row, like it shows on my image.
 
I'm having a huge problem to know implementing this.
Sergey Alexandrovich Kryukov at 30-Sep-12 19:22pm
   
What do you mean "what now"? If you can do
 
$_SESSION["Some index"] = somevalue;
 
in one page, you can always read it
 
someOtherValue = $_SESSION["Some index"];
 
later in some other page and expect that someOtherValue will get the same value.
 
--SA
Sergey Alexandrovich Kryukov at 30-Sep-12 9:57am
   
"Different PHP" means "different pages", and those means different isolated HTTP requests and responses. Basically, this is what session variables are designed for. My 5.
--SA
Sergey Alexandrovich Kryukov at 30-Sep-12 10:25am
   
Please also see my solution and Solution 2 with my comments.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Most likely, the Solution 1 by CPallini should work for you. But you should know the alternative. As I can see, you are using the database. Another way to overcome the stateless nature of HTTP protocol is to store the state information somewhere on the server side, and the database can be used for this. In contrast to the session variables, this state would be not per session, but global, which is sometimes needed (for example, this is how you can track total number of users currently using the application, or some other global statistics). If you need to distinguish session, you need to do it explicitly.
 
[EDIT]
 
Solution 2 describes a very special case, only applicable to forms, but essentially the similar thing could be done with Ajax. Please see my comment to Solution 2. This is not really a tool to share any data between arbitrary pages at all, but you need to know how it works anyway.
 
Please see the description of HTTP methods:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html[^].
 
About Ajax:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^],
http://en.wikipedia.org/wiki/List_of_Ajax_frameworks#PHP[^] (a list of Ajax frameworks for PHP).
 
—SA
  Permalink  
v3
Comments
CPallini at 30-Sep-12 11:50am
   
My 5.
Sergey Alexandrovich Kryukov at 30-Sep-12 11:53am
   
Thank you, Carlo.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You could also use GET variables too. (vars added to the URL)
 
Here's 2 ways of doing it:
 
1) Using a form
<form action='002.php' type='GET'>
    <input name='firstName' value='enter firstName'/>
    <input name='lastName' value='enter firstName'/>
    <input name='purpose' value='enter purpose'/>
    <input type='submit' value='submit'/>
</form>
 
2) Using a hand-constructed URL (& redirectng via header in this instance)
<?php
 
    $url = '002.php';
    $url .= '?firstName=Code';
    $url .= '&lastName=Project';
    $url .= '&purpose=answers';
 
    header('Location: '.$url);
?>
 

002.php
<?php
	$firstName = $_GET['firstName'];
	$lastName = $_GET['lastName'];
	$purpose = $_GET['purpose'];
	
	echo $firstName . $lastName . " - purpose: " . $purpose;
?>
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 30-Sep-12 10:19am
   
I don't see that OP uses the form. This approach is only a special case of different pages where the data is passed throw the same pair of HTTP request/response. Roughly speaking, this is what already happens with every HTTP request/response, but some of the HTTP "method" sections are empty. You should have explained all the methods, not just GET. Here they are:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
 
Session variables (Solution 1) is really universal way to be used; and I also mentioned another one for better completeness, please see my answer.
 
Nevertheless, as formally this is also a method of sharing data between pages, it should be mentioned and is good to know. (I voted 4 this time).
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 680
1 Maciej Los 300
2 Suvendu Shekhar Giri 229
3 Sascha Lefévre 229
4 Richard Deeming 195
0 Sergey Alexandrovich Kryukov 9,678
1 OriginalGriff 9,135
2 Peter Leow 5,082
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 2,606


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100