简体   繁体   English

如何将表格从Angular发送到PHP?

[英]How do I send the form from Angular to PHP?

I'm trying to send my form from angular 5 to my php file, and re-inject the result back into angular 我正在尝试将我的表单从angular 5发送到我的php文件,然后将结果重新注入angular

/* My typescrit file */ import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core'; import { trigger , state, style, transition, animate, keyframes } from '@angular/animations'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Headers, Response, RequestOptions } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import { MatButtonModule, MatCardModule, MatMenuModule, MatToolbarModule, MatIconModule, MatCheckboxModule, MatRadioModule, MatSelectModule, MatInputModule, MatDialogModule, MatTooltipModule, MatSnackBarModule, MatSidenavModule } from '@angular/material'; import { ReactiveFormsModule, FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms'; constructor(public http: HttpClient) {} let formdata = document.querySelector('form'); let datajson= { action: 'login', username: formdata.username.value, useremail: formdata.useremail.value, usersubject: formdata.usersubject.value, usermessage: formdata.usermessage.value }; let apiurl = '../../assets/php/message.php'; this.http.post(apiurl,datajson) .subscribe(data => {alert(data.data)}) .catch(error=>{ alert (error.status)}); </pre>

But I'm getting "Http undefined error". 但是我收到“ Http undefined error”。

You can achieve this through sending get or post request from angular controller to php page. 您可以通过将获取或发布请求从角度控制器发送到php页面来实现。 Sample code should like this(it's from my site): 示例代码应如下所示(来自我的网站):

app.controller('sign_up', function ($scope, $http) {
    /*
    * This method will be called on click event of button.
    * Here we will read the email and password value and call our PHP file.
    */
    $scope.check_credentials = function () {
    document.getElementById("message").textContent = "";
    var request = $http({
        method: "post",
        url: window.location.href + "login.php",
        data: {
            email: $scope.email,
            pass: $scope.password
        },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });

    /* Check whether the HTTP Request is successful or not. */
    request.success(function (data) {
        document.getElementById("message").textContent = "You have login successfully with email "+data;
    });
}

http is undefined because you are trying to use it without defining it in your class. http是未定义的,因为您尝试使用它而不在类中定义它。

I'm guessing this file your showing is from your app.module.ts ? 我猜您显示的文件来自您的app.module.ts吗? If so import the HttpClientModule there. 如果是这样,则在其中导入HttpClientModule

import { HttpClientModule } from '@angular/common/http';
...
imports: [
// import HttpClientModule after BrowserModule.
HttpClientModule,
],

Go to your app.component.ts and do your request there. 转到您的app.component.ts并在那里进行请求。 Or in any component you want it to be done. 或在您希望完成的任何组件中。

import { Component, OnInit, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
 datajson= {
      action: 'login', 
      username: formdata.username.value, 
      useremail: formdata.useremail.value,
      usersubject: formdata.usersubject.value,
      usermessage: formdata.usermessage.value
    };
apiurl = '../../assets/php/message.php';


 constructor(private http: HttpClient) {
  this.http.post(apiurl, datajson).subscribe(data => {alert(data.data)}
  .catch(error=> {alert(error)}
 }
}

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

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