0

When I click on "subcribeButton" I want it to save the value and pass that value to my PHP file. I want that PHP file to then make a post to the SQL database.

This is my 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
$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
11
  • 2
    Good!!! You have done it. Now what do you want us to do?? Is there anything wrong or is it not working??? Commented Sep 1, 2016 at 20:12
  • 1
    You see that is what I am talking about....you should add that to your question so that everyone else here (trying to help you) knows what is the exact problem. Also to avoid downvote :) :) Commented Sep 1, 2016 at 20:15
  • 1
    Make sure the url that you are calling in $.post is correct. It looks like the path specified is not right. Commented Sep 1, 2016 at 20:20
  • 1
    Open this URL and tell us what do you see: flockto.it/home/save.php Commented Sep 1, 2016 at 20:22
  • 1
    see I told you its the url issue. Anyways you'll get there. happy coding :) Commented Sep 1, 2016 at 20:42

3 Answers 3

1

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

$.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

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
Sign up to request clarification or add additional context in comments.

2 Comments

This helped me get past one of the errors, thank you!! $.post( "save.php", { "email" : email } should have also been $.post( "data/save.php", { "email" : email }
whenever you're in a doubt with url to post to i can suggest you to put the absolute url of the page as "http://.../.../../save.php" so with this you can dubug it easily :D i'm glad cause it helped
1

You have a syntax error.
Your code is missing the ; at the end of this line:

echo $text

It should be:

echo $text;

Comments

0

The solution:

In the js file url was wrong and quotation marks around email were missing

$(".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

// 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.