Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help with php form validation



1:10 am on Jan 9, 2011 (gmt 0)

I am trying to have a payment form validated prior to being submitted to the gateway. I already have client side validation but need it to pass through server validation. I have tried to find info about this but it seem as if where one person says to do it one way, there are 5 other people saying not to. I am very frustrated and any advice would be great. even if its just an overview of the process.


6:52 pm on Jan 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Welcome to WebmasterWorld ryan1127

What data you want to validate? then we can talk about "how to"


7:39 pm on Jan 9, 2011 (gmt 0)

Sorry. I have written a payment form that will eventually need to post to our payment gateway. The information also needs validated on the server side prior to being posted. I can do both of these steps individually but can't understand the process of doing both together. For example:

- User fills out form and clicks submit.
- Form action is set to post to self and then info is validated with php.
- If any errors, user stays on form and missing fields are highlighted.
- If no errors, form data needs to then post to payment gateway url.

This is how I am picturing it in my head, but the problem is if the action of the form is set to post to itself for validation, how can it then automatically post to the gateway url given there are no errors?


11:14 pm on Jan 9, 2011 (gmt 0)

10+ Year Member

It will only post to the gateway url if the section of code doing the validation is executed successfully, i.e. the user didn't make any errors. I appreciate it takes a bit of getting your head around, but it can be done with a form that posts back to itself, it's just a case of structuring the form and its processing routines correctly.



5:33 am on Jan 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

This is how I am picturing it in my head, but the problem is if the action of the form is set to post to itself for validation, how can it then automatically post to the gateway url given there are no errors?

cURL is your first answer here, after you have validated the php data you can use cURL library to post this data to your gateway url and then redirect or process accordingly



9:23 pm on Jan 15, 2011 (gmt 0)

First of all, thank you guys for pointing me in the right direction. I have picked up quite a bit. I have beed able to post my form back to itself, validate it, and then use curl to send all fields to the gateway. Everything is working great except for one thing:

One of the fields I send to the gateway is a return URL (for response). The gateway then sends all response variables to the return url as GET.

The problem though is when it does this, its taking the content from my return url and displaying it on the original form page, under the content from that page. It doesn't redirect to the new page. Here is a simplified example of the code I'm using:

Original page - form.php:

<form id="order" method="post" action="form.php">

<p><label>First Name</label><input type="text" name="firstname" class="text-input" id="firstname" value="<? echo $firstname?>"/></p>

<p><label>Last Name</label><input type="text" name="lastname" class="text-input" id="lastname" value="<? echo $lastname?>"/></p>

<input type="image" src="mybtn.png" class="order-submit" name="submit" value="Submit Order"/>


//code to put post info into variables and validate

//set return url
$ReturnURL = 'https://www.example.com/response.php';

//create post string
$fields = array(

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }

//send it to gateway
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.examplegateway.com/process.asp");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$Response = trim(curl_exec($ch));
echo $Response;

curl_close ($ch);

My response page, response.php, contains:
echo $_GET["firstname"];
echo $_GET["lastname"];

So whats happening is it takes everything from response.php and displays it at the bottom of form.php where I echoed the response variable. If I don't echo the variable nothing is displayed.

$Response = trim(curl_exec($ch));
echo $Response;

How can it just redirect to the response.php page?

Thanks for your help


3:44 pm on Jan 16, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Hi all,

Just my 10p while I skim the lists! short form tags: <? echo "hello!"; ?> are not good to use. Full tags: <?php echo "hello"; ?> are much more widely accepted and generally referred to as the "better practise" option, and will work on any server.

Just something that jumped off the screen to me :)



11:41 pm on Jan 16, 2011 (gmt 0)

Thanks for the response. I have all the right tags in my actual script. I was just showing a simplified example of what is going on. Any ideas as to the cause of the problem?


5:43 pm on Jan 17, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Try something along these lines. Your script is executing linear, top to bottom.

if ([some input from the form]) {
// validate
if ([validate error]) {
output form here with error
else {
if ($Response = trim(curl_exec($ch))) {
// output your localized response here
else {
// There should be some form error to display here.
// Or possibly an error from the gateway.
else {
// output the form here

Featured Threads

Hot Threads This Week

Hot Threads This Month