Jump to content

PHP w/ a separate XML or TXT field Validation


martinn82

Recommended Posts

Greetings,

 

Here is a piece of sample code that I will be using in a form (form not complete yet). This specific field will not be validated for entry like other ones BECAUSE I want to validate it against a unique client code ie: XML or TXT document.

 

I am looking to have the following happen:

 

ex: Client Number: 12345678 --> validate against XML or TXT "list" of numbers in a document to prove they are a client if not return the error page specified in the below code.

 

Also can you please tell me is it better to list in XML or TXT also give an example? When it comes to validation I get lost.

 

Thanks

----

// get posted data into local variables

$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));

$EmailTo = "xxxxxx";

$Subject = "Client Inquiry Request";

$clientnumber = Trim(stripslashes($_POST['clientnumber']));

 

// validation

$validationOK=true;

if (Trim($EmailFrom)=="") $validationOK=false;

if (!$validationOK) {

  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";

  exit;

}

 

// prepare email body text

$Body = "";

$Body .= "clientnumber: ";

$Body .= $clientnumber;

$Body .= "\n";

 

// send email

$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

 

// redirect to success page

if ($success){

  print "<meta http-equiv=\"refresh\" content=\"0;URL=success.html\">";

}

else{

  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";

}

?>

----

Link to comment
Share on other sites

"best" for flexibility and protection against data loss would be putting them in a database.  But if you want them in a file, there are 2 basic steps to the validation.

 

1.  Read in the data from the file.  This requires opening the file, reading in the lines and removing newline characters.

2.  Check each number against the submitted number

 

For #1 you could use a tutorial such as this one, found by googling for "php file read tutorial" : http://www.phphelps.com/8_How_to_read_file_in_PHP.shtml

Then once you have a loop that reads in the file, you can add code to check each number.  Keep in mind if there might be extra spaces or other invisible characters in either number.

 

I wouldn't consider XML until you are comfortable with dealing with a plain text file.

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.