1

How does one pass a PHP variable stored on one page that is NOT in a form, go to another page where a function uses it?

The reason I need this is so a user can adjust their form they are using by adding more rows with a submit button.

This is what I want to achieve:

  • $maxrows = stores the total amount of rows currently on the page

  • user presses "add more rows button"

  • another page is loaded which has a function which adds $maxrows + 5

  • once complete, the page redirects to the form

  • the form redisplays the page with 5 more rows

Any ideas how this can be implemented?

Thanks

Preferably not in a session if possible!

4
  • Thanks - im using a framework and the session class I built is purely for login. Im too scared to put any other data into the session that is not needed. Commented Jul 2, 2011 at 17:07
  • Give it a prefix then so you have no chance of conflicts. Something like $_SESSION["my_max_rows"] = 13; will be safe. If you don't want to change the name on your pages you can just assign it back out. $max_rows = $_SESSION["my_max_rows"]; Commented Jul 2, 2011 at 17:15
  • Thanks im just creating a new session class like you said which will deal with form customization. :) Commented Jul 2, 2011 at 17:18
  • Don't forget to accept an answer if one helped you. Commented Jul 3, 2011 at 16:58

5 Answers 5

3

I would use sessions.

Sign up to request clarification or add additional context in comments.

Comments

2

you can use jQuery + ajax to achieve this. Here when you click on add more rows it will add up just dome elements with dynamic names. The concept can be understood from http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding-form-elements/

1 Comment

jQuery would be good in this case. Much cleaner then jumping around pages to change the look of a form.
1

Use it with GET parameters - include the variable in the URL that loads your new page like

http://example.com/file.php?maxrows=10

In file.php you get it by using $_GET instead of $_POST

4 Comments

Its just a variable in my php code its not set by an input box or any form. How do I get it then? Thanks
you said ---another page is loaded which has a function which adds $maxrows + 5 - the URL for this page could be generated with the parameter in the end - or, as Drazisil said - you could use sessions
How do I generate the url with my variable at the end then? Thanks I tried googling $_get but nothing relates to my situation when im trying to pass a global variable to another page. Thanks
Anything with a querystring becomes a $_GET page.php?a=moo becomes $_GET['moo'].
1

You could use $_GET instead of $_POST.

You pass the variable in to the page via the url:

someurl.com/page.php?somevar=hello

Then in page.php you can use $_GET['somevar'] which will equal "hello".

Comments

1

Try using a session.

Put start_session(); at the beginning of your script.

You can store the number of rows in $_SESSION['max_rows'].

Comments

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.