Solved

Passing variables or constants through a section of php code

Posted on 2011-03-24
4
256 Views
Last Modified: 2012-06-22
Be gentle, I am very new to PHP coding so I am sure there is a simple explination to this I am just missing. I have a long section of PHP code that grabs $_POST information passed from another page. That code works flawlessly up to a point, then my variable $CustID is reset to null, even if I make a constant called custid2 it too is reset at the same point.

 echo "test1>".$CustID."< <br />";
define("custid2", $CustID);
echo "test1b>".custid2."< <br />";
if (isset($_POST['submit'])) {
echo "test2>".$CustID."< <br />";
echo "test3>".custid2."< <br />";

Open in new window

Line 1 displays "91"
Line 3 displays "91"
Lines 5 and 6 display nothing at all.

Line 4 executes after the submit button is clicked on an included form. This is intended to get information from the user, combine information obtained earlier in the code (for example, the $CustID) and use all that information to insert a record into a table.

Ideas?
0
Comment
Question by:AMPLECOMPUTERS
4 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 35212956
You have to include in the included form all values you wish to pass as hidden fields:

<form id='included' action='yourpage.php' method='post'>
<input type='text' name='' id=''>
<input type='hidden'> name='custID' value='$custID'>
<input type='submit'>
</form>

This way when your user submit additional info all values will be passed.  If you don't do this, custID and all other values will be lost, even if you have used $_SERVER['PHP_SELF'] as action for your included form.
If this is not too clòear, feel free to ask.

Cheers
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35213790
Please post your form, particulare, the HTML for the SUBMIT button.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 35213854
You could consider storing the variables in a session, so they're available throughout your site. You shouldn't be using DEFINE to set variables - only constants.

When a user submits your form, validate the data and then store the values in the $_SESSION array. You must include session_start() and the beginning of each page in order to access the session functions.

Once you're finished with your session, call session_destroy() to clear all the values.

For more info on session - http://www.w3schools.com/PHP/php_sessions.asp


session_start();
$_SESSION['CustID'] = 91;

...

echo $_SESSION['CustID'];

Open in new window

0
 

Author Closing Comment

by:AMPLECOMPUTERS
ID: 35215668
That idea worked perfectly. After a little tweaking to your code I got it to work exactly the way I wanted, thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now