【问题标题】:Grab form data in a php class and not just in php file在 php 类中获取表单数据,而不仅仅是在 php 文件中
【发布时间】: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 等语句

标签: php jquery ajax oop


【解决方案1】:
<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

$(function(){
    $("#registration-form").submit(function(){
        var name = $("#name").val();
        var email = $("#email").val();
        var data = {"name":name, "email":email};
        $.ajax({
            type: "POST",
            url: "http://localhost/mySite/controllers/process-registration.php",
            data: data,
            dataType: "json",
            success:  function(data){
                if(data.status == "OK"){
                    alert(data.message)//it will give you the response message from server
                    alert("User registered successfully.");
                    window.location.reload(true);
                }else{
                    //if error
                    alert("Error - "+data.message);
                }
            },
            error: function(data){
                alert("Internal server error");
            }
        }); 
    });    
});

php代码(process-registration.php)

<?php 
require_once("../models/registrationModel.php")
class Process-registration(){
    function addUser(){
       //Where should I grab these values? "Thats upto you"
       $name = $_POST["name"];
       $email = $_POST["email"];
       $response = array("status"=>"OK","message"=>"Got name($name) and email($email)");
       return json_encode($response);
    }
}

$process-registration=new Process-registration();
echo $process-registration->addUser();
?>

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2011-07-23
    • 2020-06-28
    相关资源
    最近更新 更多