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
Solved

Passing variables or constants through a section of php code

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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…
The viewer will learn how to dynamically set the form action using jQuery.

860 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