duanjupiao4730 2017-04-18 19:24
浏览 47
已采纳

首先单击提交表单提交后显示第二个提交按钮 - Javascript

I've build a function in javascript to show me a second submit but only after the first submit was clicked.

Edited - this is ex.php:

<form method="post" action = "ex.php">
    <input type="submit" id="button" name="search" value="Search" 
    onclick="myFunction();" >
    <br>
     <input type="submit" name="search_close"  id="submit"  style="display: 
     none;" value="Find close results">
    </form>

    <?php 
    if(isset($_POST['search']))
    {
       echo "first search";
    }
     else if(isset($_POST['search_close']))
    {
       echo "second search";
    }
    else {
       echo "nothing";
    }
    ?>
    <script type="text/javascript">
    inputSubmit = document.getElementById("submit");     
    function myFunction(){
       inputSubmit.style.display = "block";    };
    </script>

Updated: I've edited in order to conclude the main problem.

So what I want is this:

a) The user has pressed the first submit button "search" then it will echo on the page "first search" and then show the second submit button "search_close" .

b)Then, if the user has pressed the second submit button it will show "second search".

When first Refreshing:
======
search (button)
======

if clicking the "search" button:
======
search (button)
======
first search  (text)
==================
Find close results (button)
==================

if clicking the "find close results:
======
search (button)
======
first search  (text)
==================
Find close results (button)
==================
second search  (text)

This code doesn't do what I want. Why?

Updated - why the button find close results disappers after one second?

<?php
session_start();
$db=mysqli_connect("localhost","root","","travelersdb");

if(@$_SESSION["username"]){
    ?>
<?php
// function to connect and execute the query
function filterTable($query)
{
    $connect=mysqli_connect("localhost","root","","travelersdb");
    $filter_Result = mysqli_query($connect, $query) or die(mysqli_error($connect));
    return $filter_Result;
}

$submit_value = 0;
if(isset($_POST['search']))
{

    $Destination = $_POST['Destination'];
    $TypeOfTravel = $_POST['TypeOfTravel'];
    $Age= $_POST['Age'];
    $email = $_POST['email'];
    $topic_name =  $_POST['topic_name'];
    $StartingPoint =  $_POST['StartingPoint'];


// search in all table columns
$query = "SELECT * FROM `topics`
              left join `users` on users.username = topics.topic_creator
              WHERE 1=1 "; 
if(!empty($Destination)) {
$query.="AND destination='$Destination'";
}
if(!empty($TypeOfTravel)) {
$query.=" AND typeTravel='$TypeOfTravel'";
}
if(!empty($Age)) {
$query.="AND age='$Age'";
}
if(!empty($email)) {
$query.=" AND email='$email'";
}
if(!empty($topic_name)) {
$query.=" AND topic_name='$topic_name'";
}
if(!empty($StartingPoint)) {
$query.=" AND StartingPoint='$StartingPoint'";
}

$search_result = filterTable($query);
$submit_value = 1; 
}

///Make The search more wider, only for the fields that were in the post
 else if(isset($_POST['search_close']))
{
    $Destination = $_POST['Destination'];
    $TypeOfTravel = $_POST['TypeOfTravel'];
    $topic_name =  $_POST['topic_name'];
    $StartingPoint =  $_POST['StartingPoint'];


// search in all table columns
$query = "SELECT * FROM `topics`
              left join `users` on users.username = topics.topic_creator
              WHERE 1=1 "; 
if(!empty($Destination)) {
$query.="AND destination='$Destination'";
}
if(!empty($TypeOfTravel)) {
$query.=" AND typeTravel='$TypeOfTravel'";
}
if(!empty($topic_name)) {
$query.=" AND topic_name='$topic_name'";
}
if(!empty($StartingPoint)) {
$query.=" AND StartingPoint='$StartingPoint'";
}
$search_result = filterTable($query); 
$submit_value = 2;
}
else {
   $query = "SELECT * FROM `topics`";
   $search_result = filterTable($query);
}
?>

<html>
    <head>

<?php header('Content-Type: text/html; charset=utf-8'); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

        <title>Home Page</title>  
         <style>
            .hidden {
                display: none;
            }
        </style>
        <script type="text/javascript">
            function showHide()
            {
               var checkbox = document.getElementById("chk");
               var hiddeninputs = document.getElementsByClassName("hidden");
               for (var i=0; i != hiddeninputs.length; i++) {
                   if(checkbox.checked){
                       hiddeninputs[i].style.display = "block";
                   } else {
                       hiddeninputs[i].style.display = "none";
                   }
               }
           }

<?php 
if($submit_value == 1 || $submit_value == 2){ ?>
    myFunction();
   inputSubmit = document.getElementById("submit");     
   function myFunction(){
    document.getElementById('submit').style.display = "block";   
    };
   <?php } ?>


        </script>
    <body>

    </body>
    </head>
    <?php include("header.php");?>
    <center>
        </br>
        <a href ="post.php"><button>Post</button></a>
        </br>
        <br/>
        <h4>Simple Serach</h4>



        <form action="" method="post">
            <input type="text" name="Destination" placeholder="Destination" value=
"<?php if(isset($_POST['Destination']))
{echo htmlentities($_POST['Destination']);}?>" ></br>
            <input type="text" name="TypeOfTravel" placeholder="Type Of Travel"
              value=
"<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['TypeOfTravel']);}?>"
                   ></br>
            <input type="text" name="Age" placeholder="Age" value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['Age']);}?>">
       </br>
       </br>
    <!-- Advanced Search-->
       <input type="checkbox" name="chkbox" id="chk" onclick="showHide()" />


       <label for="chk"><b>Advanced search</b></label>
       </br>
       <input type ="text" name="email"  placeholder="Email"  class="hidden" value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['email']);}?>">
       <input type ="text" name="topic_name"  placeholder="topic name" class="hidden"value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['topic_name']);}?>">
       <input type="text" name="StartingPoint" placeholder="Starting Point"  class="hidden"value="<?php if(isset($_POST['TypeOfTravel']))
{echo htmlentities($_POST['StartingPoint']);}?>">
       </br><br/>

  <input type="submit" id="button" name="search" value="Search" 
onclick="myFunction();" >
  <br><br>
 <input type="submit" name="search_close"  id="submit"  style="display: 
 none;" value="Find close results">
        </form>    
        <br/>
        <?php echo '<table border="1px">';?>
                <td width="400px;" style="text-align:center;">
                 Name
                </td>
                  <td width="400px;" style="text-align:center;">
                Destination
                </td>
                 <td width="400px;" style="text-align:center;">
               Type Of Travel:
                </td>
                <td width="80px;" style="text-align: center;">
                  First Name
                </td>
                <td width="80px;" style="text-align: center;">
                  Age
                </td>
                 <td width="400px;" style="text-align:center;">
                 profile picture
                </td>
                <td width="80px;" style="text-align: center;">
                  Creator
                </td>
                <td width="80px;" style="text-align: center;">
                  Date
                </td>
      </tr>   
    </center>
<?php
$sql = "Select * from `topics`";
$check =  mysqli_query($db,$sql); 
$rows = mysqli_num_rows($search_result);
if($rows != 0){
     while ($row = mysqli_fetch_assoc($search_result)){
         $id = $row['topic_id'];
         echo "<tr>";
         //echo "<td>".$row['topic_id']."</td>";
         echo "<td style='text-align:center;'><a href='topic.php?id=$id'>".$row['topic_name']."</a></td>";
         echo "<td>".$row['Destination']."</td>";
         echo "<td>".$row['typeTravel']."</td>";

         $sql_u = "Select * from users where username='".$row['topic_creator']."'";
         $check_u =  mysqli_query($db,$sql_u);
          while ($row_u = mysqli_fetch_assoc($check_u))
                          {
                           $user_id = $row_u['id'];
                           $profile_pic = $row_u['profile_pic'];
                           $firstname = $row_u['firstname'];
                           $age = $row_u['age'];
                            echo "<td>".$firstname."</td>";
                            echo "<td>".$age."</td>";
                            echo "<td><img src='".$profile_pic."' width='10%' height='10%' alt='me'></td>"; 
                            echo "<td><a href='profile.php?id=$user_id'>".$row['topic_creator']."</a></td>";
                          }
          $get_date = $row['date'];
         echo "<td>".$row['date']."</td>";             
         echo "</tr>";
     }
}else {
        echo "No topics found";
      }
echo "</table>";


if (@$_GET['action']=="logout")
{
    session_destroy();
    header('location:login.php');
}
}else 
    {
    echo "You must be logged in.";
    echo "<a href ='login.php'>Click here to login</a>";
    }
    ?>
</br>
</br>
    <body>
    </body>
</html> 
  • 写回答

2条回答 默认 最新

  • duandianwen1723 2017-04-18 19:58
    关注

    you need to set type button cause when you set type submit then form automatically submit so show second not show but it work.

    <form method="post" action = "">
    <input type="submit" id="button" name="search" value="Search" >
    <br>
     <input type="submit" name="search_close"  id="submit"  style="display: 
     none;" value="Find close results">
    </form>
    
    <?php 
    $submit_value = 0;
    if(isset($_POST['search']))
    {
       echo "first search";
       $submit_value = 1;
    
    }
     else if(isset($_POST['search_close']))
    {
       echo "first search";
       echo '<br>';
       echo "second search";
       $submit_value = 2;
    }
    else {
       echo "nothing";
    }
    
    
    ?>
    
    <script type="text/javascript">
    <?php 
    if($submit_value == 1 || $submit_value == 2){ ?>
        document.getElementById('submit').style.display = "block";   
       <?php } ?>
    </script>
    

    check this code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序