如何验证使用 PHP 服务器端“操作”的表单


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

我使用它向"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'??
}
?>
您可以使用

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 

}
?>

正如这个问题中所说,有一些非卷曲的方法可以做到这一点 如何使用 PHP 发送 POST 请求?

你问错了问题,你应该问"如何使用PHP将数据发布到另一台服务器?这里有很多答案是一些:

如何使用 PHP 发送 POST 请求?

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

你需要

通过JavaScript在页面加载事件中做到这一点。

您需要执行的更改:

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

如果您使用的是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)代码,您可以搜索或问我,我将编辑答案。