?
Solved

getting child script to see the parent script's form field values?

Posted on 2004-11-06
5
Medium Priority
?
222 Views
Last Modified: 2008-02-26
Currently I have a php page/script and at some point in the script I have an include statement with the parameter of another php script.
So its :
"parent.php"
--------------------
<?

/*
 code
*/
include("child.php");
/*
 more code
*/
?>
When I click a button generated from the child.php script, it submits to the parent script's form element. The problem I am having is after doing so, I can see a form hidden field value in the parent  script, but the child script does not see that value. I need the child script to see that value in order to do some processing. Is there any way I can get the child script to see the parent scripts form field values?

Thanks!
0
Comment
Question by:mistertransistor
5 Comments
 
LVL 1

Expert Comment

by:Snow-Storm
ID: 12516137
That is weird because when you include them they should become like one file.  Try using require instead of include to see if its messing up somewhere with inclusion.
0
 
LVL 1

Expert Comment

by:waan
ID: 12519918
Could you possibly supply a little more detail, especially in relation to the parent and child scripts
0
 
LVL 2

Accepted Solution

by:
aabbas earned 300 total points
ID: 12522422
Make make sure, your child script does not access the hidden value withing a function. Functions have their own scope for every variable.

Therefore, you might need to use global keyword within child's function to make that hidden variable of parent script visible to child function too.

e.g.

"parent.php"
--------------------
<?
$secret = $_REQUEST["secret"];
/*
 code
*/
<input type=hidden name=secret value="Bravo!">
/*
 code
*/
include("child.php");
/*
 more code
*/
function printValue () {
  global $secret; // <--- THIS WILL MAKE THE PARENT VARIABLE VISIBLE TO CHILD.
  echo $secret;
}
/*
 more code
*/
?>

Hope, it will help.

Best of luck.
0
 

Author Comment

by:mistertransistor
ID: 12618383
Sorry for the delay in responding. Thanks a lot aabbas, using $_REQUEST["fieldname"] worked out just fine. At first I thought I just needed the parent script to set that variable as global which I did but still no dice. Then using $_REQUEST the value showed up so all is well now. Thanks again! (I wonder if setting global variables are even needed if you can just use $_REQUEST array? well another thought for another time. Thanks again!
0
 
LVL 2

Expert Comment

by:aabbas
ID: 12622609
Well, Mister Transistor!

$_REQUEST["fieldname"] is used to receive the variable values by a submitted form. On the other hand, global keyword is used to make an out-of-scope variable visible to a child (sub-scope) function.

If you are not using function, then you don't need to put global there. e.g.
$a = $_REQUEST["secret"];
echo $a;

While, if you are using function then you will need to put global to access the value, otherwise it will be blank. e.g.
$a = $_REQUEST["secret"];
function b() {
  echo $a; // <== It won't work, because it will create $a with its own (new) scope.
}
function b() {
  global $a;
  echo $a; // <== It will work because it will get the value from the parent scope of $a.
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
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 …
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 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