homepage Welcome to WebmasterWorld Guest from 23.23.57.182
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help with php form validation
ryan1127



 
Msg#: 4250852 posted 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.

 

Anyango

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4250852 posted 6:52 pm on Jan 9, 2011 (gmt 0)

Welcome to WebmasterWorld ryan1127

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

ryan1127



 
Msg#: 4250852 posted 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?

Accelerator

10+ Year Member



 
Msg#: 4250852 posted 11:14 pm on Jan 9, 2011 (gmt 0)

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.

Rgds

Anyango

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4250852 posted 5:33 am on Jan 10, 2011 (gmt 0)


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

[php.net...]

ryan1127



 
Msg#: 4250852 posted 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"/>

</form>

<?
//code to put post info into variables and validate
?>

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

//create post string
$fields = array(
'CustomerFirstName'=>urlencode($firstname),
'CustomerLastName'=>urlencode($lastname),
'ReturnURL'=>urlencode($ReturnURL),
);

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

//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_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
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

Matthew1980

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4250852 posted 3:44 pm on Jan 16, 2011 (gmt 0)

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 :)

Cheers,
MRb

ryan1127



 
Msg#: 4250852 posted 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?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4250852 posted 5:43 pm on Jan 17, 2011 (gmt 0)

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
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved