I am trying to check only selected domain extensions using check-box but it check all domain extensions existing in array.

here is what I am trying.

$name_domain = trim($_POST['domain_name']).$_POST['suffix'];
    $domains = array(
    ($name_domain) ? trim($_POST['domain_name']).$_POST['suffix'] : '',
    (isset($_POST['_org'])) ? trim($_POST['domain_name']).'.org' : trim($_POST['domain_name']).$_POST['_org'],
    (isset($_POST['_net'])) ? trim($_POST['domain_name']).'.net' : trim($_POST['domain_name']).$_POST['_net'], 
    (isset($_POST['_biz'])) ? trim($_POST['domain_name']).'.biz' : trim($_POST['domain_name']).$_POST['_biz'],
    (isset($_POST['_ws'])) ? trim($_POST['domain_name']).'.ws' : trim($_POST['domain_name']).$_POST['_ws'],
    (isset($_POST['_mobi'])) ? trim($_POST['domain_name']).'.mobi' : trim($_POST['domain_name']).$_POST['_mobi'],
    (isset($_POST['_info'])) ? trim($_POST['domain_name']).'.info' : trim($_POST['domain_name']).$_POST['_info'],
    );
    $msg='';
    foreach($domains as $d){
        $response = @dns_get_record($d, DNS_ALL);
        if(empty($response)){
                    $msg .= "<h2 style='color:green;' >Domain $d is available.</h2>";
        }else if(!empty($response)){
                    $msg .= "<h2 style='color:red;'>Domain $d has taken.</h2>";
        }

html

<td colspan="2"><input type="checkbox" name="_org" value=".org"> <label for="_org">.org</label></td>
<td colspan="2"><input type="checkbox" name="_net" value=".net"> <label for="_net">.net</label></td>
<td colspan="2"><input type="checkbox" name="_biz" value=".biz"> <label for="_biz">.biz</label></td>
<td colspan="2"><input type="checkbox" name="_ws" value=".ws"> <label for="_ws">.ws</label></td>
<td colspan="2"><input type="checkbox" name="_mobi" value=".mobi"> <label for="_mobi">.mobi</label></td>
<td colspan="2"><input type="checkbox" name="_info" value=".info"> <label for="_info">.info</label></td>

any idea how to only show and check only selected extension checking through checkboxes?

Recommended Answers

All 13 Replies

Hi,

you can try to generate an array from the checkboxes, change them like this:

<input type="checkbox" id="_org" name="tld[]" value=".org">
<label for="_org">.org</label>

Note that the id attribute of the input tag matches the label for attribute, that way the label becomes clickable, while the name attribute is the array: tld[].

The full example form is this:

<form method="post" action="">
    <input type="text" name="domain_name" />
    <table>
        <tr>
            <td colspan="2">
                <input type="checkbox" id="_org" name="tld[]" value=".org">
                <label for="_org">.org</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_net" name="tld[]" value=".net">
                <label for="_net">.net</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_biz" name="tld[]" value=".biz">
                <label for="_biz">.biz</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_ws" name="tld[]" value=".ws">
                <label for="_ws">.ws</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_mobi" name="tld[]" value=".mobi">
                <label for="_mobi">.mobi</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_info" name="tld[]" value=".info">
                <label for="_info">.info</label>
            </td>
        </tr>
    </table>
    <input type="submit" name="submit" value="submit" />
</form>

When sending a request it will look like this:

Array
(
    [domain_name] => test
    [tld] => Array
        (
            [0] => .org
            [1] => .net
            [2] => .info
        )

    [submit] => submit
)

Now it's easy to loop the top level domains (tld). In the script you have to use a white-list approach, to avoid arbitrary values, so the script becomes:

$domain_name = trim($_POST['domain_name']);
$responses = array();
$msg = '';

foreach($_POST['tld'] as $tld)
{
    switch($tld)
    {
        # white list
        case '.org':
        case '.net':
        case '.biz':
        case '.ws':
        case '.mobi':
        case '.info':

            $domain = $domain_name . $tld;
            $dns = dns_get_record($domain, DNS_ALL);

            # available
            if(count($dns) == 0) $msg .= "<h2 style='color:green;' >Domain $domain is available.</h2>";

            # error
            elseif($dns === false) $msg .= "<h2 style='color:green;' >Error on domain $domain.</h2>";

            # not available
            else
            {
                $msg .= "<h2 style='color:red;'>Domain $domain has taken.</h2>";
                $responses[$domain] = $dns;
            }

            break;
    }
}

echo $msg;

this is not working :(

This is my full code.

    if (!empty($_POST['domain_name']) && !empty($_POST['suffix'])){
        $domain_name = trim($_POST['domain_name']);
        $responses = array();
        $msg = '';

        foreach($_POST['tld'] as $tld)
        {
            switch($tld)
            {
                # white list
                case '.org':
                case '.net':
                case '.biz':
                case '.ws':
                case '.mobi':
                case '.info':

                    $domain = $domain_name . $tld;
                    $dns = @dns_get_record($domain, DNS_ALL);

                    # available
                    if(count($dns) == 0) $msg .= "<h2 style='color:green;' >Domain $domain is available.</h2>";

                    # error
                    elseif($dns === false) $msg .= "<h2 style='color:green;' >Error on domain $domain.</h2>";

                    # not available
                    else
                    {
                        $msg .= "<h2 style='color:red;'>Domain $domain has taken.</h2>";
                        $responses[$domain] = $dns;
                    }

                    break;
            }
        }
    }
    else if (empty($_POST['domain_name'])) {
        $msg = "<h2 style='color:red;'>Error: Domain name can not be left empty.</h2>";
    }

if (isset($_POST['domain_name']))echo $msg

I also have this markup

<select name="suffix" id="suffix">
<option value=".com">.com</option>
<option value=".in">.in</option>
<option value=".co.in">.co.in</option>
<option value=".net">.net</option>
<option value=".pk">.pk</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=".co.id">.co.id</option>
<option value=".or.id">.or.id</option>
<option value=".go.id">.go.id</option>
<option value=".sch.id">.sch.id</option>
<option value=".ac.id">.ac.id</option>
<option value=".mil.id">.mil.id</option>
<option value=".web.id">.web.id</option>
<option value=".tv">.tv</option>
<option value=".cn">.cn</option>
<option value=".cc">.cc</option>
</select>

this is my first aim, and checkboxes is for aditional search and its look like this.

Change your select tag with this:

<select name="tld[]" id="suffix">

Then to avoid duplicates between the selected value and the checkboxes, you can add array_unique() right before the loop:

$_POST['tld'] = array_unique($_POST['tld']);
foreach($_POST['tld'] as $tld)
{

Othwerwise, if you choose, for example, .info from both the input fields the script will receive:

Array
(
    [domain_name] => test
    [tld] => Array
        (
            [0] => .info <- duplicate
            [1] => .org
            [2] => .net
            [3] => .info <- duplicate
        )

    [submit] => submit
)

Bye!

did you check it?? its not returning at all.

no response at all.

Yep, it works fine for me. Change your IF statement:

if (!empty($_POST['domain_name']) && !empty($_POST['suffix'])){

To:

if (!empty($_POST['domain_name']) && !empty($_POST['tld'])){

yes I did, but same, its not echoing the results but the array results showed.

in this live example the code seems work fine, but the select options value is not showing results from the drop down.

Thanks for your help, I figured it out myself. :)

I really appreciated your help and I am happy.

If I were you I will be do the same as you did. :) :D

One last question I want to ask, If I want to show the prices of the domains next to their results, like if the .com is $9.99 and .mobi is $5.99 how can I show these values in the anchor tag really next to their values only if the domain is available, other wise show a link of that domain which has been taken?

and many thanks in advance. :)

I'm glad it works fine. You can return the price list as an index => value array, for example:

$prices = array(
    '.com'  => '9.99',
    '.info' => '5.99',
    ...
);

so you can match them into the loop:

foreach($_POST['tld'] as $tld)
{
    $price = money_format('%n', $prices[$tld]);

Note, here I'm using the money_format() function, this requires setlocale(LC_MONETARY, 'en_US.UTF-8'); on the top of the script:

The full script now look like this:

<?php

    setlocale(LC_MONETARY, 'en_US.UTF-8');

    if (!empty($_POST['domain_name']) && !empty($_POST['tld'])){

        # get price list
        $prices = require './generate_prices.php';

        $domain_name = filter_var(trim($_POST['domain_name']), FILTER_SANITIZE_STRING, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $responses = array();
        $msg = '';

        foreach($_POST['tld'] as $tld)
        {
            # format the price
            $price  = money_format('%n', $prices[$tld]);
            $url    = filter_var('http://'.$domain_name, FILTER_VALIDATE_URL);

            # stop the loop
            if($url === false)
            {
                $msg .= '<h2>Domain is not valid</h2>';
                break;
            }

            switch($tld)
            {
                # white list
                case '.ac.id':
                case '.biz':
                case '.cc':
                case '.cn':
                case '.co.id':
                case '.co.in':
                case '.com':
                case '.go.id':
                case '.in':
                case '.info':
                case '.mil.id':
                case '.mobi':
                case '.net':
                case '.or.id':
                case '.org':
                case '.pk':
                case '.sch.id':
                case '.tv':
                case '.web.id':
                case '.ws':

                    $domain = $domain_name . $tld;
                    $dns = @dns_get_record($domain, DNS_ALL);

                    # available
                    if(count($dns) == 0) $msg .= "<h2 style='color:green;' >Domain $domain is available for: $price</h2>";

                    # error
                    elseif($dns === false) $msg .= "<h2 style='color:green;' >Error on domain $domain.</h2>";

                    # not available
                    else
                    {
                        $msg .= "<h2 style='color:red;'>Domain <a href='$url'>$domain</a> has taken.</h2>";
                        $responses[$domain] = $dns;
                    }

                    break;
            }
        }
    }
    else if (empty($_POST['domain_name'])) {
        $msg = "<h2 style='color:red;'>Error: Domain name can not be left empty.</h2>";
    }

if (isset($_POST['domain_name'])) echo $msg;


?>

<form method="post" action="">
    <input type="text" name="domain_name" />
    <select name="tld[]" id="suffix">
        <option value=".com">.com</option>
        <option value=".in">.in</option>
        <option value=".co.in">.co.in</option>
        <option value=".net">.net</option>
        <option value=".pk">.pk</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=".co.id">.co.id</option>
        <option value=".or.id">.or.id</option>
        <option value=".go.id">.go.id</option>
        <option value=".sch.id">.sch.id</option>
        <option value=".ac.id">.ac.id</option>
        <option value=".mil.id">.mil.id</option>
        <option value=".web.id">.web.id</option>
        <option value=".tv">.tv</option>
        <option value=".cn">.cn</option>
        <option value=".cc">.cc</option>
    </select>
    <table>
        <tr>
            <td colspan="2">
                <input type="checkbox" id="_org" name="tld[]" value=".org">
                <label for="_org">.org</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_net" name="tld[]" value=".net">
                <label for="_net">.net</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_biz" name="tld[]" value=".biz">
                <label for="_biz">.biz</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_ws" name="tld[]" value=".ws">
                <label for="_ws">.ws</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_mobi" name="tld[]" value=".mobi">
                <label for="_mobi">.mobi</label>
            </td>
            <td colspan="2">
                <input type="checkbox" id="_info" name="tld[]" value=".info">
                <label for="_info">.info</label>
            </td>
        </tr>
    </table>
    <input type="submit" name="submit" value="submit" />
</form>

On line 8 at the moment there is a simple script to returns random prices:

$prices = require './generate_prices.php';

It consist in this:

<?php

$domains = array(
    '.ac.id',
    '.biz',
    '.cc',
    '.cn',
    '.co.id',
    '.co.in',
    '.com',
    '.go.id',
    '.in',
    '.info',
    '.mil.id',
    '.mobi',
    '.net',
    '.or.id',
    '.org',
    '.pk',
    '.sch.id',
    '.tv',
    '.web.id',
    '.ws'
);

$price_list = array();

foreach($domains as $domain)
{
    $price = mt_rand(3,11);
    $price_list[$domain] = $price . '.99';
}

return $price_list;

In your case this can be a static array:

return array(
    '.ac.id' => '5.99',
    '.biz'   => '4.99',
    '.cc'    => '5.99',
    ...
);

Or the result of a query to the database, it's not required the use of this external script, it's just for the example.

For the links just add the tag to the $msg variable:

$msg .= "<h2 style='color:red;'>Domain <a href='http://$domain'>$domain</a> has taken.</h2>";

Remember to sanitize the script, in the example I'm using filter_var:

To sanitize $_POST['domain_name'] and, after, to verify if the domain is a valid url.

Here's the live example:

Thanks for the help, I am very glad that you helped me to figured it out.

but this problem I figured out myself, :) never mind.

this one is I think is really good thing, but I tested it has somethings not corect,

like when search for the domain if the domain is taken the domain url is not correct because you placed $url = filter_var('http://'.$domain_name, FILTER_VALIDATE_URL); and its not returning correct url,
it should be $url = filter_var('http://'.$domain_name.$tld, FILTER_VALIDATE_URL);

or $msg .= "<h2 style='color:red;'>Domain <a href='http://$domain'>$domain</a> has taken.</h2>"; as you mentioned in your answer.

and for every domain price is not fixed its randomally changed, because you used $price = mt_rand(3,11); so this is not correct way.

well I figgured it out myself and thanks for thelp again :)

What I tried is something like this

if (!empty($_POST['domain_name']) && !empty($_POST['tld'])){

        $domain_name = trim($_POST['domain_name']);
        $responses = array();
        $msg = '';
        $_POST['tld'] = array_unique($_POST['tld']);

        foreach($_POST['tld'] as $tld) {
            switch($tld) {
                # white list
                case '.com':
                case '.org':
                case '.net':
                case '.biz':
                case '.info':
                case '.mobi':
                case '.name':
                case '.ws':
                case '.in':
                case '.tv':
                case '.cc':
                case '.ru':
                case '.me':


                    $domain = $domain_name . $tld;
                    $dns = @dns_get_record($domain, DNS_ALL);

                    # available
                    if(count($dns) == 0) {
                        $prices = array(
                            '.com' => '$9.95',
                            '.org' => '$9.95',
                            '.net' => '$9.95',
                            '.biz' => '$9.95',
                            '.info' => '$9.95',
                            '.mobi' => '$22.00',
                            '.name' => '$11.95',
                            '.ws' => '$20.00',
                            '.in' => '$9.99',
                            '.tv' => '$35.00',
                            '.cc' => '$30.00',
                            '.ru' => '$4.95',
                            '.me' => '$22.95',
                        );
                            switch($tld) {
                                case '.com':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.com']."</a></h2>";
                                break;
                                case '.org':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.org']."</a></h2>";
                                break;
                                case '.net':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.net']."</a></h2>";
                                break;
                                case '.biz':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.biz']."</a></h2>";
                                break;
                                case '.info':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.info']."</a></h2>";
                                break;
                                case '.mobi':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.mobi']."</a></h2>";
                                break;
                                case '.name':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.name']."</a></h2>";
                                break;
                                case '.ws':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.ws']."</a></h2>";
                                break;
                                case '.in':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.in']."</a></h2>";
                                break;
                                case '.tv':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.tv']."</a></h2>";
                                break;
                                case '.cc':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.cc']."</a></h2>";
                                break;
                                case '.ru':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.ru']."</a></h2>";
                                break;
                                case '.me':
                                    $msg .= "<h2 style='color:green;' >Domain $domain is available. <a href='#'>".$prices['.me']."</a></h2>";
                                break;
                            }

                    }# error
                    elseif($dns === false) {
                        $msg .= "<h2 style='color:green;' >Error on domain $domain.</h2>";
                    }# not available
                    else {
                        $msg .= "<h2 style='color:red;'>Domain $domain has taken. <a href='http://www.$domain' target='_blank'>View Website</a></h2>";
                        $responses[$domain] = $dns;
                    }

                    break;
            }
        }
    }
    else if (empty($_POST['domain_name'])) {
        $msg = "<h2 style='color:red;'>Error: Domain name can not be left empty.</h2>";
    }

and it works perfect :)

and you can also test it yourself. it will give you the correct value for the same domain no matter how much you run it again.

and the link is also works fine. :)

$url = filter_var('http://'.$domain_name, FILTER_VALIDATE_URL); and its not returning correct url, it should be $url = filter_var('http://'.$domain_name.$tld, FILTER_VALIDATE_URL);

Yes, you're correct, this morning I was a bit in a hurry but I fixed this in the live example.

and for every domain price is not fixed its randomally changed, because you used $price = mt_rand(3,11); so this is not correct way.

The randomness was just for the example, I agree it's not correct for production :) I tend to generate random data for my test scripts to be sure I'm not pulling data from a cache.

Thanks for the help, I am very glad that you helped me to figured it out. but this problem I figured out myself, :) never mind.

No problem, bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.