ram esh
ram esh

Reputation: 259

PHP - passing variable from one PHP file to another PHP file

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?

Upvotes: 4

Views: 42394

Answers (4)

Gar
Gar

Reputation: 862

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>

Upvotes: 1

Dinesh
Dinesh

Reputation: 4110

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

Upvotes: 1

fos.alex
fos.alex

Reputation: 5637

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

Upvotes: 3

inf3rno
inf3rno

Reputation: 26147

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.

Upvotes: 1

Related Questions