简体   繁体   English

在不在Ajax的对象上下文中时使用$ this

[英]Using $this when not in object context in Ajax

getting this error 得到这个错误

Fatal error: Uncaught Error: Using $this when not in object context in E:\\xampp\\htdocs\\StudentGuideBook\\Version1.0\\models\\AjaxChecking.php:4 Stack trace: #0 {main} thrown in E:\\xampp\\htdocs\\StudentGuideBook\\Version1.0\\models\\AjaxChecking.php on line 4 致命错误:未捕获错误:在E:\\ xampp \\ htdocs \\ StudentGuideBook \\ Version1.0 \\ models \\ AjaxChecking.php:4不在对象上下文中时使用$ this:堆栈跟踪:#0 {main}抛出在E:\\ xampp \\第4行上的htdocs \\ StudentGuideBook \\ Version1.0 \\ models \\ AjaxChecking.php

 function checkAvailability() { jQuery.ajax({ url: "../models/AjaxChecking.php", data: 'adminName=' + $("#adminName").val(), type: "POST", success: function(data) { $("#admin-availability-status").html(data); }, error: function() {} }); } < /script> 
 AjaxChecking <?php require_once'DB_Connection.php'; $obj_db=$this->obj_db(); if (!empty($_POST["adminName"])) { $query="SELECT count(*) FROM admins WHERE adminName='" . $_POST["adminName"] ."'"; $result=$obj_db->query($query); $row=mysql_fetch_row($result); $adminCount=$row[0]; if ($adminCount > 0) { echo"<span class='status-not-available'> $adminName Admin Name is already in use.</span>"; } else { echo"<span class='status-available'> $adminName Admin Name is Available.</span>"; } } ?> DB_connection <?php abstract class DB_Connection { protected function obj_db() { $host="localhost"; $user="root"; $password=""; $database="studentguidebook"; $obj_db=new mysqli(); $obj_db->connect($host, $user, $password); if($obj_db->connect_errno) { throw new Exception(" * DB Conenct Error - $obj_db->connect_error -$obj_db->connect_errno"); } $obj_db->select_db($database); if($obj_db->errno) { throw new Exception(" * DB Select Error - $obj_db->error -$obj_db->errno"); } return $obj_db; } } ?> 
 <div class="form-group mb-n"> <label for="largeinput" class="col-sm-2 control-label label-input-lg">Admin name</label> <div class="col-sm-7"> <input type="text" class="form-control1 input-lg" id="adminName" name="adminName" onBlur="checkAvailability()" placeholder="Enter Username ie Mast Be Unique" value="<?php echo($obj_admin->adminName); ?>"> </div> <div class="col-sm-3"> <p class="help-block" id="admin-availability-status"> <?php if (isset($errors[ 'adminName'])) { echo($errors[ 'adminName']); } ?> </p> </div> </div> 

在DB_connectio.php中,替换受公共保护的对象。

The goal of the abstract class is that you extend that class like this: 抽象类的目标是像这样扩展该类:

class myApp extends DB_Connection {

Then in one of the methods of your class (maybe the constructor) you can reference: 然后,在您的类的一种方法(可能是构造函数)中,您可以引用:

    $obj_db=$this->obj_db();

So you should look into making classes and separate model and view. 因此,您应该研究制作类并将模型和视图分开。 Read about MVC (or similar patterns) to get going with that. 了解有关MVC(或类似模式)的信息。

Now if you just want to have a quick-and-dirty solution, edit the DB_Connection script and turn it into a plain function: 现在,如果您只想使用一种快捷的解决方案,请编辑DB_Connection脚本并将其变成简单的函数:

function obj_db() {
   // ...
}

... so without the class wrapper. ...因此没有类包装器。 And then in your main script, just call it like this: 然后在您的主脚本中,像这样调用它:

$obj_db=obj_db();

As said, this is quick-and-dirty. 如前所述,这很简单。 Over time, as your script will evolve you'll notice that there is a benefit in splitting up your code into modules, separating business logic from presentation and persistance. 随着时间的流逝,随着脚本的发展,您会注意到将代码拆分为模块,将业务逻辑与表示和持久性分开是有好处的。 Classes and modules can help you to manage this. 类和模块可以帮助您进行管理。 So then the first way is preferred. 因此,首选第一种方法。

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

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