繁体   English   中英

如何使用Ajax帖子调用PHP文件

[英]How to call a php file with ajax post

//文件inserirPF.js

function add(){  
   var metodo = 'AdicionaPessoa';
   $.ajax({
     url: "../class/dao/InserirPFDao.class.php",
     type: 'POST',
     data: {Metodo:metodo, NOME_:NOME, EMAIL_:EMAIL, SENHA_:SENHA},
     success: function(data) {
        alert(data); 
     }
   });
 }

//文件InserirPFDao.class.php

<?php 
require_once("../class/dao/connection.class.php");  

class InserirPFDao extends ConexaoMySQl
{

    function __construct(){
        parent::Connection();
        if (isset($_POST['Metodo'])) {
        switch ($_POST('Metodo')) {
            case 'AdicionaPessoa':
                $retorno = AdicionaPessoa();
                mysql_close($db);
                echo $retorno;
                break;
            default:
                echo "Nenhum metodo encontrado!"
                break;
            }
        }
    }

    public function AdicionaPessoa(){
        try {
            $NOME_ = $_POST("NOME_");
            $EMAIL_=$_POST("EMAIL_");
            $SENHA_=$_POST("SENHA_");
            $Adiciona = mysql_query("INSERT INTO PESSOA (NOME, EMAIL, SENHA)VALUES('".$NOME_."','".$EMAIL_."','".$SENHA_."')");
            if($Adiciona) 
                return true;
            else
                return false; 

        } 
        catch (Exception $e) {

        }           
    }
 }
 ?>

如何调用jQuery arquivoEstou,以尝试使用ajax在Javascript和php之间建立连接。 如果有人知道另一种方法...向我显示此错误:

<font size='1'>
<table class='xdebug-error xe-parse-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr>
    <th align='left' bgcolor='#f57900' colspan="5">
    <span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> 
    Parse error: syntax error, unexpected T_PUBLIC in C:\Arquivos de programas\VertrigoServ\www\gofinder\class\dao\InserirPFDao.class.php on line 
    <i>7</i>
    </th>
    </tr>
</table>

$retorno = AdicionaPessoa(); 看起来不正确。 我猜你的意思是:

$retorno = $this->AdicionaPessoa();

就像注释中指出的那样,您正在创建一个类,但不对其进行任何操作。 您需要实例化该类。 将以下内容添加到文件末尾:

$inserirPFDao = new InserirPFDao();

您可能需要阅读一些基本的面向对象编程教程,以帮助您入门。 您看起来好像在混合OO和过程代码,却不了解它们之间的差异。

阅读以下内容: http : //net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/

暂无
暂无

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

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