-1

I'm trying to study php and I'm already on the sessions part where I want to input something on my first page that would then be an output for the second page

1stpage.php

<?php session_start();?>
    <form method="post"> 
    Enter a number: <input type="number" name="num1" min="1" max="20"  value="<?php echo $_SESSION["$num1"];?>">
        <a href ="2ndpage.php"> 
             <input type="button" name="select" value="select">
        </a>
</form>

2ndpage.php

<?php
    session_start();
    echo $_SESSION[$num1];
?>

Well, it does'nt work and I'm getting lots of undefined index error. Any fix guys? Thanks in advance.

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
  • 4
    Where you are assigning the value for `$_SESSION["$num1"];` – Jenz Sep 25 '15 at 12:26
  • what is this ? input text value contains a tag – Happy Coding Sep 25 '15 at 12:26
  • 1
    What are you trying to do here: `value=" php echo $_SESSION["$num1"];?>`? I believe that you think you're passing `input` value to a session var, but in fact you are assiging `input` value with `$_SESSION["$num1"]` value, which doesn't even exists. If you want to pass values to a PHP script to process, you need to choose a method like POST or GET. You can make it through HTML with `form method` or with an AJAX function using JS, but either way you have to do it. – al'ein Sep 25 '15 at 12:27
  • $_SESSION['num1'] = $num1; – John Sep 25 '15 at 12:30
  • this is wrong way to set session, `` tag itself tell the meaning, you can not set any variable and session using input tag – Prafulla Sep 25 '15 at 12:33
  • Ok so now I understand that I should name my input first before putting that variable on a session. Thanks for the help guys. much appreciated. – user3396839 Sep 25 '15 at 12:44
  • 1
    You're getting an undefined index notice, aren't you? You're not? Here http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Sep 25 '15 at 12:49
  • 1
    Then this stands to fail `` which isn't supported by older browsers. – Funk Forty Niner Sep 25 '15 at 12:50
  • @user3396839 have you got it resolved? – DirtyBit Sep 25 '15 at 12:57

5 Answers5

0

Take a look at form handling:

http://www.w3schools.com/php/php_forms.asp

If you still want to save the value into a session use:

$_SESSION['num1'] = $_POST['num1'];
Tim van Uum
  • 1,873
  • 1
  • 17
  • 36
0

I don't think you understand how all this works. You're having PHP output a session variable into a text box and expecting that that text box will somehow magically update your session. Somewhere you have to pass that data back to PHP and update it. So let's make your code into a process

<?php session_start(); ?>
<form method="post" action="process.php"> 
    Enter a number: <input type="number" name="num1" min="1" max="20"  value="<?php echo $_SESSION["$num1"];?>">
<input type="submit" name="select" value="select">
</form>

What I've done is make this form do a basic POST. If you want to avoid this you can use AJAX instead (same result using JS instead of HTML)

Next, let's make a basic process.php intermediate page

<?php
session_start();
$_SESSION['num1'] = (int)$_POST['num1'];
header('Location: 2ndpage.php');

Now we have a logistics chain for the data. You accept the data in your HTML and POST it to this page. It then takes it, sanitizes it (note I cast it to an (int) so we are certain this is a number) and then issues a redirect to the browser which takes you to your original landing page, which should now work.

This is all oversimplified for the process of teaching. You could have your form submit directly to 2ndpage.php and process there. Or, as I said before, you could use AJAX to send to process.php and when it returns some success parameter you then use JS to redirect. You have many options here.

Machavity
  • 30,841
  • 27
  • 92
  • 100
0

1stpage.php

<?php session_start();?>
<?php if(isset($_SESSION['num1'])) $num1 = $_SESSION['num1']; else $num1 = ''; ?>

    <form method="post" action="2ndpage.php"> 
    Enter a number: <input type="number" name="num1" min="1" max="20"  value="<?php echo $num1;?>">
    <input type="submit" name="select" value="select">
    </form>

2ndpage.php

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

What I have done here is I have first looked into the session if the num1 offset has been set into the session. If its not then I give $num as blank else I set the assign the value from session to $num. Now when we input something and post it on 2nd page, the posted value is assigned to the variable in session and is displayed as well. So that next time you visit 1stpage.php while in same session, you will see your last posted value.

Let us know if it solves your practive problem or if this is not what you wanted.

Ashish Choudhary
  • 2,004
  • 1
  • 18
  • 26
  • I'm actually doing a more complicated program so I'm trying to learn how session works because I need the value of my variables to be retained for all my php pages. I need every help I can get. I'll try the every codes until it works. Thank you very much for your concern. – user3396839 Sep 25 '15 at 14:21
0

I believe this is what you're trying to do:

Page 1:

<?php 
session_start();
if(isset($_POST['select'])){
    $num1 = $_POST['num1'];
    $_SESSION['num1'] = $num1;
}
?>
<form method="post"> 
    Enter a number: <input type="number" name="num1" min="1" max="20"  value="<?php echo $_SESSION['num1']; ?>">
<input type="submit" name="select" value="select">
<a href ="2ndpage.php">Click here to See the Value</a>
</form>

Page 2:

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

Where, at first you press the select to enter the value inside num1 then click on Click here to See the Value to see it.

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
-1

Put this on the first page:

$_SESSION['num1'] = $num1;

and this on the second

$num1 = $_SESSION['num1'];
Jack Albright
  • 509
  • 3
  • 11