如何验证php服务器端使用'action'的表单

时间:2014-12-28 08:45:56

标签: php forms validation

我使用这个来向'2co'提交一些信息作为付款方式我需要通过PHP服务器端验证它的一些信息,同时通过邮件向管理员发送通知告诉他有人得到了报酬。

现在这是表格。

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

现在我需要验证这些字段并发送表单,我需要向管理员发送备注

我知道要像这样做

<?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'??
}
?>

3 个答案:

答案 0 :(得分:1)

您可以使用curl最终将其发布到2co http://php.net/manual/en/book.curl.php

发送邮件就像使用邮件功能http://php.net/manual/en/function.mail.php

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


}
?>

正如在这个问题中所说,有非卷曲方法How do I send a POST request with PHP?

答案 1 :(得分:1)

你问的是错误的问题,你应该问过&#34;如何使用php将数据发布到另一台服务器?&#34; 这里有很多答案:

How do I send a POST request with PHP?

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

答案 2 :(得分:0)

您需要在页面加载事件上通过JavaScript执行此操作。

您需要做的更改:

  1. 将操作更改为相同的php页面或不同并按照if(isset($_POST['submit']))检查提交操作。
  2. 验证所有数据,发送邮件,设置php var $validated=true else false并设置一些会话变量,您应该验证以确认,来自付款服务器的响应。例如:payment_id,user_id |这些也应该发送到支付网关和网关返回响应,在你的情况下,我认为sid
  3. if($validated) echo JavaScript代码,用于创建隐藏的form,其中包含隐藏input字段中的所有信息,并在页面加载事件时自动提交表单。
  4. 来自支付服务器的响应,将在您的特定php页面上验证会话/用户信息。

  5. 如果您使用的是jquery,则自动提交代码如下:

    <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  } ?>
    

    对于非jQuery(纯JavaScript)代码,您可以搜索或询问我我将编辑答案。