【发布时间】:2015-04-22 10:24:40
【问题描述】:
我有一个带有表单的 html 文件。我想通过 jQuery AJAX 将表单数据发送到另一个 php 文件(process-registration.php)中的 php 类中的函数。我的问题是 i) 如何在 AJAX 请求中设置 url 变量?我应该在 php 类中包含处理请求的函数名称吗? ii) 如何在 php 类函数中接收通过 AJAx 发送的表单数据? 这是html代码
<form id = "registration form">
<input type = "text" id = "name" placeholder = "Name" />
<input type = "text" id = "email" placeholder = "Email" />
<input type = "submit" id = "register" value "Register" />
</form>
//Jquery Ajax
var name = $("#name").val();
var email = $("#email").val();
var datastring = 'name='+name+'&mail='+email;
$.ajax({
//Should I add the function name to the url to look like url: "http://localhost/mySite/controllers/process-registration.php/addUser()"
type: "POST",
url: "http://localhost/mySite/controllers/process-registration.php",
data: datastring,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert("User registered successfully.");
window.location.reload(true);
}
});
//php code (process-registration.php)
<?php
require_once("../models/registrationModel.php")
class Process-registration(){
function addUser(){
//Where should I grab these values? Within the class? Outside the class? Within the function?
$name = $_POST["name"];
$email = $_POST["email"];
}
}
?>
【问题讨论】:
-
Krishna,该帖子只是在 php 文件中接收数据,但在我的问题中,我希望表单数据在 php 类中接收并由该类中的函数处理
-
所以你要做的是,在ajax调用的那个文件中,调用php类并使用那个类中的函数
-
我的问题是如何做到这一点,克里希纳
-
请谷歌一下,你可以很容易地得到它。查看this 帖子了解如何操作。您可以使用 include、include_once、require 或 require_once 等语句