Setting session var from form in recordset

Record set has 2 text fields in the form which is set in a full repeat recordset browse. So, we get a long list of every record in the database. However, I want to be able to click on a single record and make another page appear. I can do this if the display is set as a table without using a text field form – just the record variable and using a hyperlink. But, I want to use the text field. Wrapping the form only gets me the value of the last record displayed. Help would be appreciated.

Welcome to SitePoint.

You sir, are making no sense. :smiley:

Forget using terms, like variables and record-sets, and just tell us in plain English what you would like to do.

Anthony.

Sorry about that. Thought that I was being plain. Ok, using PHP, I have a whole list of names each displayed in a text field. A name browser. I want to be able to click on a name and pass the information to the next page – a detail page. I do not want to use a URL link to pass the information. I want to use $_SESSION[‘session_var’]. The problem is that it will only load the information from the last record from the browser. Here is the code at present:

<?php $_SESSION[‘session_var’] = $row_rsIDetails[‘callname’];?>
<form action=‘session-Test2.php’ method=‘POST’ onclick=“MM_goToURL(‘parent’,‘…/sh_files/session-Test2.php’);return document.MM_returnValue”>
<input type=‘hidden’ name=‘form_var’ value=‘testing’>
<input name=“sCall” type=“text” id=“sCall” value=“<?php echo $row_rsIDetails[‘callname’]; ?>” />
</form>

And yes, session_start is on. The current detail/child page receives the variable. Just the wrong one.

Thanks for your help.

Thanks.

Can you tell us why you don’t want to use the record’s primary key (id) in the URL and would like to use the $_SESSION ?

Actually it is the primary key that I want to use. The code was just the last one I was trying. I don’t want the key seen or able to be captured. I will actually alway be coming from a hidden field. But is so… necessary to pass to each new page. I need to get it from a click on the name field. Go look at one of my browsers at www.shilohgens.com/sh_files/frmIFListFrame.php.

Thanks again.

You could encrypt the primary key (pk) and store the encryption key in the session, once the session has been destroyed the link is useless.

Would that be a viable option?

No, although a good idea, I need to be able to get the info from the selected row of data. This same procedure is to be used in many different pages. I need to find a way for it to work. Is it possible to to do this?

I mean if it will work from a simple text line displayed from the row:

<a href=“tempDetail.php?recordID=<?php echo $row_rsIDetails[‘d1code’]; ?>”> <?php echo $row_rsIDetails[‘d1code’]; ?>  </a>

why won’t it work with a text field? (d1code is a pk) This code works fine.

Thanks again.

Maybe the content of the text field is being malformed/changed when passed via the url, could you pass a hashed version of this?

Something like…


view.php?id=<?php echo md5($record['text']); ?>

SELECT * FROM table WHERE MD5(text) = $var

Can you explain more about the reasoning for not using the record’s pk?

The URL works fine. It’s the text box I want to get the info from. I guess what I really need is to know exactly where I am in the database – what row/record am I clicking on.

Well, you would use the pk to determine what record to display; then use this on the submission page.


<?php
function records(){
  return array(
    array('id' => 1, 'text' => 'one'),
    array('id' => 2, 'text' => 'two'),
    array('id' => 3, 'text' => 'three'),
  );
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <?php foreach(records() as $record): ?>
      <div>
        <form action="view.php" method="post">
          <input type="text" value="<?php echo $record['text']; ?>" readonly="readonly" />
          <input type="hidden" name="id" value="<?php echo $record['id']; ?>" />
          <input type="submit" value="View" />
        </form>
      </div>
    <?php endforeach; ?>
  </body>
</html>

That’s basically what I am trying to do. Taking your code with a few adjustments for SESSIONS:

PHP:

<?php if (!session_id()) session_start(); ?> // added for sessions
<html>
<head>
<title>Demo</title>
</head>
<body>
<?php foreach(records() as $record): ?>
<div>
<form action=“view.php” method=“post”>
<input type=“text” value=“<?php echo $record[‘text’]; ?>” readonly=“readonly” />
<input type=“hidden” name=“id” value=“<?php echo $record[‘id’]; $_SESSION[sesDCode]=$record[‘id’]; ?>” /> //added to get code
<input type=“submit” value=“View” />
</form>
</div>
<?php endforeach; ?>
</body>
</html>


I want to capture the ID for #2. The “view.php” contains an array dump so that I can see what is being passed:

PHP:

<?php
echo “<pre>”;
print_r($_SESSION); // display what is stored
echo “</pre>”;

?>

Clicking on the “View” for #2, the display is:

Array
(
[sesDCode] => 3
)

See what the problem is? It will only capture the last one displayed not the one selected. I want the selection. Got any more ideas? I am totally dry on this. If it was any other coding C, VB, etc. no problem but this???

Just a thought, if you really want to use sessions to hide the id number, then why not create an array in the session of the id’s and use the array key as the id number listed in the form for each item?

2 things to consider: the database contains thousands of records and 2nd how would I get the “onclick” to know which one I clicked on. Do you have a code suggestion?

Your database may have thousands of records, but if this is a page you would be creating on a regular basis, you shouldn’t be pulling them all at once. I wouldn’t get them in groups larger than 50 at a time, so you should create a pagination system for it and just flush your session set each page.

For part two, create your session array at the same time that you would create your forms:


<?php
$idList = array();
 foreach(records() as $record): 
$idList[] = $record['id'];
$sessionRecordId = count( $idList ) - 1; //or just track what number you are on
?>
<div>
<form action="view.php" method="post">
<input type="text" value="<?php echo $record['text']; ?>" readonly="readonly" />
<input type="hidden" name="id" value="<?php echo $sessionRecordId; ?>" /> //added to get code
<input type="submit" value="View" />
</form>
</div>
<?php endforeach; 
$_SESSION['idList'] = $idList;
?>

That would establish your session array of session id numbers that reveal nothing about the unique record id that you want to keep secret and it would replace it with a fresh array on each page, that way, when you click an item, the session array would know how to identify it. Then, on your page where you need the record id itself:


$recordId = $_SESSION[$_POST['id']];
// do whatever you need to with your record id
// you can still do a dump of the session and see all the key: value pairs
echo "<pre>";
print_r($_SESSION); // display what is stored
echo "</pre>";

This would still work with thousands of records, in theory, but the page would have an incredible load time and bog down your server, regardless of your chosen method.

What’s going on here? Non of this makes any sense. Am I the only one? :stuck_out_tongue:

All of this is unnecessary and overly complicated, there is currently no reason at all to hide the pk from the user. The code I posed, passes the script which receives the post’d values the pk for that row - therefore indicating ‘which row was clicked on’.

If you use the session solution posted above, it will break once you use multiple tabs.

I am wondering the same thing - why is the pk such a secret (you realize that anyone could view the source of the page and see the pk anyway, yes?)

All of this is unnecessary and overly complicated, there is currently no reason at all to hide the pk from the user.

I don’t know why the poster is so worried about exposing the record id either. I am just trying to give him a solution that he could work with.

If you use the session solution posted above, it will break once you use multiple tabs.

That is true. Every load of a page will blank the session array and reload it with new values. However, if he wants a session solution that bad, then he may have to make some sacrifices along the way or make an adjustment to the array to include a page number as a key above the array.

Have you tried the standalone example I posted here: http://www.sitepoint.com/forums/php-application-design-147/setting-session-var-form-recordset-749590.html#post4854779 ?

If you perform a var_dump of $_POST in view.php, you should be pleasantly surprised. :slight_smile: