0

There are some inputs, and there is a function. The function requires these inputs, and the inputs are user-given. But, the buttons that fire the function and the input submission form are two different buttons. So, when the user presses "submit" to store his variables, the variables are stored fine. But, when he presses the "calculate" button (which fires the function), php says "undefined index" because it reads the $_POST of that input again and again.

If I disable register_globals, it does not show 'undefined index' but these values are 0 again.

If I use another file just to store these values and then redirect back to the page where the function button is, there is a redirect loop, require_once does not work.

What is the way to store the inputs in such way that they can be used again and again in functions and whatsoever? No databases, I need a way to store them in variables.

edit: the form: <label for="asdf">enter value:</label> <input type="text" id="asdf" name="asdf" value="<?php echo $asdf;?>" />

storing the value: $asdf=$_POST['asdf'];

then I need to write $asdf in the function with the updated value that the user gave through the html form. How to do it? Cannot be much simpler

10
  • 1
    Please show some code and what you have tried. Commented Jun 24, 2015 at 23:29
  • you could store them as session variables Commented Jun 24, 2015 at 23:29
  • 4
    No, @gamwtoshiston, please show what you have tried so far. We would like you to put effort into researching your questions because this question is too broad as written. Databases, cookies, sessions, globals, static files.... you get the point. Commented Jun 24, 2015 at 23:32
  • 1
    @gamwtoshiston you should look up a tutorial. This site is not for that purpose. If you have specific problems once you have tried, then demonstrate these here, and people might help, but you are expected to do some research first before coming here. Sessions will likely help you. search "php session tutorial" Commented Jun 24, 2015 at 23:35
  • 2
    Do you know @tototororo? This sounds a lot like a question he asked a couple of hours ago. stackoverflow.com/questions/31037815/… Commented Jun 25, 2015 at 0:10

2 Answers 2

1

I would just store them in the session. That way they, they can be used across php scripts, but are not stored in the long-term. Here's an example:

form.php

<?php
    session_start();
?>

<html>
<body>

<form action="store.php">
    <input type="text" name="x" value="<?php echo $_SESSION['x'] ?>">
    <input type="text" name="y" value="<?php echo $_SESSION['y'] ?>">
    <input type="submit" value="Submit">
</form>


<form action="calculate.php">
    <input type="submit" value="Submit">
</form>

</body>
</html>

store.php

<?php
  // Start the session
  session_start();
  $_SESSION["x"] = $_POST['x']; // substitute your input here
  $_SESSION["y"] = $_POST['y']; // substitute your input here
?>

calculate.php

<?php
  // Start the session
  session_start();
  $result = $_SESSION["x"] * $_SESSION["y"];
  echo $result;
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Let's say in store.php , how can these be stored in sessions while in a form? Can the output of the form be with $_SESSION instead of $_POST?
great, this sounds like a solution, will return with results
You can then use the $_SESSION variables in your form too.
Added form.php and made it as simple as possible... But I agree with previous comments. You really should be looking for a tutorial for this.
@BakerStreet: you want to echo $_SESSION in the form.php
|
0

There is no way to store them in variables. Every request to your server is a new request. You could store the variables in a cookie/session or give them back after pushing the first button and store them in a hidden field in your html form. Or store them in a file on your server.

1 Comment

@gamwtoshiston edit your original question and add the sample code there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.