Jump to content

is there a way to shorten this validation? into an array?


grk101

Recommended Posts

hi guys, i really haven't used an array for ever, probably never, i am reading on it on php.net trying to get an idea but  i can't understand it fully.

 

sorry i am a noob.

 

<?php 
   session_start();
   if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
      // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
$error=('required.html');
$choice=$_POST['choice'];
$division=$_POST['division'];
$name=$_POST['name'];
$sex=$_POST['sex'];

if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email']))
{
die("Invalid e-mail address");
}

// Check if the field was empty or not:  
if( empty($_POST['choice']) )  
{  
    header( "Location:$error" );
exit;
}  
if( empty($_POST['division']) )  
{  
    header( "Location:$error" );
   exit;
}
if( empty($_POST['company']) )  
{  
    header( "Location:$error" );
   exit;  
}
if( empty($_POST['comments']) )  
{  
    header( "Location:$error" );
   exit; 
}




  $mailcontent = 
"Registration Information:\n\n".
"I am registering as an: ".$choice ."\n\n".
"Division Selected: ".$division . "\n\n".
  "Player Information:"."\n\n".
"Name: ".$name."\n\n".

   
$subject = "Outdoor Volunteer";
$emailto = "test@test.com\n\n";
$fromname = $name." ".$email;
$others="From: ".$fromname."<".$user_email.">\n";

mail($emailto, $subject, $mailcontent, $others);  



      unset($_SESSION['security_code']);
   } else {
           header( "security.php" );

   }
$os0 = $_POST['os0'];
$os1 = $_POST['os1'];
?>

 

 

how can i shorten this down? i have like 20 more fields to enter

 

<?php
$field = array("division", "name", "age");
foreach($field as $key => $value) {
echo $key . " " . $value . "<br>";
} 

 

I am assuming that i have to create

 

 

I am not sure if that is the proper way to display that they haven't entered a field.

 

I want an error page to be shown and they have to hit back to fill it out.  thats why i made the required.html page

 

but write now i am wrote this from examples i found online echo $key . " " . $value . "<br>";

 

how can i make it work properly?

 

thanks for any help

 

Link to comment
Share on other sites

hey guys while you responded, i was putting up my whole script i only included a bit of it by mistake.

 

what i want to do, eventually it will end up checking 20 fields,

 

Out of the 20 fields only 15 will be required.

 

but it would look weird typing in  15 of these

 

if( empty($_POST['choice']) ) 

    header( "Location:$error" );

exit;

}

Link to comment
Share on other sites

Oh, I didn't see the edited code.

 

This should help:

<?php
$error = 'required.html';

$requiredfields = array('fieldname1', 'fieldname2', 'fieldname3');

foreach ($_POST as $key => $value) {
     if (in_array($key, $requiredfields) && empty($value)) header('Location: ' . $error);
     ${$key} = $value;
}

Link to comment
Share on other sites

hey Ken thanks for your reply,

 

while i was adding the edit code in, it told me someone replied,i was a tad slow. :D

 

 

I dont have to do the following below do i? my friend was telling me i would have to declare the array above my code?

$errors = array(); // set the errors array to empty, by default
$fields = array(); // stores the field values$message = ""; 
$values = array(); // stores the validation rules

Link to comment
Share on other sites

The $errors will be useless because you're going to be redirecting the user anyways. What would you put in for the validation rules? I don't think you would need to do all that.

 

Also, in my code, if you want a field name say it's $_POST['field1'], you can just call $field1 instead because I defined it in the foreach loop. Just in case you didn't know what the last statement in that loop did.

Link to comment
Share on other sites

hey Ken, everything works!

 

question for you though..

 

in you code

<?php
$error = 'required.html';

$requiredfields = array('fieldname1', 'fieldname2', 'fieldname3');

foreach ($_POST as $key => $value) {
     if (in_array($key, $requiredfields) && empty($value)) header('Location: ' . $error);
     ${$key} = $value;
}

 

is there a way to display what fields need to be complete by the user?

 

i tried the following..

 

foreach ($_POST as $key => $value) {
     if (in_array($key, $requiredfields) && empty($value));
    
echo $key . " " . $value . "<br>";

 

which shows all my fields in a new page even the ones entered, so gauranteed i am doing something wrong..

 

In the form processor i have a redirect going if everything is correct, for donations for our club, , unless i can display the errors and what needs to be completed

on a different pages like error.php so the redict doesn't happen to the donations page.

 

 

 

 

Link to comment
Share on other sites

<?php
$error = 'required.html';

$requiredfields = array('fieldname1', 'fieldname2', 'fieldname3');
$errors = array();

foreach ($_POST as $key => $value) {
     if (in_array($key, $requiredfields) && empty($value)) $errors[] = $key;
     ${$key} = $value;
}

Instead of redirecting the user, it will store all the required fields that are empty into the $errors array. Then do as you wish from there.

Link to comment
Share on other sites

Okay so this is the code below:

 

Okay so far it works great, and I appreciate the help;

 

What happens is:

 

the echo statement, lists every field and the ones entered, and the entries, and then the javascript automatically goes to paypal.

 

Thats why i was thinking that if the errors show up on a different page, therefore the redirect wouldn't happen or is that not do able.

 

I guess you could say i am a little bit lost right now !

 

I used a javascript redirect cause i didn't know how to do it in php.

 

 

 

<?php 
   session_start();
   if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
      // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
$error=('required.php');
$choice=$_POST['choice'];
$division=$_POST['division'];
$os0=$_POST['os0'];
$sex=$_POST['sex'];
$birth=$_POST['birth'];
$birthmonth=$_POST['birthmonth'];
$birthyear=$_POST['birthyear'];
$address=$_POST['address'];
$city=$_POST['city'];


if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email']))
{
die("Invalid e-mail address");
}


$requiredfields = array('choice', 'division', 'name','sex','birth','birthmonth','birthyear','address','city');

$errors = array();

foreach ($_POST as $key => $value) {
     if (in_array($key, $requiredfields) && empty($value)) $errors[] = $key;
     ${$key} = $value;

 echo $key . " " . $value . "<br>";

}


  $mailcontent = 
"Registration Information:\n\n".
"I am registering as an: ".$choice ."\n\n".
"Division Selected: ".$division . "\n\n".
  "Player Information:"."\n\n".
"Name: ".$os0."\n\n".
"Sex: ".$sex."\n\n".
"Date Of Birth: " . $birth ."-".$birthmonth."-".$birthyear. "\n\n".
  "Address: ".$address."\n\n".
"City: ".$city."\n\n".
   
$subject = "Youth Online Donation";
$emailto = "test test@gmail.com\n";
$others="From: ".$os0."<".$email.">\n";

mail($emailto, $subject, $mailcontent, $others);  

$others="From: test@info.com\n"
. "Reply-To:  test@.com\n";
$subject = "Youth Online Donation";
$email_to = "$email";
$message = "Thank you for your submission.";
@mail($email_to, $subject ,$message,$others); 


      unset($_SESSION['security_code']);
   } else {
           header( "Location: security.php" );

   }

?>
<html><head>
<script language="JavaScript" type="text/javascript">
<!--
function submitForm() {
document.myForm.submit();
setTimeout('self.close();',10000);
}
//-->
</script>
</head>

<body onLoad="submitForm()">
You are being redirected to PayPal.

<form name="myForm" method="post" action="https://www.paypal.com/cgi-bin/webscr" target="_self">
  <input type="hidden" name="add" value="1">
  <input type="hidden" name="cmd" value="_xclick">
  <input type="hidden" name="business" value="test@gmail.com">
  <input type="hidden" name="item_name" value="Youth Donation">
  <input type="hidden" name="item_number" value="Youth Donation">
  <input type="hidden" name="on0" value="Name">
  <input type="hidden" name="os0" value="<? echo $os0 ?>">
<!--  <input type="hidden" name="on1" value="URL">
  <input type="hidden" name="os1" value="<?// echo $os1 ?>">-->
</form>
</body>
</html>

Link to comment
Share on other sites

Oh, so there is no way to actually let them know what is missing?

 

I thought that is what

 

<?php

  session_start();

  if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {

      // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.

$error=('required.php');

$choice=$_POST['choice'];

$division=$_POST['division'];

$os0=$_POST['os0'];

$sex=$_POST['sex'];

$birth=$_POST['birth'];

$birthmonth=$_POST['birthmonth'];

$birthyear=$_POST['birthyear'];

$address=$_POST['address'];

$city=$_POST['city'];

 

 

if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email']))

{

die("Invalid e-mail address");

}

 

 

$requiredfields = array('choice', 'division', 'name','sex','birth','birthmonth','birthyear','address','city');

 

$errors = array();

 

foreach ($_POST as $key => $value) {

    if (in_array($key, $requiredfields) && empty($value)) $errors[] = $key;

    ${$key} = $value;

 

echo $key . " " . $value . "<br>";

 

}

 

did where the echo would write out the key errors,

Link to comment
Share on other sites

I wish i knew my php like you or even remotely close as you.

 

if i use the following:

 

foreach ($_POST as $key => $value) {
     if (in_array($key, $requiredfields) && empty($value)) header('Location: ' . $error);
     ${$key} = $value;

}

 

what happens is the error pages shows up, which is great, but the page still submits to my email it doesn't stop it at the validation.

 

I tried using the die; command or exit; which stopped the email from being sent, but no page displayed

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.