Forum Moderators: coopster

Message Too Old, No Replies

how to post form data after validation

         

sodani

5:21 am on Feb 20, 2007 (gmt 0)

10+ Year Member



I'm posting the contents of a html form (I have the html form on the same file as the php mail()) to itself in order to validate and then send an e-mail. However, I'd like it to post it to another php file after upon successful validation. Can someone tell me how to do this?

Just a bit of background - the reason why I'd like to do this is because I have multiple contact us forms on a site, and for efficiency purposes, I'd like to have a single form mail script so that I don't have to keep editing the php.

Scally_Ally

9:14 am on Feb 20, 2007 (gmt 0)

10+ Year Member



The only 2 ways i have found to get round this are
A. Setting them into a session and the recalling when you reach the processing file

B. When the validation is dynamically writing out a complete form of hidden variables and then posting them with javascript.

Neither are particularly elegant in my opinion.

Maybe use fputs?

Ally

joelgreen

5:48 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



Lets say html form is file1.php, and "another php file" is file2.php

file1.php
-------------
// validate here
....

// post to another file via GET
file_get_contents("http://www.yoursite.com/file2.php?param1=aaa&param2=bbb");

If you would like to post to another file via POST then you'll need to do that via socket, or using cURL.

brevetoxin

6:31 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



Or, instead of posting the forms to themselves, have a single file that validates all forms and use a hidden form variable, combined with a switch/case construct on the processing page to "route" each form's $_POST data through the appropriate validation code. You can then redirect to appropriate pages after processing the data.

sodani

2:07 am on Feb 21, 2007 (gmt 0)

10+ Year Member



Hmm, all these suggestions are a bit beyond me right now. Time to learn some more php...

bysonary

3:11 am on Feb 21, 2007 (gmt 0)

10+ Year Member



this might not be the sfest way to do thisd but it will work

using the above situation of file1.php and file2.php

file1.php
---------------
//validate here
---------------

now in file1.php you could store all the values you want to keep in hidden form attributes for example if you want to keep $id

<input type="hidden" name="id" value="<?php echo $id;?>" />

i assume that the form contains some kind of submit button once pressed it leads to file2.php

in file2.php put

$id = $_POST['id'];

this will transfer the value from file1.php to file2.php, its similar to $_GET but you dont pass the parameters using the URL, alternativel you could use sessions, just a quick example on sessions

file1.php
------------------
session_start();

//if $id is valid

$_SESSION['id'] = $id;

//else error
------------------

then

file2.php
----------------
session_start();
if(isset($_SESSION['id']) {
echo $_SESSION['id'];
} else {
echo "error";
}

PS - Sorry its all a bit messy and not very clear, Its 3am here and im tired, if you want more detailed examples just say, anyway, im off to bed, oh and sorry for my terrible spelling, again i blame the lack of sleep i have had.

PS - PS - the only valid php is the very last instance of file2.php, this will work if you copy and paste it, the rest have been simplified.

cmarshall

3:51 am on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sessions are real easy to use.

Here's how it works:

File 1:

1: startsession();

2: $_SESSION['form_contents'] = $form_contents;

or

2: $_SESSION['to'] = $to;
3: $_SESSION['from'] = $from;

etc.

File 2:

1: startsession();

2: $form_contents = $_SESSION['form_contents'];

or

2: $to = $_SESSION['to'];
3: $from = $_SESSION['from'];

etc.

It's also just as secure (or more secure) than using POST.

You can find out more here [us3.php.net].