Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
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
<?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

Use a session variable, see "PHP Sessions" at w3schools.com[^].
 
Share this answer
 
Comments
Thomas Daniels 30-Sep-12 9:24am    
+5!
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?
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?
--SA
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 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
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
 
Share this answer
 
v3
Comments
CPallini 30-Sep-12 11:50am    
My 5.
Sergey Alexandrovich Kryukov 30-Sep-12 11:53am    
Thank you, Carlo.
--SA
You could also use GET variables too. (vars added to the URL)

Here's 2 ways of doing it:

1) Using a form
HTML
<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
<?php

    $url = '002.php';
    $url .= '?firstName=Code';
    $url .= '&lastName=Project';
    $url .= '&purpose=answers';

    header('Location: '.$url);
?>



002.php
PHP
<?php
	$firstName = $_GET['firstName'];
	$lastName = $_GET['lastName'];
	$purpose = $_GET['purpose'];
	
	echo $firstName . $lastName . " - purpose: " . $purpose;
?>
 
Share this answer
 
v2
Comments
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:
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900