PHP - 如何在提交表单后隐藏表单

时间:2013-09-26 15:18:09

标签: php html mysql forms

我有一个容器中的域名检查表单。提交表单后,将检查域名的可用性,并在与表单相同的容器中显示一条消息。当显示消息时,我希望表单和第一个H2消失。我怎么能这样做?

我的代码:

<div id="domain-name-check">
    <h2>Is uw domeinnaam nog vrij?</h2>
    <form method='post' class="clearfix">
        <input type=text name=domain>
            <select name="suffix">
                <option value=".nl">.nl</option>
                <option value=".be">.be</option>
                <option value=".com">.com</option>
                <option value=".net">.net</option>
                <option value=".org">.org</option>
                <option value=".biz">.biz</option>
                <option value=".info">.info</option>
                <option value=".mobi">.mobi</option>
                <option value=".ws">.ws</option>
                <option value=".cc">.cc</option>
            </select>
            <input type=submit name=proses value=Check>
    </form>
    <?php
    if(isset($_POST['proses'])){
        $domain_name = "$_POST[domain]"."$_POST[suffix]";
        $arrHost = @gethostbynamel("$domain_name");
        $date = date('y-m-d');

        if(empty($arrHost)){
            echo "<h2>$domain_name is beschikbaar</h2> <a href='/offerte-aanvragen/".$domain_name."/'>Vraag een offerte aan</a>";
            $availability = "yes";
        }else{
            echo "<h2>$domain_name is helaas bezet</h2> <a href='/offerte-aanvragen/".$domain_name."/verhuizen/'>Verhuis uw domein</a>";
            $availability = "no";
        }

        mysql_query("INSERT domainnames SET dns='".$domain_name."', available='".$availability."', timestamp='".$date."'")or die(mysql_error());

    }
    ?>
</div>

7 个答案:

答案 0 :(得分:8)

更正后的代码: 仅在我们未提交表单值时显示表单。 因此,请在else标记中编写HTML代码,如下所示:

<div id="domain-name-check">

    <?php
    if(isset($_POST['proses'])){
        $domain_name = "$_POST[domain]"."$_POST[suffix]";
        $arrHost = @gethostbynamel("$domain_name");
        $date = date('y-m-d');

        if(empty($arrHost)){
            echo "<h2>$domain_name is beschikbaar</h2> <a href='/offerte-aanvragen/".$domain_name."/'>Vraag een offerte aan</a>";
            $availability = "yes";
        }else{
            echo "<h2>$domain_name is helaas bezet</h2> <a href='/offerte-aanvragen/".$domain_name."/verhuizen/'>Verhuis uw domein</a>";
            $availability = "no";
        }

        mysql_query("INSERT domainnames SET dns='".$domain_name."', available='".$availability."', timestamp='".$date."'")or die(mysql_error());

    }
        else {
        ?>
        <h2>Is uw domeinnaam nog vrij?</h2>
    <form method='post' class="clearfix">
        <input type=text name=domain>
            <select name="suffix">
                <option value=".nl">.nl</option>
                <option value=".be">.be</option>
                <option value=".com">.com</option>
                <option value=".net">.net</option>
                <option value=".org">.org</option>
                <option value=".biz">.biz</option>
                <option value=".info">.info</option>
                <option value=".mobi">.mobi</option>
                <option value=".ws">.ws</option>
                <option value=".cc">.cc</option>
            </select>
            <input type=submit name=proses value=Check>
    </form>
        <?php
        }
    ?>
</div>

答案 1 :(得分:1)

<div id="domain-name-check">
    <?php if(!isset($_POST['proses'])){ ?><h2>Is uw domeinnaam nog vrij?</h2>
    <form method='post' class="clearfix">
        <input type=text name=domain>
            <select name="suffix">
                <option value=".nl">.nl</option>
                <option value=".be">.be</option>
                <option value=".com">.com</option>
                <option value=".net">.net</option>
                <option value=".org">.org</option>
                <option value=".biz">.biz</option>
                <option value=".info">.info</option>
                <option value=".mobi">.mobi</option>
                <option value=".ws">.ws</option>
                <option value=".cc">.cc</option>
            </select>
            <input type=submit name=proses value=Check>
    </form>
    <?php } //endif form submitted
    if(isset($_POST['proses'])){
        $domain_name = "$_POST[domain]"."$_POST[suffix]";
        $arrHost = @gethostbynamel("$domain_name");
        $date = date('y-m-d');

        if(empty($arrHost)){
            echo "<h2>$domain_name is beschikbaar</h2> <a href='/offerte-aanvragen/".$domain_name."/'>Vraag een offerte aan</a>";
            $availability = "yes";
        }else{
            echo "<h2>$domain_name is helaas bezet</h2> <a href='/offerte-aanvragen/".$domain_name."/verhuizen/'>Verhuis uw domein</a>";
            $availability = "no";
        }

        mysql_query("INSERT domainnames SET dns='".$domain_name."', available='".$availability."', timestamp='".$date."'")or die(mysql_error());

    }
    ?>
</div>

答案 2 :(得分:0)

这可以通过稍微重构当前代码来完成:

<?php
    if(isset($_POST['proses'])){
        # code ...
    }
    else {
?>
        <div id="domain-name-check">
        <!-- your form code goes here -->
        </div>
<?php 
    } //end of else block 
?>

完整代码:

<?php
if(isset($_POST['proses']))
{
    $domain_name = "$_POST[domain]"."$_POST[suffix]";
    $arrHost = @gethostbynamel("$domain_name");
    $date = date('y-m-d');

    if(empty($arrHost)){
        echo "<h2>$domain_name is beschikbaar</h2> <a href='/offerte-aanvragen/".$domain_name."/'>Vraag een offerte aan</a>";
        echo "AVAILABLE";
        $availability = "yes";
    }else{
        echo "<h2>$domain_name is helaas bezet</h2> <a href='/offerte-aanvragen/".$domain_name."/verhuizen/'>Verhuis uw domein</a>";
        echo "NOT AVAILABLE";
        $availability = "no";
    }

    mysql_query("INSERT domainnames SET dns='".$domain_name."', available='".$availability."', timestamp='".$date."'")or die(mysql_error());
} 

else 
{
?>
    <div id="domain-name-check">
    <h2>Is uw domeinnaam nog vrij?</h2>
    <form method='post' class="clearfix">
    <input type=text name=domain>
    <select name="suffix">
        <option value=".nl">.nl</option>
        <option value=".be">.be</option>
        <option value=".com">.com</option>
        <option value=".net">.net</option>
        <option value=".org">.org</option>
        <option value=".biz">.biz</option>
        <option value=".info">.info</option>
        <option value=".mobi">.mobi</option>
        <option value=".ws">.ws</option>
        <option value=".cc">.cc</option>
    </select>
    <input type=submit name=proses value=Check>
    </form>
    </div>
<?php 
    }
?>

答案 3 :(得分:0)

在您的表单标记下,插入一个隐藏的表单字段,如下所示:

<form method='post' class="clearfix">
<input type="hidden" name="action" id="action" value="hideme" />
.................................
 .............................

</form>

现在在$ _POST部分下,使用以下逻辑:

 if (isset($_POST['action'])&& ($_POST['action']=='hideme'))
 {
  //perform your intended task on submission
}else {

//Show My form code again

}

简单!

答案 4 :(得分:0)

HTML代码可以用PHP包装,以有条件地显示它。

将以下内容视为此类功能的模板:

<?php if (condition) { ?>

    <p>HTML</p>

<?php } ?>

就这么简单。

在这种情况下,您的情况可能是

if (isset($_POST['proses'])

答案 5 :(得分:0)

替换<h2>Is uw domeinnaam nog vrij?</h2>
if (!isset($_POST)){
       echo '<h2>Is uw domeinnaam nog vrij?</h2>';
}

这将确保如果未提交表单,则用户可以看到h2 或者你可以使用Javascript

答案 6 :(得分:0)

使用以下内容:

<?php

if (  isset( $_POST['field_name'] )  )
  {
  .... // submit
  exit();

  }

?>

<html>
<form ... action="" method="POST">
....
....
</form>
</html>