.PHP文件中的AJAX [英] AJAX in .PHP files

查看:34
本文介绍了.PHP文件中的AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用该网站的第一个问题,所以如果我没有遵守本书中的所有规则,请与我联系.

First question using the site so please bear with me if I haven't followed every rule in the book.

我来自C ++背景,对php/AJAX没有太多的经验,所以我知道我可能已经以次优/不正确的方式处理了以下一些编码任务,以不同的方式编写代码语言,但无论如何...

I come from a C++ background and don't have a great deal of experience with php/AJAX so I know that I probably have approached some of the following coding tasks in a sub-optimal/ improper way for writing code in different languages but anyway...

我有一个网站,该网站使用以PHP编写的成员登录系统(连接到mysql数据库),并且该网站使用.php文件编写,以适应该登录系统.

I have a Web site which uses a member login system written in PHP (connected to a mysql database), and the site is written using .php files to accomodate for this login system.

我想在我的.php页面上使用AJAX和JS来使它们具有更好的用户体验,并且我知道这样做是可行的(因为我这样做了),但是我想知道是否有任何负面/技术原因为什么我不应该这样做(以及是否有更好的方法来做到这一点),因为php是服务器端,而AJAX是客户端.

I want to use AJAX and JS on my .php pages to make them have a better user experience and I know this is possible (as I have done it), but I wanted to know if there are any negative/technical reasons why I shouldn't (and whether there are any better ways of doing this) as php is server side and AJAX is Client side.

任何建议将不胜感激.

谢谢

编辑我添加了一些代码来显示我想添加到我的php网站中的东西的类型

EDIT I've added some code to show the type of things I would like to add to my php site

<?php
require "class.loginsys.php";
$LS = new LoginSystem();
$LS->init();
?>

<!-- HTML page structure -->

<!DOCTYPE html>
<html>
    <head>
        <title>OnyxProjectsPage</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
        <script type = "text/javascript">
        function createTable()
        {
            var xhr;
            if (window.XMLHttpRequest) // Mozilla, Safari, ...
            { 
                xhr = new XMLHttpRequest(); 
            }   
            else if (window.ActiveXObject) // IE 8 and older
            { 
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }   

            xhr.open("GET", "createDatabase.php"); 
            //xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
            xhr.send();

            xhr.onreadystatechange = display_data;
            function display_data() 
            {
                if (xhr.readyState == 4) 
                {
                    if (xhr.status == 200) 
                    {  
                        alert("Table Created");
                    } 
                    else 
                    {
                        alert('There was a problem with the request.');
                    }
                }
            }
        }
        </script>

    </head>

    <body>
        <!-- Header background bar -->
        <div id="container" style="width: 1920px">
        <div id="header" style="background-color:#4c4c4c;">

        <form class="well-home span6 form-horizontal" name="ajax-demo" id="ajax-demo">
            <div class="controls">
                <button type="button" onclick="createTable()">Create Testplan</button>

            </div>
        </form>
    </body>
</html>

推荐答案

使用 AJAX / JavaScript 并不比常规的 PHP 危险.您可以说,人们可以禁用JavaScript,因此无法执行预期的结果.

Using AJAX / JavaScript is not more dangerous than regular PHP. You can argue, that people can disable JavaScript and thus not be able to perform your expected result.

通常,使用AJAX可以满足用户体验,因为用户不必在每次发送请求时都重新加载页面.

Usually, using AJAX will, as you mentioned, satisfy the user-experience, since they don't have to reload the page everytime a request is send.

我认为最好的解决方案是:

The best solution, in my opinion, would be:

检查用户是否在浏览器中启用了Javascript(关键字: noscript ).如果是这样,您可以使用 jQuery 之类的框架.使用此功能,您可以利用内置的ajax函数(请在此处)查看).否则,请准备一个后备/故障安全模式,以为每个访客提供服务.

Check if the User enabled Javascript in his browser (keyword: noscript). If so, you can do use Frameworks like jQuery. Using this you can take advantage of the build-in ajax-function (take a look here). Otherwise prepare a fallback/failsafe mode for to serve every visitor.

LT; DR

将两者都混合.无论如何,即使已经在客户端上进行了检查,也要先在服务器端进行检查并验证,然后再将数据插入数据库(或与之相关的所有内容).

Mix both of them. In any case, check and validate on serverside before inserting data in a database (or everything related to that kind of stuff), even if you checked it on the clientside already.

这篇关于.PHP文件中的AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆