flhtc Posted February 27, 2008 Share Posted February 27, 2008 I'm a bit of a newb, in over my head and loving it!!! Please excuse the long post, but I'm trying to give as much info as necessary to paint a clear picture. I downloaded a PHP login script and am modifying it to work with our FTP server's database. The scripts are quite complete as far as registering, modifying, etc.. of internal users. They also have quite complete validation of the forms. I've created additional scripts for adding and modifying FTP users. I have two scripts that do almost the same thing. The first is to register a new FTP user. I've been able to create the functions necessary for validation of the FTPuser registration form. Everything works fine. The second script is to modify an existing users profile. Both scripts use identical fields with the exception that the login name cannot be changed in the modification script. My question is; can I pass an 'extra' variable to the registration function? Basically to differentiate between modifying and adding a users info. The validation is the same. I can modify the existing registration function once the variable is passed to update instead of insert into the database, no problem. The thing is that their are three places it goes before the work is done. The forms are submitted to the process.php which checks for the existence of the $_POST for the desired process. This in turn hands it off to a local function that does a little magic and again sends it off somewhere else, this time to the session.php file which does the validation of the form and on success, submits the the variables to the database. The registration for is submitted with a variable of subftpregister to the process.php with a value of 1. I set the modification form to use subftpmodify with the same value of 1. I think if I use the same variable for both submit buttons and set the value differently it could be done. I also think that the key is in the process.php where it states: /* Register a new FTP User */ else if(isset($_POST['subftpregister'])){ $this->procFTPRegister(); } If the value of subftpregister is "1" can I pass that to the session->ftpregister function as an variable tacked on to the end of the existing ones? So the passing line would be: $retval = $session->ftpregister($_POST['vnd_login'], $_POST['vnd_company'], $_POST['vnd_contact'], $_POST['vnd_phone'], $_POST['vnd_site'], $_POST['vnd_email'], $_POST['subftpregister']); Then use that variable to determine whether or not to insert or update? I think it took me longer to write all this than to either re-create the function, or give it a whack Any help and or direction would be appreciated. Thanks, FLHTC Here's snippets of the code so you can see what's happening. Rester New FTP User: /* includes */ include("../include/session.php"); include("../include/header.php"); ?> <html> <title>FTP Registratoin Page</title> <h1>Register an FTP User</h1> Check for existing session etc... /* Start of form */ <form action="../process.php" method="POST"> <table align="left" border="0" cellspacing="0" cellpadding="3"> <tr> <td></td><td><font color = #ff0000>30</font> characters or less.</td> </tr> <tr> <td>Company Username:</td> <td> <input type="text" name="vnd_login" size = "40" maxlength="30" value="<? echo $form->value("vnd_login"); ?>"> <? echo $form->error("vnd_login"); ?> </td> </tr> More fields... /* Submit Form for for validation and insertion into the database */ <tr> <td></td><td align="left"> <input type="hidden" name="subftpregister" value="1"> <input type="submit" value="Submit"> </td> </tr> Modify FTP User: /* includes */ include("../include/session.php"); include("../include/header.php"); ?> <html> <title>FTP Modification Page</title> <h1>Modify an FTP Users Account Info.</h1> Check for session etc... /* Start of forms */ <form name = "getvendorinfo" action="" method="POST"> <table align="left" border="0" cellspacing="0" cellpadding="3"> <tr> <td></td><td><font color = #ff0000>Manditory</font> Can NOT be changed.</td> </tr> <tr> <td>Company Username:</td> <td> <p><select name=vendor onChange="submit();"> <? /* Get the list of vendors from the database, then add them to the dropdown box */ $retval = $database->getVendorList(); foreach ($retval as $initem){ if ($_POST['vendor'] == $initem) { echo "<option selected = yes> $initem </option>\n"; } else { echo "<option> $initem </option>\n"; } } echo $form->value("vendor"); ?> </select></p> More fields that are filled in from the info from the previous field... /* Submit Form for for validation and update the database */ <tr> <td></td><td align="left"> <input type="hidden" name="subftpmodify" value="1"> <input type="submit" value="Submit"> </td> </tr> Process form: A ton of if else if statements for different processes. /* Register a new FTP User */ else if(isset($_POST['subftpregister'])){ $this->procFTPRegister(); } More else if, the the functions... function procFTPRegister(){ global $session, $form; /* Convert usr_login to all lowercase (by option) */ if(ALL_LOWERCASE){ $_POST['vnd_login'] = strtolower($_POST['vnd_login']); } /* FTP Registration attempt */ $retval = $session->ftpregister($_POST['vnd_login'], $_POST['vnd_company'], $_POST['vnd_contact'], $_POST['vnd_phone'], $_POST['vnd_site'], $_POST['vnd_email']); Check the $retval and give error codes, if any. Session.php /* Error Checking */ function ftpregister($sublogin, $subcompany, $subcontact, $subphone, $subsite, $subemail){ global $database, $form, $mailer; //The database, form and mailer object /* Username error checking */ $field = "vnd_login"; //Use field name for vnd_login if(!$sublogin || strlen($sublogin = trim($sublogin)) == 0){ $form->setError($field, "* Username not entered"); } else{ /* Spruce up usr_login, check length */ $sublogin = stripslashes($sublogin); if(strlen($sublogin) < 5){ $form->setError($field, "* Username below 5 characters"); } else if(strlen($sublogin) > 30){ $form->setError($field, "* Username above 30 characters"); } 200+ more lines of validation... /* Update database */ if($database->addFTPUser($sublogin, $subcompany, $subcontact, $subphone, $subsite, $subemail)){ return 0; //New user added succesfully } else{ return 2; //Registration attempt failed } } Link to comment https://forums.phpfreaks.com/topic/93414-question-regarding-passing-variables-to-functions-etc/ Share on other sites More sharing options...
Barand Posted February 28, 2008 Share Posted February 28, 2008 you can have optional args for a function, if that's what you are asking, by definig a default value for the optional arg/s <?php function foo ($a, $b, $op='add') { switch ($op) { case 'add': return $a + $b; case 'sub': return $a - $b; default: return 0; } } echo foo(2,3); // 5 echo foo(5,2,'sub'); // 3 ?> Link to comment https://forums.phpfreaks.com/topic/93414-question-regarding-passing-variables-to-functions-etc/#findComment-478635 Share on other sites More sharing options...
flhtc Posted February 28, 2008 Author Share Posted February 28, 2008 Barand, Thanks for the input. I didn't know it could be done like that. Not exactly what I was after, but Very helpful. I did some more looking at my problem. I guess what it boils down to is the value of the input tag sent along when submitting the form to a function. What's confusing to me is that when submitting a form all the field names and their values are sent just like a variable, well I guess that's because they must be variables. Every thing I've seen in my vast (2 weeks) experience in PHP is that, variables start with a $. Also, the single quote double quote an no quote at all when using variables. I've been shell scripting for 20+ years dabbled in ansi C, Basic, and a few other languages and they all seem to handle variables a little more consistanly, at least it seems that way to me. But, that's the "fun" of learning a new language. This is an awesome and powerful language for building web content. I'll get it eventually. Best Regards Link to comment https://forums.phpfreaks.com/topic/93414-question-regarding-passing-variables-to-functions-etc/#findComment-478656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.