0

The question says it all!

I have a form that needs to be validated with php (for javascript disabled browsers). Then, once all errors (if any) are handled, the data needs to be send to another php for processing. I found this post Same page processing useful because it took care of validation on the same page as the form - this saves me the need to pass back values to the original form...etc. [Right now I am ignoring the issue of back button and page refresh retaining the values - totally undesirable from my POV)

However, I noticed, in the same post, that it looks like all the form processing should also be placed on the same page as the form. This (I feel) will make the file ugly and a headache to maintain. So I was wondering:

What if I have the completed (error checked) form processing in a different php file? Is it possible to call that file (not by an include) or send the appropriate data (from the validation code or some other way) to the external processing file (similar to how we send POST data from the actual form)?

The reason I ask is, I have a working system where a form submits data to an external php file, that processes it and saves it to the database. Bad practice, I know, but I only thought of adding the validation after all this was done and working.

I guess it would be simple enough to copy all the code in the external php into the php that validates the form input, but somehow, it doesn't seem to be such a good idea! I would prefer to keep the 'saving to database' part of the processing in a separate file.

Hence the question:

  1. User fills in the form (form.php);
  2. PHP on the same page checks for errors and reloads the form with the entered data in case of errors (form.php).
  3. In case it does not encounter any errors, it sends (?) the data as (maybe) POST values to the processing file (process.php)

Is this possible? If it is please just give a hint - no code as I would like to learn how to do this myself!

Community
  • 1
  • 1
vinaya
  • 262
  • 2
  • 15
  • server side validation is a must, you can't rely on client side validation because any knowledgeable user could hijack your code – omma2289 Aug 08 '13 at 05:59

1 Answers1

0

You can do this with curl

  1. Submit form.php
  2. If errors, reload
  3. If no errors, do curl

curl example

//open connection
$ch = curl_init();

//set the url, POST data
curl_setopt($ch,CURLOPT_URL,$process_url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Source: Post to another page within a PHP script

Community
  • 1
  • 1
doitlikejustin
  • 6,293
  • 2
  • 40
  • 68
  • Thanks @doitlikejustin.. I have never used curl - my experience with php is only about a month! Do I need to do something before I can start using curl? Is there a big learning curve? (I cannot really afford the time to learn something drastically different from PHP right now) – vinaya Aug 08 '13 at 06:11
  • This goes right into your PHP file. They are all PHP functions http://www.php.net/manual/en/ref.curl.php – doitlikejustin Aug 08 '13 at 06:47