AJAX调用在JavaScript中运行PHP脚本

时间:2021-11-01 04:51:16

I am trying to run a PHP script as soon as Submit button is clicked. I wanted to do an AJAX call as i dint want to refresh the page after the button is clicked. But the below code is not responding upon click.

我在点击“提交”按钮时尝试运行PHP脚本。我想做一个AJAX调用,因为我想点击按钮后刷新页面。但是下面的代码没有响应点击。

I have saved the login.php in the same location as my project. Any help or input is appreciated!

我已将login.php保存在与项目相同的位置。任何帮助或输入表示赞赏!

<input type="button" class="button" value="submit" />
<script type="text/javascript">
$(document).ready(function() {
    $("button").click(function() {
        $.ajax({
            type: "GET",
            url: "C:\wamp\www\ElitePass\login.php"
        })
        alert("done successully")
    });
});
</script>

2 个解决方案

#1


2  

The issue is that you are not targeting your button correctly.

问题是您没有正确定位按钮。

$("button") this selector is looking for an element of type "button". Naturally there is none. To target elements by their class names, append dot to selector:

$(“按钮”)此选择器正在查找“按钮”类型的元素。当然没有。要按类名称来定位元素,请将点附加到选择器:

$(".button").click(....)

Having said that, your code will still not work as you expect it to. Browser security restrictions will prevent loading of files directly from your file system. You need to load stuff via web server. Especially PHP files.

话虽如此,您的代码仍然无法按预期工作。浏览器安全限制将阻止直接从文件系统加载文件。您需要通过Web服务器加载东西。特别是PHP文件。

#2


0  

You can call success like :

你可以称之为成功:

<script type="text/javascript">
$(document).ready(function() {
    $("button").click(function() {
        $.ajax({
            type: "GET",
            url: "C:\wamp\www\ElitePass\login.php",
            success: function() {
                alert("done successully");
            }
        })

    });
});
</script>

#1


2  

The issue is that you are not targeting your button correctly.

问题是您没有正确定位按钮。

$("button") this selector is looking for an element of type "button". Naturally there is none. To target elements by their class names, append dot to selector:

$(“按钮”)此选择器正在查找“按钮”类型的元素。当然没有。要按类名称来定位元素,请将点附加到选择器:

$(".button").click(....)

Having said that, your code will still not work as you expect it to. Browser security restrictions will prevent loading of files directly from your file system. You need to load stuff via web server. Especially PHP files.

话虽如此,您的代码仍然无法按预期工作。浏览器安全限制将阻止直接从文件系统加载文件。您需要通过Web服务器加载东西。特别是PHP文件。

#2


0  

You can call success like :

你可以称之为成功:

<script type="text/javascript">
$(document).ready(function() {
    $("button").click(function() {
        $.ajax({
            type: "GET",
            url: "C:\wamp\www\ElitePass\login.php",
            success: function() {
                alert("done successully");
            }
        })

    });
});
</script>