【问题标题】:PHP, React, MySQL database contact form errorPHP、React、MySQL 数据库联系表单错误
【发布时间】:2020-09-12 12:41:20
【问题描述】:

我正在使用 react 和 php mysql 制作一个简单的登录页面。我想将查询发布到 php 数据库中。我能够在前端控制台中显示响应对象;我在尝试将联系表单信息提交到数据库时遇到错误。我的前端是使用 react 编写的,当前控制台记录了我想发布到我的 MySQL 数据库中的状态对象。我的数据库列是:

前端代码:

这是我在控制台记录结果的地方;我的网络出现错误提示“无法加载响应数据”

import React, { Component } from "react";
import axios from "axios";

const url = "https://localhost:8080/react-php/insert.php";

class Form extends Component {
  constructor() {
    super();
    this.state = {
      name: "",
      email: "",
      message: "",
      formError: false,
      error: null
    };
  }

  handleFormSubmit = e => {
    e.preventDefault();

    if (
      this.state.name === "" ||
      this.state.email === "" ||
      this.state.message === ""
    ) {
      this.setState({
        formError: true
      });
      return false;
    } else {
      this.setState({
        formError: false
      });
      const obj = {
        name: this.state.name,
        email: this.state.email,
        message: this.state.message
      };
      console.log(obj);
      axios
        .post(url, obj)
        .then(res => console.log(res.data))
        .catch(error => this.setState({ error: error.message }));

      this.setState({
        name: "",
        email: "",
        message: ""
      });
    }
  };

  // Functions to  get the input values
  getName = e => {
    let name = e.target.value;
    this.setState({
      name: name
    });
    console.log(this.state.name);
  };

  getEmail = e => {
    let email = e.target.value;
    // eslint-disable-next-line no-useless-escape
    if (
      email.match(
        /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
      )
    ) {
      this.setState({
        email: email
      });
    } else {
      this.setState({
        email: ""
      });
      console.log("Incorrect e-mail, must match expression");
    }

    console.log(this.state.email);
  };

  getMessage = e => {
    let message = e.target.value;
    this.setState({
      message: message
    });
    console.log(this.state.message);
  };

  render() {
    return (
      <form id="contact">
        {/* I am just sending a basic error message */}
        {this.state.formError && (
          <p className="error">Fill all the input fields please.</p>
        )}
        {/* <p>Fill in the next form to send us a message</p> */}
        <div>
          <label htmlFor="name">Name</label>
          <input
            type="text"
            name="name"
            placeholder=""
            onChange={this.getName}
            value={this.state.name}
          />
        </div>
        <div>
          <label htmlFor="email">Email</label>
          <input
            type="email"
            name="email"
            placeholder=""
            onChange={this.getEmail}
            value={this.state.email}
          />
        </div>
        <div>
          <label htmlFor="name">Message</label>
          <textarea
            onChange={this.getMessage}
            maxLength="450"
            value={this.state.message}
          ></textarea>
        </div>
        <div>
          <p>We will answer as soon as possible</p>
          <input
            type="submit"
            name="submit"
            value="Send"
            onClick={this.handleFormSubmit}
          />
        </div>
      </form>
    );
  }
}

export default Form;

后端代码: 我已经在端口 8080 上导入了连接到我的本地主机的连接。

<?php
require 'connect.php';


header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
$postdata = file_get_contents("php://input");

if(isset($postdata) && !empty($postdata)){

    $request = json_decode($postdata);


    $name=$request->name;
    $email=$request->email;
    $message=$request->message;

}

$query = mysqli_query($con, "INSERT INTO `users` (`id`, `name`, `email`, `message`) VALUES (NULL, '{$name}', '{$email}', '{$message}')");
echo "Error: " . mysqli_error($con);


if(mysqli_query($con, $query)){
    http_response_code(201);
}else{
    http_response_code(422);

}

【问题讨论】:

  • 嗨。您能否分享提交表单时遇到的确切错误
  • POST ERR_SSL_PROTOCOL_ERROR 是错误。我检查了 connection.php 以确保 url 是正确的。仍然出现同样的错误。
  • 您尝试将 url 更改为 http 而不是 https?

标签: php mysql reactjs axios


【解决方案1】:

改变

const url = "https://localhost:8080/react-php/insert.php"

const url = "http://localhost:8080/react-php/insert.php"

您可能会收到 SSL 证书错误,因为您没有 SSL 证书

【讨论】:

  • 注意:未定义变量:第 21 行 /opt/lampp/htdocs/react-php/insert.php 中的名称 注意:未定义变量:/opt/lampp/htdocs/react-php/ 中的电子邮件第 21 行的 insert.php 注意:未定义的变量:第 21 行的 /opt/lampp/htdocs/react-php/insert.php 中的消息仍然在 php 中出现 undef 错误
  • 对,看起来 SSL 错误正在掩盖其他错误。您的 php 代码没有从反应代码中获取通过表单传递的参数,或者您的反应代码没有成功传递它们。我不是 100% 看你的代码。您熟悉 POSTMAN 应用程序吗?您可以生成一个 POST 请求传递示例参数以快速验证它是 php 代码还是 react 代码
  • 参数用于 GET 请求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多