0

我对 PHP 和编程比较陌生,所以请原谅我的无知和语言。

我有一个具有相同输入字段的多个实例的表单(见下文)。我有一个脚本可以处理输入数据,但前提是一次提交一组(也在下面)。我希望在表单的按钮上设置一个提交按钮,该按钮将为页面上的多个数据集执行脚本。

有谁知道我怎样才能让它工作?我无法重命名变量(我试过了),因为接收服务器需要数据标签完全如下所示。任何帮助/方向将不胜感激。谢谢!

我的表格:

<form method="post" action="<?php echo (AUTHORIZENET_SANDBOX ? AuthorizeNetDPM::SANDBOX_URL : AuthorizeNetDPM::LIVE_URL)?>" id="checkout_form">
    <?php
    $time = time();
    $fp_sequence = $time;
    $fp = AuthorizeNetDPM::getFingerprint(AUTHORIZENET_API_LOGIN_ID, AUTHORIZENET_TRANSACTION_KEY, $amount, $fp_sequence, $time);
    $sim = new AuthorizeNetSIM_Form(
        array(
        'x_amount'        => $amount,
        'x_fp_sequence'   => $fp_sequence,
        'x_fp_hash'       => $fp,
        'x_fp_timestamp'  => $time,
        'x_relay_response'=> "TRUE",
        'x_relay_url'     => $coffee_store_relay_url,
        'x_login'         => AUTHORIZENET_API_LOGIN_ID,
        'x_test_request'  => TEST_REQUEST,
        )
    );
    echo $sim->getHiddenFieldString();
}
?>
  <fieldset>
  <H3>Card #1</H3>
  <div>
      <label>Amount</label>
      <input type="text" class="text required" size="4" name="amount" value=""></input>
    </div>
    <div>
      <label>Credit Card Number</label>
      <input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input>
    </div>
    <div>
      <label>Exp.</label>
      <input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input>
    </div>
    <div>
      <label>CCV</label>
      <input type="text" class="text required" size="4" name="x_card_code" value="782"></input>
    </div>
  </fieldset>
  <fieldset>
    <div>
      <label>First Name</label>
      <input type="text" class="text required" size="15" name="x_first_name" value="John"></input>
    </div>
    <div>
      <label>Last Name</label>
      <input type="text" class="text required" size="14" name="x_last_name" value="Doe"></input>
    </div>
  </fieldset>
  <fieldset>
    <div>
      <label>Address</label>
      <input type="text" class="text required" size="26" name="x_address" value="123 Four Street"></input>
    </div>
    <div>
      <label>City</label>
      <input type="text" class="text required" size="15" name="x_city" value="San Francisco"></input>
    </div>
  </fieldset>
  <fieldset>
    <div>
      <label>State</label>
      <input type="text" class="text required" size="4" name="x_state" value="CA"></input>
    </div>
    <div>
      <label>Zip Code</label>
      <input type="text" class="text required" size="9" name="x_zip" value="94133"></input>
    </div>
    <div>
      <label>Country</label>
      <input type="text" class="text required" size="22" name="x_country" value="US"></input>
    </div>
  </fieldset>
    <fieldset>
      <H3>Card #2</H3>
      <div>
      <label>Amount</label>
      <input type="text" class="text required" size="4" name="amount" value=""></input>
    </div>
   <div>
      <label>Credit Card Number</label>
      <input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input>
    </div>
    <div>
      <label>Exp.</label>
      <input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input>
    </div>
    <div>
      <label>CCV</label>
      <input type="text" class="text required" size="4" name="x_card_code" value="782"></input>
    </div>
  </fieldset>
  <fieldset>
    <div>
      <label>First Name</label>
      <input type="text" class="text required" size="15" name="x_first_name" value="John"></input>
    </div>
    <div>
      <label>Last Name</label>
      <input type="text" class="text required" size="14" name="x_last_name" value="Doe"></input>
    </div>
  </fieldset>
  <fieldset>
    <div>
      <label>Address</label>
      <input type="text" class="text required" size="26" name="x_address" value="123 Four Street"></input>
    </div>
    <div>
      <label>City</label>
      <input type="text" class="text required" size="15" name="x_city" value="San Francisco"></input>
    </div>
  </fieldset>
  <fieldset>
    <div>
      <label>State</label>
      <input type="text" class="text required" size="4" name="x_state" value="CA"></input>
    </div>
    <div>
      <label>Zip Code</label>
      <input type="text" class="text required" size="9" name="x_zip" value="94133"></input>
    </div>
    <div>
      <label>Country</label>
      <input type="text" class="text required" size="22" name="x_country" value="US"></input>
    </div>
  </fieldset>
  <input type="submit" value="BUY" class="submit buy">
</form>

我的脚本:

    <?php

require_once 'coffee_store_settings.php';

if ($METHOD_TO_USE == "AIM") {
    $transaction = new AuthorizeNetAIM;
    $transaction->setSandbox(AUTHORIZENET_SANDBOX);
    $transaction->setFields(
        array(
        'amount' => $amount, 
        'card_num' => $_POST['x_card_num'], 
        'exp_date' => $_POST['x_exp_date'],
        'first_name' => $_POST['x_first_name'],
        'last_name' => $_POST['x_last_name'],
        'address' => $_POST['x_address'],
        'city' => $_POST['x_city'],
        'state' => $_POST['x_state'],
        'country' => $_POST['x_country'],
        'zip' => $_POST['x_zip'],
        'email' => $_POST['x_email'],
        'card_code' => $_POST['x_card_code'],
        )
    );
    $response = $transaction->authorizeAndCapture();
    if ($response->approved) {
        // Transaction approved! Do your logic here.
        header('Location: thank_you_page.php?transaction_id=' . $response->transaction_id);
    } else {
        header('Location: error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text);
    }
} elseif (count($_POST)) {
    $response = new AuthorizeNetSIM;
    if ($response->isAuthorizeNet()) {
        if ($response->approved) {
            // Transaction approved! Do your logic here.
            // Redirect the user back to your site.
            $return_url = $site_root . 'thank_you_page.php?transaction_id=' .$response->transaction_id;
        } else {
            // There was a problem. Do your logic here.
            // Redirect the user back to your site.
            $return_url = $site_root . 'error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text;
        }
        echo AuthorizeNetDPM::getRelayResponseSnippet($return_url);
    } else {
        echo "MD5 Hash failed. Check to make sure your MD5 Setting matches the one in config.php";
    }
}
4

2 回答 2

1

PHP 支持表单字段的数组表示法,因此您可以强制值显示多次。例如

<input type="text" name="data[1]" value="foo" />
<input type="text" name="data[a]" value="bar" />
<input type="text" name="data[abcef]" value="baz" />

会给你一个 $_POST 结构,如:

$_POST['data'] = array(
    0 => 'foo'
    'a' => 'bar'
    'abcef' => 'baz'
)

由于您有多个字段的多个副本,所有这些都需要将数据保存在一起,因此您的表单结构如下:

Set #1
<input name="somefield[1]" />
<input name="otherfield[1]" />
...

Set #2
<input name="somefield[2]" />
<input name="otherfield[2]" />

然后在提交时在服务器上执行此操作:

foreach(array_keys($somefield) as $key) {
   $somefield = $_POST['somefield'][$key];
   $otherfield = $_POSt['otherfield'][$key]; 

   ... do something ...
}
于 2013-05-02T17:19:21.983 回答
0

您可以在数组中添加表单输入,并在运行代码时使用 foreach。

<input type="text" class="text required" size="4" name="cards[0][amount]" value=""></input>
<input type="text" class="text required creditcard" size="15" name="cards[0][x_card_num]" value="6011000000000012"></input>

<input type="text" class="text required" size="4" name="cards[1][amount]" value=""></input>
<input type="text" class="text required creditcard" size="15" name="cards[1][x_card_num]" value="6011000000000012"></input>

等等等等等等

你可以通过做得到结果

foreach($_POST['cards'] AS $c) {
    echo $c['amount'];
}
于 2013-05-02T17:16:28.300 回答