Page 1 of 1

reg. functions and the best way to go about em

Posted: Fri Jul 16, 2010 11:41 am
by edhen
hi,

I have visited numerous sites regarding functions etc. but i'm still stumped. i am currently working on a register user page with a function to process this, I have my functions included at the top prior to the form. i have the form action to php_self and the register.php looks like this

Code: Select all

$submitted = $_POST['submit'];

if (isset($submitted)) {		
		$fname = $_POST['fname'];
		$lname = $_POST['lname'];
		$aname = $_POST['aname'];
		$pass = $_POST['pass'];
		$confirmpass = $_POST['confirmpass'];
		$email = $_POST['email_address'];
		$country = $_POST['country'];
		$state = $_POST['state'];
		$DOB_day = $_POST['DOB_day'];
		$DOB_month = $_POST['DOB_month'];
		$DOB_year = $_POST['DOB_year'];
		$sex = $_POST['sex'];
		$artist = $_POST['artist'];
		$producer = $_POST['producer'];
		$avatar = $_FILES['avatar'];
		$arr = array($fname, $lname, $aname, $pass, $confirmpass, $email, $country, $state, $DOB_day, $DOB_month, $DOB_year, $sex, $artist, $producer, $avatar);
			register($arr);
}

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="register">
<table width="90%" border="0" cellpadding="2">
  <tr>
    <td><label>First Name</label></td>
    <td><input name="fname" type="text" size="32" maxlength="32" value="<?php if (isset($submitted)) { echo $fname; } ?>" ></td>
    <td><?php echo $error[0]  ?></td>
  </tr>
  <tr>
    <td><label>Last Name</label></td>
    <td><input name="lname" type="text" size="32" maxlength="32" value="<?php if (isset($submitted)) { echo $lname; } ?>" ></td>
    <td><?php echo $error[1] ?></td>
  </tr>
  <tr>
    <td><label>Artist/Band Name</label></td>
    <td><input name="aname" type="text" size="32" maxlength="32" value="<?php if (isset($submitted)) { echo $aname; } ?>" ></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><hr></td>
  </tr>
......................... i wont fill the page with the rest but i guess you get the idea..

now in my function file i have....

Code: Select all

	function register($arr) {

		$fname = $arr[0];
		$lname = $arr[1];
		$aname = $arr[2];
		$pass = $arr[3];
		$confirmpass = $arr[4];
		$email = $arr[5];
		$country = $arr[6];
		$state = $arr[7];
		$DOB_day = $arr[8];
		$DOB_month = $arr[9];
		$DOB_year = $arr[10];
		$sex = $arr[11];
		$artist = $arr[12];
		$producer = $arr[13];
		$avatar = $arr[14];
				
		//code to validate and process will go here
	}
my main concern is.. is this the way to go as it seems a little awkward and doesnt feel right.. although it works it just doesnt feel like its the best way.. so if this is wrong can someone please tell me of a better way to go about it.. i'm kinda new to php but as i got the basics out the way i want to try and slowly ease myself into the more technical stuff..

Re: reg. functions and the best way to go about em

Posted: Fri Jul 16, 2010 2:01 pm
by AbraCadaver
I like to use arrays in my forms. This may simplify things for you:
[text]
<td><input name="register[fname]" type="text" size="32" maxlength="32" value="<?php if (isset($submitted)) { echo $fname; } ?>" ></td>
<td><?php echo $error[0] ?></td>
</tr>
<tr>
<td><label>Last Name</label></td>
<td><input name="register[lname]" type="text" size="32" maxlength="32" value="<?php if (isset($submitted)) { echo $lname; } ?>" ></td>
etc...[/text]
Now you have a $_POST['register'] array so you don't have to assign individual items to an array and then break it apart again. It may also help to have two arrays, one for required fields and one for optional:

[text]name="required[fname]"
name="optional[avatar]"[/text]

Re: reg. functions and the best way to go about em

Posted: Fri Jul 16, 2010 4:11 pm
by edhen
nice..thanks for that... worked out a lot cleaner and less coding involved.... i now seem to only have 1 more problem with functions and hopefully ill have a clearer understanding of how it operates once i bypass this problem... now i gather that the information in a function is not passed back as i have tried echoing variables with no effect yet out side a function it works ok.... as in my example for the function that is to validate and process the form, how to i output errors back to the form? i have tried numerous ways with different results...

Code: Select all

  function validateMail($required){  
  if($required['email'] !== "") { 
    if(ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $required['email'])) { 
      return true; 
    } else { 
	
	$error[0] = "not a valid email";
      
	  return $error;
	  
    } 
  } else { 
    return false; 
  } 
	
}
This script is a play around prior to my main register function just so i can get the hang of it..... with this code as it is.. it will output the error as should but it will do it even when it passes validation... also to my knowledge i gather return ends the function so how would i have all validations processed aswell as arrayed then echoed on the form next to a specific field?