我如何在执行javasript函数之前保存php值

时间:2019-06-26 12:01:29

标签: javascript php html

我一直在尝试制作wordpress插件,以检查域的可用性。我一直试图按提交按钮,然后保存api调用GET中的值,然后用javascript检查域是否可用,但问题是我的javascript函数先运行,然后保存了api调用GET中的值,因此,检查器会在当前输入之前检查输入的值。我该如何解决?

   <div>
    <form action="" method="post">
        <input id="availableyn" name="action" type="hidden" value="' . api_connection() . '"/>
    www.
        <input name="domain" size="30" type="text" value=""/>
                <select name="tld">
                    <option value=".com">.com</option>
                    <option value=".net">.net</option>
                    <option value=".org">.org</option>
                    <option value=".info">.info</option>
                    <option value=".nl">.nl</option>
                    <option value=".be">.be</option>
                    <option value=".de">.de</option>
                </select>
        <input name="whoissubmit" type="submit" value="Check" onclick="check_domain_availability();"/>
        <label id="label_result" content=""></label>
  </form>
</div>
function api_connection(){

    if($_POST["whoissubmit"] == true) {
// Set username and password
        $username = 'something';
        $password = 'something';
// Get cURL resource
        $curl = curl_init();
// Set some options - we are passing in a useragent too here
// This time we only want to get the categories
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_URL => 'https://www.versio.nl/testapi/v1/domains/' . $_POST['domain'] . $_POST['tld'] . '/availability',
            CURLOPT_HTTPAUTH => CURLAUTH_ANY,
            CURLOPT_USERPWD => "$username:$password"
        ));
// Send the request & save response to $resp
        $resp = (string)curl_exec($curl);
// Get status code of the response
        $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Close request to clear up some resources
        curl_close($curl);
// Do something with the response (if you echo it, you will see it's in JSON format) and the status code
        $result = json_decode($resp, true);
    }
    // print debug info
    echo $resp;
    return $result['available'];
}
function check_domain_availability(){

    var val = document.getElementById("availableyn").value;

    console.log(val);

    if(val == true){
        document.getElementById("label_result").innerHTML = "Het domein is beschibaar";
    }
    else{
        document.getElementById("label_result").innerHTML = "Het domein is niet beschibaar";
    }

}

0 个答案:

没有答案