Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session variables sending specific variable from a while loop

Tags:

jquery

ajax

php

I have this simple while loop which retrieves data from a mysql query and displays several links on my homepage.

I would like to avoid using the php get function and add query strings to my urls

I am thinking of using session variables but I need help and I'm pretty sure this can't be done.

When a visitor clicks a link from the several ones displayed by the while loop, that particular variable would be set in a session.

In my code, the session will always send the last var.

Can this be done?

    <? session_start(); // Start Session Variables

    $result = mysql_query("my query");

    while($slice = mysql_fetch_assoc($result)){
        $url = $slice['url'];
        $name  = $slice['name']; ?>

        <a href="<? echo $url; ?>"><? echo $name; ?></a>

    <? } 

    $_SESSION['name'] = $name; // Store session data  ?>
like image 536
webmasters Avatar asked Dec 23 '11 11:12

webmasters


People also ask

Which variable is used to access the session variables?

Session variables are set with the PHP global variable: $_SESSION.

What is $_ session []?

PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values. Example: Store information.

How do you print the value of a session variable?

Use this: echo '<pre>'; var_dump($_SESSION); echo '</pre>'; Or you can use print_r if you don't care about types. If you use print_r, you can make the second argument TRUE so it will return instead of echo, useful for...


2 Answers

What you wish to do can be done by using a javascript function which will make an AJAX request sending the clicked name to the server. The server side code will then store the required session variable

 <? 
     session_start(); // Start Session Variables

    $result = mysql_query("my query");
    $name = '';
    while($slice = mysql_fetch_assoc($result)){
        $url = $slice['url'];
        $name  = $slice['name']; ?>

        <a href="<? echo $url; ?>" onclick='setSession(<? echo $url;?>);'><? echo $name; ?></a>

    <? } 

Now the setSession will make an AJAX call passing the value obtained. This can be then saved as session url by a simple server side code

The javascript code to present on the same page as your page having links

<script type='text/javascript'>
    function getXMLHTTPRequest() {
    try {
    req = new XMLHttpRequest();
    } catch(err1) {
      try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (err2) {
        try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (err3) {
          req = false;
        }
      }
    }
    return req;
    }



      var http = getXMLHTTPRequest();
       function setSession(value) {
    var myurl = "session.php";  // to be present in the same folder
     var myurl1 = myurl;
      myRand = parseInt(Math.random()*999999999999999);
      var modurl = myurl1+"?rand="+myRand+"url"+value ; // this will set the url to be sent

      http.open("GET", modurl, true);
      http.onreadystatechange = useHttpResponse;
      http.send(null);
    }


    function useHttpResponse() {
       if (http.readyState == 4) {
        if(http.status == 200) {
          var mytext = http.responseText;
          // I dont think u will like to do any thing with the response
          // u can redirect the user to the req page (link clicked), once the session url has been setted
            }
         }
         else {
             // don't do anything until any result is obtained
            }
        } 
</script>

PHP server side code to be present to set the required url as session value

<?php
session_start();
if($_SESSION['url']!=""){
unset($_SESSION['url']);   
$_SESSION['url'] = $_REQUEST['url'];  
}
else {
$_SESSION['url'] = $_REQUEST['url'];  
}
?>
like image 146
Prashant Singh Avatar answered Oct 18 '22 08:10

Prashant Singh


I don't think you can do what you are talking about in a simple fashion... The best way to pass the data is in the url, to be retrieved with $_GET.

You could use your while loop to echo individual forms, and set a hidden variable in each form to the value of your $name. Then you'd have to output a link that called a javascript function to submit that particular form. On the next page, you'd be able to grab the value for $name from the $_POST variable that was submitted by the hidden value in the form from the previous page...

But that would require javascript to function, and would just be an odd way to go about it.

something like:

echo '<form action="'.$url.'" method="post" name="myform1" id="myform1">';
echo '<input type="hidden" name="value" id="value" value ="'.$name.'">';
echo '</form>';
echo '<a onclick="document.forms['myform1'].submit();">'.$name.'</a>;

then on the next page ($url), you would get the value of the hidden variable using php, like so:

$name = $_POST['value'];

Again though, an odd way to go about it...

like image 43
rmmoul Avatar answered Oct 18 '22 08:10

rmmoul