Codehouze
Codehouze

Reputation: 15

How to process two forms on a page in PHP

When processing two forms on a page how do I prevent Undefined Index for a variable that has not been submitted?

I have a page with two forms and I am trying to send one of the forms to the server, but I keep getting variables from the other form as an undefined index.

<form id="dassets" class="d-none" action="process.php" method="post" enctype="multipart/form-data">
  <div>
    <label class="medium my-2" for="assetType">Type</label>
    <input class="form-control my-2" name="coinName" id="assetType" type="text" placeholder="Card Selected" /readonly>
  </div>

  <div class="col px-0">
    <!-- Get current BTC value in dollars -->
    <div id="dataBTC" class="text-primary"></div>

    <label class="medium my-2">Coin value</label>
    <div class="input-group input-group-sm mb-3">
      <div class="input-group-prepend">
        <span class="input-group-text" id="coin-value">BTC</span>
      </div>
      <input type="text" class="form-control" name="quantity" aria-label="BTC" aria-describedby="coin-value">
    </div>
  </div>

  <div class="form-group">
    <div class="col px-0">
      <label for="file01" class="my-2">Please upload a very clear photo of the payment receipt/gift card where necessary</label>

      <div class="form-group mt-4">
        <label for="file01">Upload a file</label>
        <input type="file" class="form-control-file" id="file01">
      </div>
    </div>

    <input type="hidden" name="action" value="BTC">
    <input type="submit" class="btn btn-success btn-rounded px-3 mt-3" value="Trade" name="submit"/>
  </div>

</div>

<!-- Card trade -->
<div class="col-xl-7 card-body d-none" id="dock">

Here is the second form; I added a hidden field to distinguish them:

<form id="amazonForm" class="d-none" action="" method="post" enctype="multipart/form-data">
  <div>
    <label for="cardType">Type</label>
    <input class="form-control" id="typeAmazon" type="text" placeholder="Card Selected" name="ca2rdType" readonly/>
  </div>
  <div class="col px-0">
    <label for="ctry" class="my-2">Country</label>
    <select id="ctryAmazon" class="form-control" name="country">
      <option selected>USD</option>
      <option>EUR</option>
      <option>CAD</option>
      <option>GDP</option>
      <option>CHF</option>
      <option>AUD</option>
    </select>
  </div>
  <div class="col px-0">
    <label for="country" class="my-3">Select Gift Card Type</label>

    <!--Amazon Gift Cards -->
    <div id="amazonUsa" class="d-none">

      <!-- Cash Receipt -->
      <div class="custom-control custom-checkbox ">
        <input type="checkbox" class="custom-control-input" id="amazonCash">
        <label class="custom-control-label" for="amazonCash">Cash Receipt</label>
      </div>

      <!-- Debit Receipt -->
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="amazonDebit">
        <label class="custom-control-label" for="amazonDebit">Debit Receipt</label>
      </div>

      <!-- No Receipt -->
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="noReceipt">
        <label class="custom-control-label" for="noReceipt">No Receipt</label>
      </div>

      <!-- Ecode -->
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="eCode">
        <label class="custom-control-label" for="eCode">Ecode</label>
      </div>

      <!-- Physical -->
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="physical">
        <label class="custom-control-label" for="physical">Physical</label>
      </div>

      <!-- Large card -->
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="largecard">
        <label class="custom-control-label" for="largecard">Large Card</label>
      </div>
    </div>

    <div class="form-group">
      <div class="col d-md-flex justify-content-between px-0">
        <div>
          <label for="amount" class="my-3">Input card value amount</label>
          <input class="form-control" id="amount" type="number" placeholder="Enter an amount"/>
        </div>
        <div>
          <label for="equivalent" class="my-3">Equivalent in Naira</label>
          <input class="form-control text-success" id="cashout" type="text" placeholder="Cashout in Naira" readonly/>
        </div>
      </div>
    </div>

    <div class="col px-0">
      <label for="file02" class="my-2">Please upload a very clear photo of the payment receipt/giftcard</label>

      <div class="form-group mt-4">
        <label for="file01">Upload a file</label>
        <input type="file" class="form-control-file" id="file02">
      </div>
    </div>
    <input type="hidden" name="action" value="GTC">
    <input type="submit" class="btn btn-success btn-rounded px-3 mt-3" value="Trade" name="submit_2"/>
  </div>
</form>

Upvotes: 0

Views: 77

Answers (4)

Satish
Satish

Reputation: 726

Try this at SERVER side (i.e. process.php):

if ($_POST['action'] == 'BTC') {
        //action for BTChere
    } else if ($_POST['action'] == 'GTC') {
        //action for GTC
    } else {
        //invalid action!
    }

Refer this answer

Still you have issue then try AJAX.

Also,

You miss action attribute from second form (it's empty).

If you want to submit to process.php then add in second form (No need to check action again)

or other file then add respective path (Need to check action i.e. $_POST['action'] is BTC or GTC).

By default,empty action submit redirect to same file.

Check this for details

Hope you get idea.

Upvotes: 0

collins Onyetube
collins Onyetube

Reputation: 1

Can you show the php block, also are you checking isset("submit")? then since you know the particular action that was performed also check the isset the variables too.

Upvotes: 0

user2417483
user2417483

Reputation:

Give each submit button a different name i.e.

<input name='form1' type='summit' value='submit'>
<input name='form2' type='summit' value='submit'>

In php you can then use

if( isset($_POST['submit']) && $_POST['submit'] == 'form1' ) {
     // do form1 stuff
} else if( isset($_POST['submit']) && $_POST['submit'] == 'form2' ) {
     // do form 2 stuff
}

Upvotes: 2

Da Mahdi03
Da Mahdi03

Reputation: 1608

Just use an if statement in your PHP to check if your variables exist before you use them, and if they are undefined then you can deal with that error in a cleaner fashion.

Upvotes: 2

Related Questions