0

I have a problem with my php site. I've made a form that when submitted loads a php file on the same page. Here's my code:

index.php:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<table cellspacing="0" cellpadding="10" width="320" border="0">
  <tr>
    <td>
      <button type="submit" id="btn" name="form_submit">Submit</button>   
    </td>
  </tr>
</table>
</form>

<?php 
if(isset($_POST['form_submit'])) {
    session_start();
    $_SESSION['tblno'] = "3";
}
?>

Now the problem is in my display.php file with the following codes:

<?php 
  session_start(); 
  $tblno = $_SESSION['tblno'];
  echo($tblno);
?>

I would expect it will output with the value of 3, which was initialized in the index.php. But what was displayed on my display.php file was 1. Any help would be much appreciated.

Angel M.
  • 2,692
  • 2
  • 32
  • 43
clyde one
  • 5
  • 1
  • 5

5 Answers5

0

try this code:

<?php 
if(isset($_POST['form_submit']))
    {
     session_start();
    $_SESSION['tblno']="3";
    }
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<table cellspacing="0" cellpadding="10" width="320" border="0">
  <tr><td>
  <button type="submit" id="btn" name="form_submit">Submit</button>   
  </td></tr>
</table>
</form>
0

It looks that you have just many php scripts without one cetral that manage the other scripts. So you have to start every script with

<?php 
session_start();
...

Or the session will not work properly.

tttpapi
  • 887
  • 2
  • 9
  • 32
0

To use cookie-based sessions, session_start() must be called before outputing anything to the browser. An other thing , you should not separate files , because you use $_SERVER['PHP_SELF'],all code must be in the same page , if you want to separate , change action to action='display.php' So your code must be like this if you still want use $_SERVER['PHP_SELF']:

<?php      session_start();?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<table cellspacing="0" cellpadding="10" width="320" border="0">
  <tr><td>
  <button type="submit" id="btn" name="form_submit">Submit</button>   
  </td></tr>
</table>
</form>

<?php 

if(isset($_POST['form_submit']))
    {
    $_SESSION['tblno']="3";
    }

$tblno=$_SESSION['tblno'];
echo($tblno);

?>
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
0

You need the following in index.php.

<?php 
if(isset($_POST['form_submit']))
{
  session_start();
  $_SESSION['tblno']="3";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<table cellspacing="0" cellpadding="10" width="320" border="0">
  <tr><td>
  <button type="submit" id="btn" name="form_submit">Submit</button>   
  </td></tr>
</table>
</form>

<iframe src=display.php>

And in display.php:

<?php
session_start();
$tblno=$_SESSION['tblno'];
echo($tblno);
?>

(Your posting to your own index.php which at the top sets the cookies.
After which all the other pages have, after session_start, the right cookies)

Just always make sure session_start(); is before any other output to the browser, like html.

Edit: added the iframe.

Rik
  • 1,982
  • 1
  • 17
  • 30
-2

I think you need the following:

<?php 
session_start();
if(isset($_POST['form_submit']))
{
    $_SESSION['tblno']="3";
} 
?>

Since wherever you use sessions, you must always use session_start

Phorce
  • 4,424
  • 13
  • 57
  • 107