Solved

Passing variables or constants through a section of php code

Posted on 2011-03-24
4
266 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 31

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 43

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

11 Experts available now in Live!

Get 1:1 Help Now