4

我有一个放在容器中的域名检查表。在我提交表单后,会检查域名的可用性,并在与表单相同的容器中显示一条消息。显示消息时,我希望表单和第一个 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>
4

7 回答 7

8

更正的代码:仅当我们没有提交表单值时才显示表单。因此,在标签中编写您的 HTML 代码,else如下所示:

<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>
于 2013-09-26T15:21:20.473 回答
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>
于 2013-09-26T15:19:49.950 回答
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 
    }
?>
于 2013-09-26T15:23:08.713 回答
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

}

简单的!

于 2013-09-26T15:30:08.297 回答
0

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

考虑以下作为此类功能的模板:

<?php if (condition) { ?>

    <p>HTML</p>

<?php } ?>

就是这么简单。

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

if (isset($_POST['proses'])
于 2013-09-26T15:31:21.557 回答
0

替换<h2>Is uw domeinnaam nog vrij?</h2>

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

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

于 2013-09-26T15:33:15.357 回答
0

使用以下内容:

<?php

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

  }

?>

<html>
<form ... action="" method="POST">
....
....
</form>
</html>
于 2015-07-29T05:18:50.950 回答