【问题标题】:Loop and auto submit HTML form within PHP在 PHP 中循环和自动提交 HTML 表单
【发布时间】:2016-01-20 09:21:12
【问题描述】:

我正在尝试通过 HTML 表单将几个变量发送到另一个 PHP 页面, 我不是 PHP 方面的专家,但在大多数情况下都知道足以让我知道, 并且在其他情况下没有问题,

但是可惜 PHP 不是我的菜,所以我在这里向编码界的大师们寻求智慧的明珠,

好的,让我为你分解一下,

我有一个 PHP IPN (PayPal) 页面,当客户购买时执行,原始代码只是检查成功销售,然后相应地向我和用户发送一些电子邮件,

我还有一个自动生成许可证的许可脚本,

我在页面中使用 HTML 表单,以便在销售被标记为成功时生成许可证并在发送电子邮件的同时发送给用户,

所有非常基本的东西,并且已经在其他 3 个脚本中正常工作,但由于某种原因,这次我无法理解它,而且似乎无法让它工作,

我联系了我的商店脚本的作者,以确保我在表格中使用了正确的变量,他说需要的变量是:

$usr->username

$usr->email

$crow->title

这是 IPN 页面:

<?php
  define("_VALID_PHP", true);
  define("_PIPN", true);

  ini_set('log_errors', true);
  ini_set('error_log', dirname(__file__) . '/ipn_errors.log');
  if (isset($_POST['payment_status'])) {
      require_once ("../../init.php");

      include (BASEPATH . 'lib/class_pp.php');
      $demo = getValue("demo", Content::gTable, "name = 'paypal'");

      $listener = new IpnListener();
      $listener->use_live = $demo;
      $listener->use_ssl = false;
      $listener->use_curl = true;

      try {
          $listener->requirePostMethod();
          $ppver = $listener->processIpn();
      }
      catch (exception $e) {
          error_log($e->getMessage());
          exit(0);
      }

      $payment_status = $_POST['payment_status'];
      $receiver_email = $_POST['receiver_email'];
      $payer_email = $_POST['payer_email'];
      $payer_status = $_POST['payer_status'];
      $mc_currency = $_POST['mc_currency'];
      $mc_fee = isset($_POST['mc_fee']) ? floatval($_POST['mc_fee']) : 0.00;

      list($user_id, $sesid) = explode('_', $_POST['custom']);
      $mc_gross = $_POST['mc_gross'];
      $txn_id = $_POST['txn_id'];

      $getxn_id = $core->verifyTxnId($txn_id);

      $cartrow = $content->getCartContent($sesid);
      $totalrow = Content::getCart($sesid);
      $v1 = compareFloatNumbers($mc_gross, $totalrow->totalprice, "=");
      $items = array();

      $pp_email = getValue("extra", Content::gTable, "name = 'paypal'");

      if ($ppver) {
          if ($_POST['payment_status'] == 'Completed') {
              if ($receiver_email == $pp_email && $v1 == true && $getxn_id == true) {
                  if ($cartrow) {
                      foreach ($cartrow as $crow) {
                          $data = array(
                              'txn_id' => sanitize($txn_id),
                              'pid' => $crow->pid,
                              'uid' => intval($user_id),
                              'downloads' => 0,
                              'file_date' => time(),
                              'ip' => sanitize($_SERVER['REMOTE_ADDR']),
                              'created' => "NOW()",
                              'payer_email' => sanitize($payer_email),
                              'payer_status' => sanitize($payer_status),
                              'item_qty' => $crow->total,
                              'price' => $crow->total * $crow->price,
                              'coupon' => $totalrow->coupon,
                              'tax' => $totalrow->totaltax,
                              'mc_fee' => $mc_fee,
                              'currency' => sanitize($mc_currency),
                              'pp' => "PayPal",
                              'status' => 1,
                              'active' => 1);

                          $items[$crow->price] = $crow->title;   
                          $db->insert(Products::tTable, $data);
                      }

                      unset($crow);
                      $xdata = array(
                        'invid' => date('Ymd').$db->insertid(),
                        'user_id' => intval($user_id),
                        'items' => serialize($items),
                        'coupon' => $totalrow->coupon,
                        'originalprice' => $totalrow->originalprice,
                        'tax' => $totalrow->tax,
                        'totaltax' => $totalrow->totaltax,
                        'total' => $totalrow->total,
                        'totalprice' => $totalrow->totalprice,
                        'currency' => sanitize($_POST['currency_code']),
                        'created' => "NOW()",
                      );
                    $db->insert(Content::inTable, $xdata);                  }

                  /* == Notify Administrator == */

                  require_once (BASEPATH . "lib/class_mailer.php");
                  $row2 = Core::getRowById(Content::eTable, 5);
                  $usr = Core::getRowById(Users::uTable, $user_id);

                  $body = str_replace(array(
                      '[USERNAME]',
                      '[STATUS]',
                      '[PRODUCT]',
                      '[TOTAL]',
                      '[PP]',
                      '[IP]'), array(
                      $usr->username,
                      "Completed",
                      $crow->title,
                      $totalrow->totalprice,
                      "PayPal",
                      $_SERVER['REMOTE_ADDR']), $row2->body);

                  $newbody = cleanOut($body);

                  $mailer = Mailer::sendMail();
                  $message = Swift_Message::newInstance()
                            ->setSubject($row2->subject)
                            ->setTo(array($core->site_email => $core->site_name))
                            ->setFrom(array($core->site_email => $core->site_name))
                            ->setBody($newbody, 'text/html');

                  $mailer->send($message);

                  /* == Notify User == */
                  $row3 = Core::getRowById(Content::eTable, 8);
                  $val = '
                  <table border="0" cellpadding="4" cellspacing="2">';
                    $val .= '
                    <thead>
                      <tr>
                        <td width="20"><strong>#</strong></td>
                        <td class="header">' . Lang::$word->PRD_NAME . '</td>
                        <td class="header">' . Lang::$word->PRD_PRICE . '</td>
                        <td class="header">' . Lang::$word->TXN_QTY . '</td>
                        <td class="header">' . Lang::$word->CKO_TPRICE . '</td>
                      </tr>
                    </thead>
                    <tbody>
                    ';
                    $i = 0;
                    foreach ($cartrow as $ccrow) {
                    $i++;
                    $val .= '
                    <tr>
                      <td style="border-bottom-width:1px; border-bottom-color:#bbb; border-bottom-style:dashed">' . $i . '.</td>
                      <td style="border-bottom-width:1px; border-bottom-color:#bbb; border-bottom-style:dashed">' . sanitize($ccrow->title, 30, false) . 

'</td>
                      <td style="border-bottom-width:1px; border-bottom-color:#bbb; border-bottom-style:dashed">' . $core->formatMoney($ccrow->price) . 

'</td>
                      <td align="center" style="border-bottom-width:1px; border-bottom-color:#bbb; border-bottom-style:dashed">' . $ccrow->total . '</td>
                      <td align="right" style="border-bottom-width:1px; border-bottom-color:#bbb; border-bottom-style:dashed">' . $core->formatMoney($ccrow-

>total * $ccrow->price) . '</td>
                    </tr>
                    ';
                    }
                    unset($ccrow);
                    $val .= '
                    <tr>
                      <td colspan="4" align="right" valign="top" style="border-bottom-width:1px; border-bottom-color:#bbb; border-bottom-

style:dashed"><strong>';
                        $val .= Lang::$word->CKO_SUBT . ':<br />';
                        $val .= Lang::$word->CKO_DISC . ':<br />';
                        $val .= Lang::$word->VAT . ':<br />
                        </strong></td>
                      <td align="right" valign="top" style="border-bottom-width:1px; border-bottom-color:#bbb; border-bottom-style:dashed"><strong>';
                        $val .= $core->formatMoney($totalrow->originalprice) . '<br />';
                        $val .= '- ' . $core->formatMoney($totalrow->coupon) . '<br />';
                        $val .= '+ ' . $core->formatMoney($totalrow->total * $totalrow->tax) . '<br />
                        </strong>';
                        $val .= ' </td>
                    </tr>
                    <tr>
                      <td colspan="4" align="right" valign="top"><strong style="color:#F00">' . Lang::$word->CKO_GTOTAL . ':</strong></td>
                      <td align="right" valign="top"><strong style="color:#F00">' . $core->formatMoney($totalrow->tax * $totalrow->total + $totalrow->total) 

. '</strong></td>
                    </tr>
                      </tbody>
                  </table>';

                  $body3 = str_replace(array(
                      '[USERNAME]',
                      '[ITEMS]',
                      '[SITE_NAME]',
                      '[URL]'), array(
                      $usr->username,
                      $val,
                      $core->site_name,
                      SITEURL), $row3->body);

                  $newbody2 = cleanOut($body3);

                  $mailer2 = Mailer::sendMail();
                  $message2 = Swift_Message::newInstance()
                            ->setSubject($row3->subject)
                            ->setTo(array($usr->email => $usr->username))
                            ->setFrom(array($core->site_email => $core->site_name))
                            ->setBody($newbody2, 'text/html');

                  $mailer2->send($message2);

                  $db->delete(Content::crTable, "user_id='" . $sesid . "'");
                  $db->delete(Content::exTable, "user_id='" . $sesid . "'");
                  $db->delete(Products::rTable, "user_id='" . $sesid . "'");
?>
<!doctype html>
<html>
<head></head>
<body onload="document.createElement('form').submit.call(document.getElementById('Form'))">
<form id='Form' name='form' action='http://www.****************.php' method='post'>
<input type='hidden' name='name' value='<?php echo $_POST['usr->username'];?>'>
<input type='hidden' name='email' value='<?php echo $_POST['usr->email'];?>'>
<input type='hidden' name='original_url' value='http://www.****************ipn.php'>
<input type='hidden' name='projname' value='<?php echo $_POST['crow->title'];?>'>
<input type=hidden name="submit" id="submit" value="Continue"/>
</form>
</body>
</html>
<?php
                  $sale_amount = '$totalrow->totalprice';
                  $product = '$crow->title';
                  include('/home/**********************************.php');
              }

          } else {
              /* == Failed Transaction= = */
              require_once (BASEPATH . "lib/class_mailer.php");
              $row = Core::getRowById(Content::eTable, 6);
              $usr = Core::getRowById(Users::uTable, $user_id);


              $body = str_replace(array('[USERNAME]','[STATUS]','[TOTAL]','[PP]','[IP]'), array(
              $usr->username,"Failed",$core->formatMoney($gross),"PayPal",$_SERVER['REMOTE_ADDR']), $row->body);

              $newbody = cleanOut($body);

              $mailer = Mailer::sendMail();
              $message = Swift_Message::newInstance()
              ->setSubject($row->subject)
              ->setTo(array($core->site_email => $core->site_name))
              ->setFrom(array($core->site_email => $core->site_name))
              ->setBody($newbody, 'text/html');

              $mailer->send($message);

          }
      }
  }
?>

这是我使用的 FORM 代码:

<!doctype html>
<html>
<head></head>
<body onload="document.createElement('form').submit.call(document.getElementById('Form'))">
<form id='Form' name='form' action='http://www.****************.php' method='post'>
<input type='hidden' name='name' value='<?php echo $_POST['usr->username'];?>'>
<input type='hidden' name='email' value='<?php echo $_POST['usr->email'];?>'>
<input type='hidden' name='original_url' value='http://www.****************ipn.php'>
<input type='hidden' name='projname' value='<?php echo $_POST['crow->title'];?>'>
<input type=hidden name="submit" id="submit" value="Continue"/>
</form>
</body>
</html>

我也知道我可能需要使用 foreach() 循环,但是当我到达那里时我会跨过那座桥,我真的需要先弄清楚这一点。

所以我们到了,我想我已经留下了足够的信息、细节和代码, 如果我错过了任何可能有帮助的信息,请告诉我

【问题讨论】:

    标签: php html forms paypal


    【解决方案1】:

    当你使用时

    <input type="hidden" name="name" value="<?php echo $_POST['usr->username'];?>">
    

    据我在您的脚本中看到的,您正在调用一个不存在的 POST 变量 'usr->username'。相反,使用这个:

    <input type="hidden" name="name" value="<?php echo $usr->username;?>">
    

    这会将输入的值设置为您在该行定义的“usr”对象的属性“用户名”:

    $usr = Core::getRowById(Users::uTable, $user_id);
    

    这同样适用于其他字段。

    希望对你有帮助:)

    问候

    【讨论】:

    • 谢谢e.lopez,我认为这将是一些愚蠢的简单大声笑,我会进行编辑,看看会发生什么:)
    猜你喜欢
    • 2015-06-08
    • 2013-01-23
    • 2013-09-07
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多