Page 1 of 2
Functions
Posted: Tue Oct 10, 2006 3:37 pm
by bob_the _builder
Hi,
I guess you cant call on a function() within a function ..
Whats the best way to store post variables to call on at anytime within functions?
Code: Select all
$fname = ValidateInput($_POST['fname']);
$lname = ValidateInput($_POST['lname']);
function add() {
// if ($_POST['edit'] == 'edit') {
query database for records here
}else{
// call for the stored $_POST data here
}
Thanks
Re: Functions
Posted: Tue Oct 10, 2006 3:39 pm
by bob_the _builder
..
Re: Functions
Posted: Tue Oct 10, 2006 4:02 pm
by volka
bob_the _builder wrote:I guess you cant call on a function() within a function ..
but you can
Code: Select all
<?php
function foo() {
echo 'bar';
}
function xyz() {
foo();
}
xyz();
?>
bob_the _builder wrote:Whats the best way to store post variables to call on at anytime within functions?
The array _POST is "superglobal", i.e. it can be accessed from anywhere.
Code: Select all
<?php
function foo() {
if (isset($_POST['bar']) && 0<strlen($_POST['bar']) ) {
echo '<pre>bar: ', $_POST['bar'], '</pre>';
}
}
function xyz() {
foo();
}
?>
<html>
<head><title>post test</title></head>
<body>
<?php xyz(); ?>
<form method="post">
<div>
<input type="text" name="bar" />
<input type="submit" />
</div>
</form>
</body>
</html>
Posted: Tue Oct 10, 2006 4:50 pm
by bob_the _builder
Hi,
I had allready tried:
Code: Select all
function post() {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
}
function form() {
if ($_POST['edit'] == edit ) {
// query database for record
}else{
post();
}
<div>
<input type="text" name="fname" value="<?php echo $fname; ?>" />
<input type="text" name="lname" value="<?php echo $lname; ?>" />
</div>
}
But form(); is called and $_POST data is presant it isnt been echoed back into the text fields. I think its the same concept as your posted code?
Thanks
Posted: Tue Oct 10, 2006 4:54 pm
by Chris Corbyn
That's a scope issue. Variables created inside the funcion can only be seen inside that function unless you specify otherwise. There are to approaches to what you're doing here:
1. Specify the variables as global
Code: Select all
function post() {
global $fname, $lname;
$fname = $_POST['fname'];
$lname = $_POST['lname'];
}
2. Pass them by-reference
Code: Select all
function post(&$var1, &$var2) {
$var1 = $_POST['fname'];
$var2 = $_POST['lname'];
}
post($fname, $lname);
Posted: Tue Oct 10, 2006 4:56 pm
by volka
Why are you assigning $fname = $_POST['fname'];?
$fname and $lname are not superglobal, when post() ends they end to exist.
What do you want to achieve/What's the purpose of the code?
Posted: Tue Oct 10, 2006 5:03 pm
by bob_the _builder
Its so I can share the same form to edit records and create new records ...
$fname = $row['fname']
for an edit or
$fname = $_POST['fname']
the post data being used when the form is reloaded from mandetory fields, and wanting them held in a function so I can call on the post(); to place into the database when the form is successfully submited.
Which is the better approch, global or reference?
Thanks
Posted: Tue Oct 10, 2006 5:51 pm
by volka
reference; passing an array to the function.
Posted: Tue Oct 10, 2006 5:57 pm
by bob_the _builder
I couldnt get global to work .. reference is working, altho seems you need to be repetitve in typing the variables .. ie
function post(&$fname, &$lname, &$company) {
then typing them all over when calling the function
post($fname, $lname, $company);
Thanks
Posted: Tue Oct 10, 2006 6:13 pm
by feyd
The former is a function definition. It does not represent the variable names that are being passed to the function, but the variable names as they would be known inside the function itself.
The latter is the function call, since three (required) arguments were defined for the function, three must be passed in.
Posted: Tue Oct 10, 2006 6:17 pm
by bob_the _builder
So the best option all round is:
Code: Select all
function post(&$fname, &$lname, &$company, &$dname, &$email, &$package, &$notes, &$day, &$month, &$year) {
$fname = ValidateInput($_POST['fname']);
$lname = ValidateInput($_POST['lname']);
$company = ValidateInput($_POST['company']);
$dname = ValidateInput($_POST['dname']);
$email = ValidateInput($_POST['email']);
$package = ValidateInput($_POST['package']);
$notes = ValidateInput($_POST['notes']);
$day = ValidateInput($_POST['day']);
$month = ValidateInput($_POST['month']);
$year = ValidateInput($_POST['year']);
}
post($fname, $lname, $company, $dname, $email, $package, $notes, $day, $month, $year);
<div>
<input type="text" name="fname" value="<?php echo $fname; ?>" />
<input type="text" name="lname" value="<?php echo $lname; ?>" />
</div>
....
Thanks
Posted: Tue Oct 10, 2006 6:46 pm
by RobertGonzalez
Return values from your functions...
Code: Select all
<?php
function assign_from_post($fieldname) {
$post_var = '';
if (isset($_POST[$fieldname]))
{
$post_var = $_POST[$fieldname];
}
return $post_var;
}
?>
Usage:
Code: Select all
<?php
$fname = assign_from_post('fname');
?>
Posted: Wed Oct 11, 2006 2:35 am
by dibyendrah
Inside the function, unless you pass the variable as argument, other variable cannot be accessed unless you call global variable. So, you better call the global POST, GET variable by writing the statement :
will solve your problem.
Code: Select all
function validateInput(){
global $HTTP_POST_VARS;
if(empty($HTTP_POST_VARS['fname'])){
return false;
}else{
return true;
}
}
With Regards,
Dibyendra
Posted: Wed Oct 11, 2006 3:23 am
by CoderGoblin
$_POST, $_GET, $_COOKIE are already globals (superglobals actually) so you don't need to define them as globals in any functions. see
Predefined variables
Posted: Wed Oct 11, 2006 4:46 am
by Chris Corbyn
This function seems a bit pointless anyway to be honest, you can just create those variables in the global scope if that's what your objective is.