简体   繁体   English

Vanilla JS+AJAX - 检查一个值是否已经存在

[英]Vanilla JS+AJAX - check whether a value already exists

I've got a form, which contains an input (a title for a post).我有一个表单,其中包含一个输入(帖子的标题)。 I need that input to be filled with a value that doesn't exiest already in my database, so what I'm trying to do is requesting an AJAX call, to check on the fly whether that post title is already created, activated by "onChange" event.我需要用我的数据库中不存在的值填充该输入,所以我要做的是请求 AJAX 调用,以动态检查该帖子标题是否已经创建,由“激活” onChange”事件。

Here is my Vanilla JS\AJAX code:这是我的香草 JS\AJAX 代码:

            function checkEx(url, cFunction) {
                var xhttp;
                xhttp=new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        cFunction(this);
                    }
                };
                xhttp.open("GET", url, true);
                xhttp.send();

            }

            function checkExNow(xhttp) {
                document.getElementById("pcp_ex").innerHTML = xhttp.responseText;
            }

And here is my HTML input:这是我的 HTML 输入:

<input onchange="checkEx('inc/ajax/pcp_check_ex_value.php', checkExNow)" name="pcp_name" type="text" placeholder="Post Title"> <div id="pcp_ex"></div>

Problem is, I have to find a way to send the input's value, in order to check it with my database on that PHP file.问题是,我必须找到一种发送输入值的方法,以便使用我的数据库在 PHP 文件上检查它。

Please, teach me how to do this using vanilla JS.请教我如何使用 vanilla JS 做到这一点。

Thank you in advance!先感谢您!

You need to also send to your function the current value of the input field您还需要将输入字段的当前值发送到您的 function

function checkEx(url, cFunction, valueToCheck) {
    var xhttp;
    xhttp=new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            cFunction(this);
        }
    };
    xhttp.open("GET", url+"?check="+valueToCheck, true);
    xhttp.send();
}

<input onchange="checkEx('inc/ajax/pcp_check_ex_value.php', checkExNow, this.value)" name="pcp_name" type="text" placeholder="Post Title"> <div id="pcp_ex"></div>

Here you could check a complete example. 在这里你可以检查一个完整的例子。

Thanks to your help, I managed.感谢您的帮助,我成功了。 For those who reached that post with similar issue, wondering how to do this, this is how I managed:对于那些遇到类似问题的人,想知道如何做到这一点,这就是我的管理方式:

JS: JS:

            function checkEx(url, cFunction, valueToCheck) {
                var xhttp;
                xhttp=new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        cFunction(this);
                    }
                };
                xhttp.open("GET", url+"?check="+valueToCheck, true);
                xhttp.send();
            }

            function checkExNow(xhttp) {

                document.getElementById("pcp_ex").innerHTML = xhttp.responseText;
            }

HTML: HTML:

 <input onchange="checkEx('inc/ajax/pcp_check_ex_value.php', checkExNow, this.value)" name="pcp_name" class="reg_input" type="text" placeholder="Post Title"> 
<span id="pcp_ex"></span>

PHP: PHP:

$exe = $_GET['check'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM