- This topic is empty.
-
AuthorPosts
-
June 17, 2015 at 1:43 am #203890AitchParticipant
Please I have a code like if($_SERVER
[‘REQUEST_METHOD’] ==
‘POST’){/* check for all input */} the code is working well but I want to like put it in a function, so I can use it that if the code is true then the processing can proceed to the confirmation page which is on another page.June 17, 2015 at 7:13 am #203907makeshift67Participant<?php function post_process(){ if(isset($_POST['uniqueVariableName'])){ // do my stuff } }
June 18, 2015 at 2:03 am #203962AitchParticipantMaybe you don’t understand my explanation.
The page is like this. Registration.php
<?php
/define variables and set to empty/
$fnameErr=$lnameErr=$usernameErr=$passErr;
$fname=$lname=$username=$pass;//validate form input
function validate(){
if($_SERVER[“REQUEST_METHOD”]==”POST”){
if(empty($_POST[“fname”])){
$fnameErr= “Name is required”;}else{
$fname= test_input($_POST[“fname”]);
/* check if name contains only letters and whitespace*/
If (!preg_match(“/^[a-zA-Z]*$/”, $fname)){$fnameErr=”only letters and white space allowed”;}
}
if(empty($_POST[“lname”])){
$lnameErr= “Name is required”;}else{
$fname= test_input($_POST[“lname”]);
/* check if name contains only letters and whitespace*/
If (!preg_match(“/^[a-zA-Z]*$/”, $lname)){$lnameErr=”only letters and white space allowed”;}
}
if(empty($_POST[“username”])){
$usernameErr= “Name is required”;}else{
$fname= test_input($_POST[“username”]);
/* check if name contains only letters and whitespace*/
If (!preg_match(“/^[a-zA-Z]*$/”, $username)){$usernameErr=”only letters and white space allowed”;}
}
if(empty($_POST[“pass”])){
$passErr= “Name is required”;}else{
$pass= test_input($_POST[“pass”]);
/* check if name contains only letters and whitespace*/
If (!preg_match(“/^[a-zA-Z]*$/”, $pass)){$passErr=”only letters and white space allowed”;}
}
function test_input($data){
$data =trim($data);
$data= stripslashes($data);
$data = htmlspecialchars($data);return $data;
}
}?><form name=”registration” id=”registration” action=”” method=”POST”>
<input type=”text” name=”fname” value=<?php echo $fname; ?>/>
<input type=”text” name=”lname” value=<?php echo $lname; ?>/>
<input type=”text” name=”username” value=<?php echo $username; ?>/>
<input type=”password” name=”pass” value=”password”/>
<input type=”submit” name=”submit” value=”submit”/>
</form>
<?php
if(validate()==true){header(“location:email-confirmation.php”);}?>
Now I want the page to proceed to the confirmation page where the php processing is after the validate is true.
June 18, 2015 at 3:43 am #203966AnonymousInactiveMaybe you don’t understand my explanation.
I’m pretty sure he does. Maybe you don’t understand his answer.
The following is not good code. It makes assumptions about the validations required that are wrong. However, it is similar to what you currently do for validation but in the manner @traq suggests. Please use the principle rather than the code. Also, bear in mind that the
=== true
is important.<?php function validate($input, $fields) { $errors = array(); foreach($fields as $field) { if(empty($input[$field])) { $errors[$field] = "absent"; } else if(!preg_match("/^[a-zA-Z]*$/", $input[$field])) { $errors[$field] = "invalid"; } } if(empty($errors)) { return true; } return $errors; } $fields = array("fname", "lname", "username", "pass"); if($_SERVER["REQUEST_METHOD"] == "POST") { if(validate($_POST, $fields) === true) { header("location:email-confirmation.php"); } }
June 18, 2015 at 10:40 am #203976AitchParticipantThanks for all the response. But the validate function was not included in the main code but I just want the registration.php page to to send the form info to the email-confirmation.php page after all the inputs has been checked.
June 18, 2015 at 3:15 pm #203980AnonymousInactiveI just want the registration.php page to to send the form info to the email-confirmation.php page
Huh. You’re right – I don’t think either of us realised that was your main question.
Use include.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.