提交两个表单,其中form2输入值取决于form1输入值[关闭]

We are going to create a service form , in which customer can enter details, select service and pay cash .

-> Only one form is shown to user ->user select service with price and fill the other detail->Submitting the details , so data's are saved in backend->After saving data, form2 need to work automatically ,but the second form input value is depend on the first form input values and cash .

  1. Which is the best way to implement this ?, two form in same page or different page ?

  2. Can i use ajax for this so that customer need to stay in same page ?

we nee to hide our merchant id , merchant secret from inspect element

Please give example . please see my two forms Form-1

<form method="post" action="" name="form1" id="form1">
 Your name<input type="text" name="cl_name">
 Service < input type="checkbox" name="check-1" value="10"> Wash
 < input type="checkbox" name="check-2" value="10"> clean

 <input type="submit" name="submit" value="submit">
 </form>

Form-2

 <form method="post" action="paymet_gateway_url"  id="form2">
Name <input type="text" value="Form1 your name value" name="p_name" id="p_name">
Value<input type="text" value="Form1 service value (either 10 or 20 )" name="p_cash" id="p_cash">
<inpu type="hidden" value="return url" name="p_r_url">
<input type="submit" name="submit" value="submit">
 </form>

You can do this in different ways...

  1. You can use PHP Wraper to hide your merchant id

for Example:

payment.php ---> wrper_payment.php ---> payment_gateway_url
  1. You can use ajax in your order file

for Example:

order.php ---> payment.php (which has the merchant id) ---> payment_gateway_url

Dear, Javascript and ajax are not different, but if you want to make it very much secured, I suggest you use AngularJS, it's not hard to learn and also faster to execute and implement.

  1. you can Use action variable in POST to define more than one payments

For Example: payment.php

<?php
// For ajax use

$output = array('status'=>false,'error'=>''); //for JSON output

$switch($_POST['action']){
    case 'payment1':
        //process payment1
    break;
    case 'payment2':
        //process payment2
    break;
}

//please dont forgot to set the header
header('Content-Type:application/json;)';
echo json_encode($output);

For Example: order.php

assuming that you are using jQuery for ajax call

<Body>
....
<button onclick="payment('payment1');"> Payment Option 1 </button>
<button onclick="payment('payment2');"> Payment Option 2 </button>

<Script type="text/javascript">
function payment(option){
    $.post('payment.php',{action: option},function(data){
        //provide feedback to customer
    });
}
</script>
</body>

One last thing, do use PHP wrapper because it will hide all your credentials for security...

Best Luck

Ans:

(1) Two form in one page
(2) Of course you can use Ajax
(3) This is an irrelevant question. Because JavaScript has a specific library which is know as JQuery, and Ajax is a JQuery method.

Here is how you can implement the feature.

  1. You can create 2 forms in one PHP page, one below another. Form-1(details form) is visible and form-2(payment form) is invisible.
  2. User will see form-1 and they will put their details. When he hit the save button then a JQuery event handler will call another function who has ajax call inside it.
  3. The ajax method will receive all form-1's data using serialize method, but for that you need to pass the form-1's object to that ajax call.
  4. The ajax method will send those data to a php file which can save those data to your database with status=pending.
  5. At the success of that ajax call you will call another function which will hide form-1 and show form-2.
  6. When user will give input in form-2 then repeat from step-2 to step-5.
  7. Use 2nd ajax function for form-2. At the success of 2nd Ajax call show an alert that payment is complete.