如何发送一个变量“ text1”到PHP?我一直在尝试解决这个问题,但是没有用

时间:2019-07-15 15:40:42

标签: javascript php

我需要将数据js发送到php 但是将变量javascript传递给php时出现问题。 我一直在寻找Stackoverflow中的任何问题,但是我认为没有像我这样的问题。

function test() {
    var xhttp;
    var text1 = "nasjdnknsak";
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;
        }
    };
    xhttp.open("POST", "train.php", true);
    xhttp.send("text1=dfsbbdfsknkfj");
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "saravine";
    $text1 = $_POST['text1'];
    die("Masuk ga : " . $text1);
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $sql = "INSERT INTO train (caption)
    VALUES ('$text1')";

    if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }
    $conn->close();

0 个答案:

没有答案