Click here to Skip to main content
12,893,131 members (62,220 online)
Rate this:
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:
$username = "root";
$password = "";
$database = "banners";
@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" /> 
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:[^]

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

Solution 1

Use a session variable, see "PHP Sessions" at[^].
ProgramFOX 30-Sep-12 9:24am
Steven Borges 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?
"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.
Sergey Alexandrovich Kryukov 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?
Steven Borges 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 30-Sep-12 10:25am
Please also see my solution and Solution 2 with my comments.
Sergey Alexandrovich Kryukov 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.

Rate this: bad
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.


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:[^].

About Ajax:[^],[^] (a list of Ajax frameworks for PHP).

CPallini 30-Sep-12 11:50am
My 5.
Sergey Alexandrovich Kryukov 30-Sep-12 11:53am
Thank you, Carlo.
Rate this: bad
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'/>

2) Using a hand-constructed URL (& redirectng via header in this instance)
    $url = '002.php';
    $url .= '?firstName=Code';
    $url .= '&lastName=Project';
    $url .= '&purpose=answers';
    header('Location: '.$url);

	$firstName = $_GET['firstName'];
	$lastName = $_GET['lastName'];
	$purpose = $_GET['purpose'];
	echo $firstName . $lastName . " - purpose: " . $purpose;
Sergey Alexandrovich Kryukov 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:

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).

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2017
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