0

I've seen that there is a question: PHP Pass variable to next page

but it doesn't work for me.

I would like to parse for instance $variable to another php file, and it has to be in an href atribute.

I have a "while" and when the user clicks in the link I have to store the current value in a variable to parse it to another file.

I've alredy tried with sessions variable, but in my case, I have a query with lots of rows, and I have a variable where I put data. So, for each row, the user sees a link. When he clicks, I have to parse the variable to the other php file.

Just in case, I leave here the code

 <div class="container">
  <!-- Obtengo datos -->
    <?php 
  $conexion=mysql_connect('localhost','root','root') or die('No hay conexión a la base de datos');
    $db=mysql_select_db('carpe',$conexion)or die('no existe la base de datos.');

    $id= $_SESSION['id'];



$consulta=mysql_query("select * from(select * from messages m where 1> (select count(*) from messages m1 where m.id<m1.id and m.idm=m1.idm))c
    where id_from='".$id."' or id_to='".$id."'");
    while($filas=mysql_fetch_array($consulta)){
        $idm=$filas['idm'];
        $id=$filas['id'];
        $id_to=$filas['id_to'];
        $id_from=$filas['id_from'];
        $status=$filas['status'];
        $text=$filas['text'];
        $time=$filas['time'];


    $consulta2=mysql_query("select * from users where id='".$id_from."'");
    while($filas2=mysql_fetch_array($consulta2)){
        $Nombre_enviador=$filas2['username'];
    }


    $consulta3=mysql_query("select * from users where id='".$id_to."'");
    while($filas3=mysql_fetch_array($consulta3)){
        $Nombre_receptor=$filas3['username'];
    }

    $username= $_SESSION['username'];   



    $_SESSION['idm'] = $idm;
    echo "mensaje id: ".$idm;   




    ?>
            <div class="maincontent">
            <h3><a href="#">Asunto: <?php echo $idm?></a></h3>
            <span class="postInfo">Enviado por: 
            <?php
            if($Nombre_enviador==$username){
                ?> <a href="#">Ti</a>
            <?php
            }else{
            ?><a href="#"><?php echo $Nombre_enviador ?></a>
            <?php } ?>
             A 
             <?php
             if($Nombre_receptor==$username){
                ?> <a href="#">Ti</a>
                <?php
            }else{
            ?><a href="#"><?php echo $Nombre_receptor ?></a>
            <?php } ?>               
              - <?php echo $time ?></span>
            <p><?php echo $text ?></p>

            <a class="more" href="conversacion.php?a=register">Responder</a>

            <?php
             if(isset($_GET['a']) /*you can validate the link here*/){
                $_SESSION['link']=$_GET['a'];
            }
            ?>



            <hr>
            <?php } ?>
            <hr>
        </div>

I hope you understand it, thank you a lot!

Community
  • 1
  • 1
  • I think what you're trying to say is that you need a GET request ;) http://php.net/get See my answer below for an example on case usage. – Josh Brody Jan 02 '13 at 20:22
  • 2
    "parse" != "pass", which word do you actually intend to use? – user229044 Jan 02 '13 at 20:22
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Kermit Jan 02 '13 at 20:25
  • I'm sorry I'm from Argentina, perhaps I used the wrong words. I would like to create a variable and give it to another php file (when the user clicks the link) I tried the session variable, but it finally has the last value: I mean, if the query has 10 values, the session variable will have the last value, and if the user of the 10 link, clicks in the 3rd link, the code will send always the same value. Do you understand? thanks – user1905120 Jan 02 '13 at 20:25
  • I have an idea, would you show us the HTML output you wants to have? – SaidbakR Jan 02 '13 at 20:30
  • I might totally be wrong. But reading the question, it sounds like you want to put some data in the request or session array without doing an actual request to the server? – Redlab Jan 02 '13 at 20:42

1 Answers1

0

I'm not 100% sure of what you're trying to do based off of your code and your question, but one piece that I did notice seems to be a good segway:

<a class="more" href="conversacion.php?a=register">Responder</a>

<?php
if(isset($_GET['a']) /*you can validate the link here*/){
    $_SESSION['link']=$_GET['a'];
}
?>

The code above creates a hyperlink that has a destination of conversacion.php. In addition, your link provides an additional piece of information, which in this case is a variable labeled a with a value of register. The PHP code that you have below that link attempts to check if you have provided a GET method variable, which based off of your link, this would be the variable a. Using $_GET will attempt to access any variables that have been passed to the current URL via the GET method, which in this case is just as simple as attaching a query string.

I am not expecting this answer to fully address your issue because to be frank, I'm not too sure what the problem is as the code you have provided should be able to handle your request (at least the technical aspect which would be applied appropriately). Hopefully, the above context should help.

Robert
  • 8,717
  • 2
  • 27
  • 34