多个表单标记不能在一个php表单

时间:2016-05-26 11:09:05

标签: php curl

我正在使用下面的php页面。我已经将多个表单标记和操作属性作为#符号。我的代码适用于第一个标签,即“Pin代码可用性”和#39;并没有为第二个标签工作'生成运单'。我该如何解决这个问题。同一段代码分开工作。如果我在表单的action属性中使用php文件名。我的输出将重定向到PHP页面。它不会保留在同一页面上。

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

2 个答案:

答案 0 :(得分:1)

对于两个表单提交,您只需检查是否设置了$_POST['submit']。因此,即使您提交第二个表单,第一个if条件也是如此。

尝试在两种表单中添加隐藏字段,例如 form_name 。与$_POST['submit']一起检查(在if条件下)。

这应该可以解决你的问题。

答案 1 :(得分:1)

无需添加隐藏字段, 只需为你的提交按钮命名'btn1'和'btn2',然后点击

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