简体   繁体   English

无法将数据从JavaScript传递到PHP文件

[英]Cannot pass data from my JavaScript to my PHP file

When I click on "subcribeButton" I want it to save the value and pass that value to my PHP file. 当我单击“ subcribeButton”时,我希望它保存值并将该值传递给我的PHP文件。 I want that PHP file to then make a post to the SQL database. 我想要那个PHP文件然后发布到SQL数据库。

This is my js: 这是我的js:

$(".subscribeButton").on( "click", function() {

    var email = $(".subscribeInput").val();
    var isValid = isEmailValid(email);

    if(isValid){
        $(".errorMsg").css( "display", "none" );
        modal.style.display = "block";

        $.post( "save.php", { email: email });

        $(".subscribeInput").val("");

    } else {
        $(".errorMsg").css( "display", "initial" );
    };

});

$(".subscribeInput").on( "click", function() {

    $(".subscribeInput").val("");

});

This is my php code, I would like my php code to accept data from my js file and then post the data to my sql database: 这是我的php代码,我希望我的php代码接受我js文件中的数据,然后将数据发布到我的sql数据库中:

<?php
$servername = "localhost";
$username = "user";
$password = "pw";
$dbname = "db_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$text = $_POST['email'];

echo $text

$sql = "INSERT INTO MyGuests (email)
 VALUES ($text)";



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

$conn->close();
?>

The result is I'm getting the following error: 结果是出现以下错误:

POST http://flockto.it/home/save.php 500 (Internal Server Error)
send    @   jquery.js:4
ajax    @   jquery.js:4
m.(anonymous function)  @   jquery.js:4
(anonymous function)    @   email.js:13
dispatch    @   jquery.js:3
r.handle    @   jquery.js:3

so why not adding a callback in your ajax request so you can debug it in console or with an alert see this it may help you 因此,为什么不在您的ajax请求中添加回调,以便您可以在控制台中对其进行调试,也可以在出现警报时看到它可能会对您有所帮助

$.post( "save.php", { "email" : email } , function(result){
   console.log(result);//here the result will display what you will echo after getting the post in your php file
});

so in your php file you can add this 因此,在您的php文件中,您可以添加此内容

if (isset($_POST['email'])){
   $text = $_POST['email'];
   echo $text;
}
//so if you check the console you will get the email
//value from your php file so then you can insert it at the DB as you want

You have a syntax error. 您有语法错误。
Your code is missing the ; 您的代码缺少; at the end of this line: 在此行的末尾:

echo $text

It should be: 它应该是:

echo $text;

The solution: 解决方案:

In the js file url was wrong and quotation marks around email were missing js文件中的网址错误,并且电子邮件周围的引号丢失了

$(".subscribeButton").on( "click", function() {

    var email = $(".subscribeInput").val();
    var isValid = isEmailValid(email);

    if(isValid){
        $(".errorMsg").css( "display", "none" );
        modal.style.display = "block";

        $.post( "data/save.php", { "email": email });

        $(".subscribeInput").val("");

    } else {
        $(".errorMsg").css( "display", "initial" );
    };

});

$(".subscribeInput").on( "click", function() {

    $(".subscribeInput").val("");

});

In the php file used the wrong variable 在php文件中使用了错误的变量

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$text = $_POST['email'];

if (isset($_POST['email'])){
   $text = $_POST['email'];
   echo $text;
}

$sql = "INSERT INTO MyGuests (email)
 VALUES ('$text')";



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

$conn->close();
?>

A million thanks to everyone who replied to this thread and helped me solve the issue. 一百万感谢所有回复此主题并帮助我解决问题的人。

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

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