简体   繁体   English

从具有开关的php函数中调用ajax($ this-> method)

[英]calling ajax from within a php function having a switch ($this->method)

when creating an XMLrequest in a php file having a code which goes something like this... I am using a MVC ( model-view-controller structure ) and this is a controller php file.. 当在具有这样的代码的php文件中创建XMLrequest时...我正在使用MVC(模型-视图-控制器结构),这是一个控制器php文件。

Controller_Institute extends Controller_Default{



function register(){
    try {

        $this->requireLogin();

        switch($this->method){
            case 'GET':
                $content = $this->render('institute_registration_confirm');
                break;
                case 'POST':

$result = mysql_query("SELECT * FROM password WHERE         pass='".mysql_real_escape_string($_POST['pass'])."'");
$num=mysql_num_rows($result);
if($num==2)
{
$content = $this->render('institute_registration');
}
else
{                   
                $content = $this-  >render("message",array('msg'=>'Your password is incorrect'));

}
break;
}                   
                $institute = R::dispense('institute');
                $institute- >import($_POST,'name,latitude,state,longitude,address,phone,year,url');
                $id = R::store($institute);

                }
                catch(exception $e){
        //If there was an error anywhere, go to the error page.
        $content = $this->render('error',array('exception'=>$e));   
    }
            $page = $this->render('default',array('content'=>$content));
            return $page;

}

i am sending the ajax request from within the function ... so when the ajax sends back the request , it gets caught in the switch case... and then the response text becomes the function return value replacing the actual text... any idea how to prevent the xml response from getting into the switch case...? 我正在从函数内发送ajax请求 ...所以当ajax发送回请求时,它陷入了开关情况...然后响应文本成为函数返回值替换了实际文本...任何想法如何防止xml响应进入切换情况...? the institute_registration is the view file and i am including that file in my framework and then triggering the ajax function from within that file to check whether the password ( to enable registration form ) is correct or not... institute_registration是视图文件,我正在将该文件包含在我的框架中,然后从该文件中触发ajax函数以检查密码(启用注册表单)是否正确...

Given the limited information and pseudo-code, I recommend setting up a stand-alone page called say... "ajax.php" that is stand alone and doesn't base it's return value on the request method. 考虑到有限的信息和伪代码,我建议设置一个独立的页面,例如say ...“ ajax.php”,该页面是独立的,并且不会基于请求方法返回值。 The pages that use AJAX will need to either POST or GET from this page depending. 使用AJAX的页面将需要从该页面进行POST或GET。

If you determine whether or not regular output vs AJAX output is returned via request method, then you are limiting yourself in 2 ways. 如果确定是否通过请求方法返回常规输出与AJAX输出,那么您将通过两种方式限制自己。 The first is you will not be able to do 1 or the other on your web pages (GET vs POST) instead of both. 第一个是您将无法在网页上执行一项或两项(GET与POST),而不能同时执行两项。 Also, the second, when it comes to the AJAX, you will not be able to run GET & POST AJAX requests, and yes, you can do both with AJAX: http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/ 另外,第二,涉及到AJAX,您将无法运行GET&POST AJAX请求,是的,您可以同时使用AJAX: http : //net.tutsplus.com/tutorials/javascript-ajax / 5方式使带有jquery的ajax调用/

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

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