在单击php

时间:2017-07-01 07:07:25

标签: php

我有两个php文件,一个包含一个表单,另一个用于显示输入的内容和保存按钮。在确认细节正确后,他必须保存并将数据插入表中。我已经给出了以下代码

 <!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">     
 </script>  
 <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>  
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">      </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">  </script>
 </head>
<body>
 <?php
   $roll=$_POST['roll'];
   $sname=$_POST['sname'];
   $fname=$_POST['fname'];
   $gender=$_POST['gender'];
   $aadhar=$_POST['aadhar'];
   $caste=$_POST['caste'];
 ?>
  <div class="container container-center">     
      <!-------------total panel------------------------------------>
      <div class="panel-info">
    <!------panel heading------>        
    <div class="panel-heading">
        <h2><center><u>STUDENT DETAILS</u></center></h2>
    </div> <!-panel heading->      
    <div class="panel-body">
      <form method="post" action="">
        <table align="center" border="0">
           <tbody>              
             <tr>
               <td rowspan="7"><?php echo"<img src=/photo/$roll.jpg width='200px' height='300px' id='photo'>"; ?></td> 
               <td>1. Roll Number & Photo:</td>
               <td><?php echo"".strtoupper($roll).""; ?></td>                              
             </tr>
             <tr>
               <td>2. Name of the Student:</td>
               <td><?php echo"".strtoupper($sname).""; ?></td>             
             </tr>
             <tr>
               <td>3. Name of the Father:</td>
               <td><?php echo"".strtoupper($fname).""; ?></td>             
             </tr>
             <tr>
               <td>4. Gender:</td>
               <td><?php 
                    if($gender=='M')
                    { echo"Male";}                        
                    else
                    {echo"Female";} 
                   ?></td>             
             </tr>
              <tr>
               <td>5. Aadhar Number:</td>
               <td><?php echo"$aadhar"; ?></td>            
             </tr>
             <tr>
               <td>6. Caste:</td>
               <td><?php echo"$caste"; ?></td>             
             </tr>
              <tr>                 
               <td colspan="2" align="center"><input type="submit" id="submit" name="submit" class="btn btn-info" value="save"></td>               
             </tr>
           </tbody>
        </table>                
       </form>    
    </div>      
   </div>  
</div>
 <?php
   if(isset($_POST['submit']))
 {
    echo"<script>alert('success')</script>";
 }
?>
</body>

当我单击“保存”按钮时,只需保存数据。但即使没有点击保存,isset中的php代码也在执行。 $ _POST获得的数据来自表格。

请帮帮我。

2 个答案:

答案 0 :(得分:1)

  

注意:

实际问题是,您在这个页面上没有获取数据..在POST请求中使用给定变量,以便请检查它。其他代码很好。 ! (Here I am Just Pass Dummy Data)如下所示。

   $roll= '1234';
   $sname='XYZ';
   $fname= 'ABC';
   $gender= 'M';
   $aadhar= '123456789100';
   $caste= 'A12345';
  

解决方案:您必须Get DATA Here post来自first_php_page表单的roll,sname,fname,gender,aadhar,caste用户输入有关给定字段的详细信息:secound_php_page < / p>

  

在此Submit后,检查varable按钮是否已设置,然后将值存储在below Code:Insert

     

注意:在结束时YoU必须写<?php if(isset($_POST['Submit'])){ $roll=$_POST['roll']; $sname=$_POST['sname']; $fname=$_POST['fname']; $gender=$_POST['gender']; $aadhar=$_POST['aadhar']; $caste=$_POST['caste']; } ?> 查询保存按钮单击。将数据存储在数据库中。

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

   <?php
   /*
   $roll=$_POST['roll'];
   $sname=$_POST['sname'];
   $fname=$_POST['fname'];
   $gender=$_POST['gender'];
   $aadhar=$_POST['aadhar'];
   $caste=$_POST['caste'];
   */

   // SET THE DUMMY DATA FOR Result check

   $roll= '1234';
   $sname='XYZ';
   $fname= 'ABC';
   $gender= 'M';
   $aadhar= '123456789100';
   $caste= 'A12345';


   ?>

  <div class="container container-center">     
      <!-- total panel -->
      <div class="panel-info">
    <!-- panel heading -->        
    <div class="panel-heading">
        <h2><center><u>STUDENT DETAILS</u></center></h2>
    </div> 
    <!--panel heading -->      
    <div class="panel-body">
      <form method="post" action="">
        <table align="center" border="0">
           <tbody>              
             <tr>
               <td rowspan="7"><?php echo"<img src=/photo/$roll.jpg width='200px' height='300px' id='photo'>"; ?></td> 
               <td>1. Roll Number & Photo:</td>
               <td><?php echo"".strtoupper($roll).""; ?></td>                              
             </tr>
             <tr>
               <td>2. Name of the Student:</td>
               <td><?php echo"".strtoupper($sname).""; ?></td>             
             </tr>
             <tr>
               <td>3. Name of the Father:</td>
               <td><?php echo"".strtoupper($fname).""; ?></td>             
             </tr>
             <tr>
               <td>4. Gender:</td>
               <td><?php 
                    if($gender=='M')
                    { echo"Male";}                        
                    else
                    {echo"Female";} 
                   ?></td>             
             </tr>
              <tr>
               <td>5. Aadhar Number:</td>
               <td><?php echo"$aadhar"; ?></td>            
             </tr>
             <tr>
               <td>6. Caste:</td>
               <td><?php echo"$caste"; ?></td>             
             </tr>
              <tr>                 
               <td colspan="2" align="center"><input type="submit" id="submit" name="submit" class="btn btn-info" value="save"></td>               
             </tr>
           </tbody>
        </table>                
       </form>    
    </div>      
   </div>  
</div>
 <?php
   if(isset($_POST['submit']))
 {
    echo"<script>alert('success')</script>";
 }
?>
</body>
</HTML>
//div[@id="tab-1"]//h2/text()
  

OUTPUT:数据显示

enter image description here

  

AND ONCLICK SAVE

enter image description here

答案 1 :(得分:0)

您的代码存在一些问题,例如:

  • 如果您没有输入,那么表格和保存按钮(在另一个页面中)是没有意义的元素并且不使用$_POST超全局发送您的数据。使用type="hidden"为每个字段创建输入元素,例如:

    <td><?php echo strtoupper($roll); ?><input type="hidden" name="roll" value="<?php echo strtoupper($roll); ?>"></td>
    

    这是完整的表单代码,

    // your code
    <form method="post" action="">
        <table align="center" border="0">
            <tbody>              
            <tr>
                <td rowspan="7"><?php echo"<img src=/photo/$roll.jpg width='200px' height='300px' id='photo'>"; ?></td> 
                <td>1. Roll Number & Photo:</td>
                <td><?php echo strtoupper($roll); ?><input type="hidden" name="roll" value="<?php echo strtoupper($roll); ?>"></td>                              
            </tr>
            <tr>
                <td>2. Name of the Student:</td>
                <td><?php echo strtoupper($sname); ?><input type="hidden" name="sname" value="<?php echo strtoupper($sname); ?>"></td>             
            </tr>
            <tr>
                <td>3. Name of the Father:</td>
                <td><?php echo strtoupper($fname); ?><input type="hidden" name="fname" value="<?php echo strtoupper($fname); ?>"></td>             
            </tr>
            <tr>
                <td>4. Gender:</td>
                <td>
                <?php 
                    if($gender=='M'){ 
                        echo"Male";
                    }else{
                        echo"Female";
                    } 
                ?>
                <input type="hidden" name="gender" value="<?php echo $gender; ?>">
                </td>             
            </tr>
            <tr>
                <td>5. Aadhar Number:</td>
                <td><?php echo $aadhar; ?><input type="hidden" name="aadhar" value="<?php echo $aadhar; ?>"></td>            
            </tr>
            <tr>
                <td>6. Caste:</td>
                <td><?php echo $caste; ?><input type="hidden" name="caste" value="<?php echo $caste; ?>"></td>             
            </tr>
            <tr>                 
                <td colspan="2" align="center"><input type="submit" id="submit" name="submit" class="btn btn-info" value="save"></td>               
            </tr>
            </tbody>
        </table>                
    </form>  
    // your code  
    
  • 我没有看到您将表单数据插入表格的任何代码,您应该在另一个页面中有一个这样的代码块(您可以在其中)已显示表单数据并具有保存按钮):

    if(isset($_POST['submit'])){
        $roll = $_POST['roll'];
        $sname = $_POST['sname'];
        $fname = $_POST['fname'];
        $gender = $_POST['gender'];
        $aadhar = $_POST['aadhar'];
        $caste = $_POST['caste'];
    
        // your INSERT operation
    }