Yousef Altaf
Yousef Altaf

Reputation: 2773

how to validate a form that uses 'action' by php server-side

I uses this from to submit some information to '2co' as a payment method some of this information I need to validate it by PHP server-side and in the same time send a notification to the admin by mail to tell him that someone has been paid.

now this is the form.

<form method="post" action="https://sandbox.2checkout.com/checkout/purchase">
                            <div class="body">

                                <div class="half f_right">
                                    <label for="card_holder_name" class="width_100per"><span class="fontRed2">*</span>card_holder_name
                                        </label>
                                    <input type="text" name="card_holder_name" id="card_holder_name"
                                           class="form_textarea_rec"/>
                                </div>

                                <div class="half">
                                    <label for="country" class="width_100per"><span class="fontRed2">*</span>country</label>
                                    <select name="country" id="country" class="form_select_rec">
                                        <option value="EGY" title="Egypt">Egypt</option>
                                        <option value="SaudiArabia" title="saudi Arabia">Any other</option>
                                    </select>
                                </div>

                                <div class="half">
                                    <label for="street_address" class="width_100per"><span class="fontRed2">* </span>street_address </label>
                                    <textarea name="street_address" id="street_address"
                                              class="form_textarea_rec height_50px"></textarea>
                                </div>


                                <div class="half">
                                    <label for="street_address2" class="width_100per"> address2</label>
                                    <textarea name="street_address2" id="street_address2"
                                              class="form_textarea_rec height_50px"></textarea>
                                </div>


                                <div class="half f_right">
                                    <label for="city" class="width_100per"><span class="fontRed2">* city</span>
                                        </label>
                                    <input id="city" type="text" name="city" class="form_textarea_rec"/>
                                </div>


                                <div class="half f_right">
                                    <label for="state" class="width_100per"><span class="fontRed2">*</span>
                                        States</label>
                                    <input id="state" type="text" name="state" class="form_textarea_rec"/>
                                </div>


                                <div class="half">
                                    <label for="email" class="width_100per"> <span class="fontRed2">*</span>Email</label>
                                    <input id="email" type="text" name="email" class="form_textarea_rec"/>
                                </div>


                                <div class="half">
                                    <label for="zip" class="width_100per"> <span class="fontRed2">*</span>Zip</label>
                                    <input id="zip" type="text" name="zip" class="form_textarea_rec"/>
                                </div>

                                <div class="half">
                                    <label for="phone" class="width_100per"> <span class="fontRed2">*</span>
                                        Phone</label>
                                    <input id="phone" type="text" name="phone" class="form_textarea_rec"/>
                                </div>
                            </div>

                            <?php
                            $fullName = @$_POST['card_holder_name'];
                            ?>
                            <input type='hidden' name='sid' value='*****'/>
                            <input type='hidden' name='mode' value='2CO'/>
                            <input type='hidden' name='li_0_type' value='product'/>
                            <input type='hidden' name='li_0_name' value='<?php echo $planName ?> '/>
                            <input type='hidden' name='li_0_price' value='<?php echo $planPrice ?> '/>
                            <input type='hidden' name='card_holder_name' value='<?php echo $fullName ?>'/>
                            <input type='hidden' name='street_address' value='<?php @$_POST['street_address']; ?>'/>
                            <input type='hidden' name='street_address2' value='<?php @$_POST['street_address2']; ?>'/>
                            <input type='hidden' name='city' value='<?php @$_POST['city']; ?>'/>
                            <input type='hidden' name='state' value='<?php @$_POST['state']; ?>'/>
                            <input type='hidden' name='zip' value='<?php @$_POST['zip']; ?>'/>
                            <input type='hidden' name='country' value='<?php @$_POST['country']; ?>'/>
                            <input type='hidden' name='email' value='<?php @$_POST['email']; ?>'/>

                            <div class="footer">
                                <input name="submit" type="submit" value="Continue" class="s_btn"/>
                            </div>
                        </form>

now I need to validate this fields and send the form and I need to send notes to the admin

I know about make it like this

<?php
if(isset($_POST['submit'])){
//remove the action from the form
//write the validation here ...
//and then send the mail 

//but how to post all this in the end to '2co'??
}
?>

Upvotes: 0

Views: 493

Answers (3)

Exlord
Exlord

Reputation: 5391

you are asking the wrong question, you should have asked "how to post data to another server using php?" there are many answers here are some :

How do I send a POST request with PHP?

http://www.lornajane.net/posts/2010/three-ways-to-make-a-post-request-from-php

Upvotes: 1

aWebDeveloper
aWebDeveloper

Reputation: 38412

You could use curl to finally post it to 2co http://php.net/manual/en/book.curl.php

Sending mail is as simple as using mail function http://php.net/manual/en/function.mail.php

<?php
if(isset($_POST['submit'])){
   //Do validation
   //send mail
   //post to 2co via curl 


}
?>

As said in this question there are non curl ways to do so How do I send a POST request with PHP?

Upvotes: 1

Adarsh Rajput
Adarsh Rajput

Reputation: 1276

You need to do it by JavaScript on page load event.

Changes you need to do:

  1. change action to same php page OR different AND check submit action by if(isset($_POST['submit'])) as you said.
  2. validate all data, send mail, set php var $validated=true else false AND set some session variable which you should validate to confirm, on response from payment server. Ex: payment_id, user_id | these should also be sent to payment gateway and gateway returns on response, in your case I think sid
  3. if($validated) echo the JavaScript code to create a hidden form with all information in hidden input field and submit form automatically on page load event.
  4. on response from payment server, which will come on your specific php page validate session/user info.

If you are using jquery the autosubmit code will be like following:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- if you are not using jQuery and want to then use above code --> 

<?php if($validated) { ?> <script type="text/javascript">
    $(function(){
        var form = $('<form></form>');
        form.attr("method", "post");
        form.attr("action", link);
        form.attr("style","display: none");

        var field = $('<input />');
        field.attr("type", "hidden");
        field.attr("name", "post_var1");
        field.attr("value", "val_1");
        form.append(field); //echo values by php <?php echo $value1; ?> like:

        var field1 = $('<input />');
        field1.attr("type", "hidden");
        field1.attr("name", "li_0_name");
        field1.attr("value", "<?php echo $planName ?>");
        form.append(field1); 

        //do same for all fields

        $(document.body).append(form);
        form.submit();
     }
 </script> <?php  } ?>

For non jQuery (Pure JavaScript) code you can search OR ask me i'll edit the answer.

Upvotes: 0

Related Questions