Solved

Passing variables or constants through a section of php code

Posted on 2011-03-24
4
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 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