如何使用php会话将字段值从一个页面发送到另一个页面?

时间:2012-06-25 06:44:21

标签: php session

由于我是初学者,我陷入困境。注意:电子邮件发送代码存在于thankyou.php

我的问题是我必须存储所选日期并通过paypal完成付款后转到另一页面。我已经尝试过php会话,但不要在哪里。“

成功完成交易后,我从paypal获取$ _POST中的所有详细信息,如txn_id,payer_email等,但我没有收到我在表单中选择的日期。

请帮助我找到正确的方向。

我正在开发paypal集成,我有一个表单,其中有两个字段(index.php)

  1. 选择日期
  2. 否。门票
  3. 代码index.php:

        <?php
    
    @session_start(); 
        $var_value = $_POST['eventdate'];
        $_SESSION['eventdate'] = $var_value;
        echo $_SESSION['eventdate'];
    ?>
    
    <form name="_xclick" action="https://www.sandbox.paypal.com/r" method="post">
                          <table border="0" cellspacing="0" cellpadding="5" id="tbl_paypal_form">
                            <tr>
                              <td colspan="2"><div id="ticket_amount">£10 per person</div></td>
                            </tr>
                            <tr>
                              <td>Select Event Date <span class="mandatory">*</span></td>
                              <td><select name="eventdate" id="eventdate" class="form_field">
                                  <option value="">Select Date</option>
                                  <option value="Sat 14 July 2012">Sat 14 July 2012</option>
                                  <option value="Sun 15 July 2012">Sun 15 July 2012</option>
                                  <option value="Mon 16 July 2012">Mon 16 July 2012</option>
                                </select></td>
                            </tr>
                            <tr>
                              <td>No. of Tickets <span class="mandatory">*</span></td>
                              <td><input type="text" name="quantity" id="quantity" class="form_field">
                              <span style="color:#bbb; font-size:11px; padding-top:5px;">For ex: 4</span>
                              </td>
                            </tr>
                            <tr>
                              <td colspan="2"><?php
                                    if(!empty($errorMessage)) 
                                    {
                                        echo("<div id='validation_error'>");
                                        echo("<p class='error_message'>There was an error with your form:</p>\n");
                                        echo("<ul class='error_message'>" . $errorMessage . "</ul>\n");
                                        echo("</div>");
                                    }
                                ?></td>
                            </tr>
                            <tr>
                              <td><img src="images/paypal.gif" /></td>
                              <td><input type="image" src="images/btn_paypal.gif" border="0" name="formsubmit" value="Submit" onclick="return ValidateForm();" alt="Make payments with PayPal - it's fast, free and secure!">                              
                              </td>
                            </tr>
                          </table>
                          <input type="hidden" name="cmd" value="_xclick">
                          <input type="hidden" name="business" value="gala@iskconcoventry.com">
                          <input type="hidden" name="currency_code" value="EUR">
                          <input type="hidden" name="item_name" value="Gala Charity Show Tickets">
                          <input type="hidden" name="amount" value="10">
                          <input type="hidden" name="return" value="http://www.vyx.com/gala/thankyou.php">
                          <input type="hidden" name="notify_url" value="http://www.xyx.com/gala/thankyou.php">
                          <input type="hidden" name="cbt" value="Return to The Store">
                          <!--<input type="hidden" name="eventdate" value="<?php echo $_POST['eventdate'];?>">-->
                        </form>
    

    代码:thankyou.php

    <?php
    
    session_start();
    $ticketdate = $_SESSION['eventdate'];
    //echo $_SESSION['eventdate'];
    echo $ticketdate;
    ?>
    
    <pre style="background:#fff;">
    <?php print_r($_POST);?>
    </pre>
    
    <?php
    
    
    if($_POST['txn_id'] != "") 
    {
        $mail_subject='Ticket Booked for Gala Charity event';
        $mail_body.= 'Hello Dear, <br><br>';
        $mail_body.= 'Below are the ticket booking details for Gala Charity event. <br><br>';
        $mail_body.= '<b>Booking Date : </b>'. $ticketdate .'<br><br>';
        //$mail_body.= '<b>Booking Date : </b>'.trim($_POST['eventdate']).'<br><br>';
        $mail_body.= '<b>Number of tickets buyed : </b>'.trim($_POST['quantity']).'<br><br>';
    
        $mail_body.= '<b>Transaction ID : </b>'.trim($_POST['txn_id']).'<br><br>';
        $mail_body.= '<b>First Name : </b>'.trim($_POST['first_name']).'<br><br>';
        $mail_body.= '<b>Last Name : </b>'.trim($_POST['last_name']).'<br><br>';
        $mail_body.= '<b>Payers Email  : </b>'.trim($_POST['payer_email']).'<br><br>';
        $mail_body.= '<b>Payment Date : </b>'.trim($_POST['payment_date']).'<br><br>';
    
        $mail_body.= '<b>Address Name : </b>'.trim($_POST['address_name']).'<br><br>';
        $mail_body.= '<b>Street : </b>'.trim($_POST['address_street']).'<br><br>';
        $mail_body.= '<b>Zip : </b>'.trim($_POST['address_zip']).'<br><br>';
        $mail_body.= '<b>State : </b>'.trim($_POST['address_state']).'<br><br>';
        $mail_body.= '<b>Country Code : </b>'.trim($_POST['address_country_code']).'<br><br>';
    
    
        $mail_body.= '<b>Item Name : </b>'.trim($_POST['item_name']).'<br><br>';
        $mail_body.= '<b>Total Amount Paid : </b>'.trim($_POST['mc_currency']).'&nbsp;'.trim($_POST['mc_gross']).'<br><br>';
    
        $mail_from=$_POST['payer_email'];
        $mail_to = "myname@example.com";
    
        //$mail_reply_to = $mail_from;
        $mail_headers = "Content-Type: text/html; charset=iso-8859-1\r\nFrom: $mail_from\r\nReply-to: $mail_reply_to\r\n";
        @mail($mail_to, $mail_subject, $mail_body, $mail_headers);
    
    }
    ?>
    

2 个答案:

答案 0 :(得分:2)

您应该使用paypal的自定义数据字段来发送和接收付款的其他值。只需在表单中添加其他输入

即可
<input name="custom" type="hidden" value="" />

如果您需要发送两个或更多值,可以使用|之类的分隔符加入它们,然后在从paypal返回时处理它们。

修改

将表单更改为

<form name="_xclick" action="https://www.sandbox.paypal.com/r" method="post" onsubmit="buildCustomVar()">

在结束</body>代码

之前向您的网页添加一些小的javascript
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
    function buildCustomVar()
    {
        var custom = $('select[name="eventdate"]').val() + "|" + $('input[name="quantity"]').val();
        $('input[name="custom"]').val(custom);
    }
</script>

在服务器端只是简单地爆炸你得到的值

$temp = explode("|", $_POST['custom']);
$date = $temp[0];
$quantity = $temp[1];

答案 1 :(得分:1)

将表单更改为

<form name="_xclick" action="https://www.sandbox.paypal.com/r" method="post" onsubmit="buildCustomVar()">

添加javascript

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
enter code here
    function buildCustomVar()
    {
        var custom = $('select[name="eventdate"]').val() + "|" + $('input[name="quantity"]').val();
        $('input[name="custom"]').val(custom);
    }
</script>