4

I am new to PHP and trying to pass variable from one page to another page. Initially, I have a HTML page which contains the frames as below.

<!DOCTYPE html>
<html>

<frameset cols="70%,*">
  <frame src="index.php">
  <frame src="slider.php">
</frameset>
</html>

As seen above, I have 2 PHP pages, in which am trying to send some values from index.php file to my slider.php file. My index.php file is as below.

<?php
$names = file('demo.csv');
$page = $_GET['page'];
$pagedResults = new Paginated($names, 20, $page);
$handle = fopen('demo.csv', 'r');
  if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
    }
echo "<table border='3' bgcolor='#dceba9' style='float:center; margin:50'>";
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
while ( $row = $pagedResults->fetchPagedRow())
{
    echo "<tr><td>";
    $row1 = str_replace( ',', "</td><td>", $row );
    echo $row1;
    echo "</td></tr>";
}
fclose($handle);
echo "</table>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
<form method="get" action="slider.php">
    <input type="hidden" name="totalcolumns" value="3">
    <input type="submit">
</form>

This is my slider.php file.

<?php 
      $totalcolumns = $_GET['totalcolumns'];
      echo "My next value should get printed";
      echo $totalcolumns;
?>

  <input type="text" data-slider="true" data-slider-range="100,500" data-slider-step="100">
</html>

As seen above, I am trying to retrieve the value with the name "totalcolumns". However, I am not able to retrieve the value in my slider.php file. I also tried using SESSION as suggested in this link, but no luck. Can someone please let me know what am doing wrong?

Community
  • 1
  • 1
ram esh
  • 259
  • 1
  • 7
  • 19
  • One security-related aspect: you should escape the GET variable *totalcolumns* when outputting: `echo htmlspecialchars($totalcolumns);` – ComFreek Oct 01 '13 at 21:11
  • 1
    I guess your problem is that you do not know for sure which is called first – Gar Oct 01 '13 at 21:11
  • 1
    you might want to avoid the use of frames alltogether, as PHP + JavaScript is combo quite capable of replacing them in an accessible manner.. just btw :P – Zathrus Writer Oct 01 '13 at 21:11
  • @Gar, I think that might be the problem. Do you know how can I resolve that issue? Thanks! – ram esh Oct 01 '13 at 21:13
  • The main problem is that you have to refresh the second frameset after you submit the form of the first frameset, in order to take the value. – Hackerman Oct 01 '13 at 21:21
  • 1
    The main problem is frames. Period. The world has moved on. Your problem is easily solved with ajax and a proper callback. – nietonfir Oct 01 '13 at 21:52

4 Answers4

3

You should be able to use the $_SESSION. This is:

$_SESSION['totalcolumns'] = $columns --> your value here in the first script

your value will be stored in the $columns variable in the second 
 $columns = $_SESSION['totalcolumns'] 

You can also review the require or include functions. These functions make one file depend on another one, it is as if you directly paste one file on the other. It is not a good practice to pass variables with these functions. You should use Session

http://php.net/manual/en/function.require.php

Btw, don't use framesets

fos.alex
  • 5,317
  • 4
  • 16
  • 18
1

You should use sessions and you should not use html framesets or iframes, it is a bad practice. If you don't want to reload the whole page by any change, you should use javascript.

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • Yeah, I think I can use sessions. However, as @Gar pointed out, the system doesn't know which PHP file is called first. I am not sure how to resolve this problem as I am using HTML frames to create 2 separate frames for both these PHP files at the same time. – ram esh Oct 01 '13 at 21:17
  • As I said don't use frames, they are not capable of what you want. Use a single html page instead of that which contains the data both of your index.php and slider.php files. – inf3rno Oct 01 '13 at 21:18
1

You can use $_REQUEST instead of $_GET or just it as:

<?php
if(array_key_exists('totalcolumns', $_GET)) {
      $totalcolumns = $_GET['totalcolumns'];
      echo "My next value should get printed";
      echo $totalcolumns;
?>

may this help you

Dinesh
  • 4,066
  • 5
  • 21
  • 35
1

i'd id the frames first, removing the src for the second one

<!DOCTYPE html>
<html>

<frameset cols="70%,*">
  <frame src="index.php" id="f1">
  <frame src="" id="f2">
</frameset>
</html>

then change the index.php adding this piece of code at the end

<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=3";
</script>

or perhaps if you have the totalcolumns in your php

<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=<?php echo $totalcolumns;?>";
</script>
Gar
  • 852
  • 13
  • 20