?
Solved

Passing variables or constants through a section of php code

Posted on 2011-03-24
4
Medium Priority
?
306 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 2000 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

800 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