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

时间:2017-04-18 19:24:15

标签: javascript php

我在javascript中构建了一个函数,只显示第二次提交,但只有在第一次提交后才会被点击。

已编辑 - 这是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>

更新: 我已编辑,以便总结主要问题。

所以我想要的是:

a)用户按下了第一个提交按钮&#34;搜索&#34;那么它将在页面上回显&#34;首先搜索&#34;然后显示第二个提交按钮&#34; search_close&#34;

b)然后,如果用户按下第二个提交按钮,它将显示&#34;第二个搜索&#34;。

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)

此代码不能满足我的需求。为什么呢?

更新 - 为什么按钮会在一秒钟后找到关闭结果?

<?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 个答案:

答案 0 :(得分:1)

你需要设置类型按钮原因当你设置类型提交然后表单自动提交所以显示第二个不显示但它工作。

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

检查此代码

答案 1 :(得分:0)

我已经清理了你的代码,但是仍有两个提交按钮的问题仍然存在。即使单击第一个按钮导致第二个按钮显示,第一个按钮也会使页面重新加载来自表单action资源的结果。在此示例中,我已取消表单的提交事件,以显示第二个按钮的显示有效。

最后,我认为你这一切都是错的。我认为您应该对服务器端资源进行AJAX调用,并将该调用的结果注入您的页面 - 没有表单也没有提交。

&#13;
&#13;
// Don't forget to add "var" to your variable declarations, otherwise the variables
// become global!
var sub1 = document.getElementById("btnSub1"); 
var sub2 = document.getElementById("btnSub2"); 

// Set up the event handler for the first button
sub1.addEventListener("click", myFunction);

function myFunction(e){
  // Adjust classes, not individual styles
  sub2.classList.remove("hidden");
  
  // Cancel the native event and stop bubbling
  e.preventDefault();
  e.stopPropagation();
};
&#13;
.hidden { display:none; }
&#13;
<form>
  <!-- Submit buttons don't get a name attriute unless you expect their value to be submitted
       along with all the other form data. Inline styles should be avoided and internal style
       sheets used instead. And, inline JavaScript event attributes (onclick, etc.) should not
       be used. -->
  <input type="submit" id="btnSub1" name="search" value="Search" >
  <br>
  <input type="submit" id="btnSub2" class="hidden" 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";
    }
    ?>
&#13;
&#13;
&#13;