简体   繁体   中英

Using the same script multiple times for one form (using PHP)

I am relatively new to PHP and programming, so please pardon my ignorance and language.

I have a form that has multiple instances of the same input fields (see below). I have a script that will process the input data, but only if it is submitted one set at a time(also below). I will like to have one submit button at the button of the form that would execute the script for however many multiples of the data set I have on the page.

Does anyone have any idea how I can get this to work? I cannot rename the variables (I tried that) because the receiving server needs the data labels to appear exactly as they are below. Any help/direction would be greatly appreciated. Thanks!

My form:

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

My script:

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

PHP supports array notation for form fields, so you can force values to show up multiple times. eg

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

would give you a $_POST structure like:

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

Since you'd have multiple copies of multiple fields, all requiring the data to be kept together, then structure your form like:

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

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

and then do this on the server upon submission:

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

   ... do something ...
}

You could add the form inputs in an array and use an foreach when you run the code.

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

etc etc etc.

You can get the results by doing

foreach($_POST['cards'] AS $c) {
    echo $c['amount'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM