Rashmi Sonke Rashmi Sonke - 6 months ago 23
PHP Question

multiple form tag is not working on one php form

I am using the below php page. and i have taken multiple form tag and action attribute as # sign. My code is working for first tab which is 'Pin code availability' and not working for second tab 'generate waybill'. How can i resolve this. Same piece of code working separately. if i use the php file name in action attribute of form. my output will redirect to the php page. It will not remain on the same page.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Case</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<h2>Shipment Process</h2>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#pincode">Pin code Availability </a></li>
<li ><a data-toggle="tab" href="#waybill">Generate Waybill</a></li>
<li><a data-toggle="tab" href="#menu2">Create an Order</a></li>
<li><a data-toggle="tab" href="#menu3">Track an order</a></li>
<li><a data-toggle="tab" href="#menu2">Invoice Generator</a></li>
<li><a data-toggle="tab" href="#menu3">Packing Slip</a></li>

</ul>

<div class="tab-content">
<div id="pincode" class="tab-pane fade in active">
<h3>Check the Availability of Pin Code Here</h3>
<form action="#" method="POST">
<div class="form-group col-sm-3">
<label for="txtpincode">Enter Pincode:</label>
<input type="text" class="form-control" id="txtpincode" name="txtpin"><br/>
<button type="submit" class="btn btn-primary" name="submit">Check</button>
</div>
</form>
<?php
if(isset($_POST['submit']))
{
//error_reporting(0);
$token="43e6c623dda8f35df4b21fa5X0ec57d58e91154a";
$code= $_POST['txtpin'];
$url="https://test.delhivery.com/c/api/pin-codes/json/?token=".$token."&filter_codes=".$code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);

// curl_setopt($ch, CURLOPT_POSTFIELDS,$query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo curl_error($ch);
$return = curl_exec ($ch);
curl_close ($ch);

//echo $return;//json
$final_result=json_decode($return,true);
/*echo'<pre>';
print_r($final_result);
echo'<pre>';*/

if(!$final_result['delivery_codes'][0]['postal_code']['pin']==$code)
echo"<strong style='color:red;'>"."Service in this area code is not available"."</strong>";


else

echo"<b style='color:green;'>"."Service is available at the Area Code Provided by you"."</b>";
}


?>


</div>
<div id="waybill" class="tab-pane fade">
<h3>Generate your Way Bill Number here</h3>
<form action="#" method="POST">
<button type="submit" class="btn btn-primary" name="submit">Click Me</button>
</form>
<?php
if(isset($_POST['submit']))
{
$ch = curl_init();

$url="https://test.delhivery.com/waybill/api/fetch/json/?cl=FALPDEALS";
//echo $url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);

// curl_setopt($ch, CURLOPT_POSTFIELDS,$query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo curl_error($ch);
$return = curl_exec ($ch);
curl_close ($ch);

//echo $return;//json
$final_result=json_decode($return,true);
echo'<pre>';
print_r($final_result);
echo'<pre>';
}

?>

</div>


</body>
</html>

Answer

No need to add hidden field, just give name to your submit buttons say 'btn1' and 'btn2', and check like

if(isset($_POST['btn1'])){
    // first form submitted
}
else if(isset($_POST['btn2'])){
    // second form submitted
}