Database not responding to form when input added









up vote
-1
down vote

favorite












So my database does not add this data into the database when the button is pressed.
I have created a form, and all the id's are perfect and the email is a foreign key so it is taken from sessionStorage of the logged in user. I need help with why it is not working, I have no idea. The page alerts me "the order was successful" when I press submit but the data does not get stored in the database.
My SQL statement also works definitely, I tried it in my database.
Here are my php and js:



<?php
header("Content-Type: application/json; charset=UTF-8");
$servername = "localhost";
$username = "root";
$password = "leaf123";
$dbname = "laxmi";

// Create Connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn)

die("Connection failed:" . mysqli_connect_error());

else


// Obtain the contents

$request = file_get_contents('php://input');

// decode json so PHP can use it

$jsonRequest = json_decode($request);

// Query
$sql = "INSERT INTO checkout(email, ccName, ccNumber, month, year, cvc) VALUES ('$jsonRequest->email', '$jsonRequest->ccName', '$jsonRequest->ccNumber', '$jsonRequest->month', '$jsonRequest->year', '$jsonRequest->cvc')"






// Execute Query

$results = mysqli_query($conn, $sql);
echo json_encode("success");
mysqli_close($conn);


my javascript



$(document).ready(function () 
//When the submit button on the checkout form is pressed.
$("#SubmitOrder").on("click", function ()
//store each individual entry into a separate variable.
var email = sessionStorage.getItem("loggedInUser");
var ccname = document.getElementById("ccName").value;
var ccnum = document.getElementById("ccNumber").value;
var month = document.getElementById("month").value;
var year = document.getElementById("year").value;
var cvc = document.getElementById("cvc").value;


//create an array with the details in.
var checkout =
email: email,
ccname: ccname,
ccnum: ccnum,
month: month,
cvc: cvc,

//direct the user to the login page and alert them that their registration was successful.
alert("Your order was successful.")
window.location.href = "../index.html"
//posts the JSON object to the php file so it can fill the database, and converts the checkout array into JSON so it can be read.
var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
)


)










share|improve this question























  • Please dont tag SQL Server and then ask about MySQL, they are 2 very different products. I've corrected these for you. It's important, however, to know what product it is you are using.
    – Larnu
    Nov 10 at 21:22










  • It'd be a good idea to separate out your PHP and JS code into separate blocks in your question - will make it a bit easier for others to read :)
    – Rich Court
    Nov 10 at 21:26










  • @Larnu sorry I must have clicked it by accident
    – Dylan Wade
    Nov 10 at 21:35






  • 1




    @RichCourt ok thanks I did so, do you know any solutions?
    – Dylan Wade
    Nov 10 at 21:37










  • I've posted an answer below - should hopefully be enough to get you started :)
    – Rich Court
    Nov 10 at 21:41














up vote
-1
down vote

favorite












So my database does not add this data into the database when the button is pressed.
I have created a form, and all the id's are perfect and the email is a foreign key so it is taken from sessionStorage of the logged in user. I need help with why it is not working, I have no idea. The page alerts me "the order was successful" when I press submit but the data does not get stored in the database.
My SQL statement also works definitely, I tried it in my database.
Here are my php and js:



<?php
header("Content-Type: application/json; charset=UTF-8");
$servername = "localhost";
$username = "root";
$password = "leaf123";
$dbname = "laxmi";

// Create Connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn)

die("Connection failed:" . mysqli_connect_error());

else


// Obtain the contents

$request = file_get_contents('php://input');

// decode json so PHP can use it

$jsonRequest = json_decode($request);

// Query
$sql = "INSERT INTO checkout(email, ccName, ccNumber, month, year, cvc) VALUES ('$jsonRequest->email', '$jsonRequest->ccName', '$jsonRequest->ccNumber', '$jsonRequest->month', '$jsonRequest->year', '$jsonRequest->cvc')"






// Execute Query

$results = mysqli_query($conn, $sql);
echo json_encode("success");
mysqli_close($conn);


my javascript



$(document).ready(function () 
//When the submit button on the checkout form is pressed.
$("#SubmitOrder").on("click", function ()
//store each individual entry into a separate variable.
var email = sessionStorage.getItem("loggedInUser");
var ccname = document.getElementById("ccName").value;
var ccnum = document.getElementById("ccNumber").value;
var month = document.getElementById("month").value;
var year = document.getElementById("year").value;
var cvc = document.getElementById("cvc").value;


//create an array with the details in.
var checkout =
email: email,
ccname: ccname,
ccnum: ccnum,
month: month,
cvc: cvc,

//direct the user to the login page and alert them that their registration was successful.
alert("Your order was successful.")
window.location.href = "../index.html"
//posts the JSON object to the php file so it can fill the database, and converts the checkout array into JSON so it can be read.
var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
)


)










share|improve this question























  • Please dont tag SQL Server and then ask about MySQL, they are 2 very different products. I've corrected these for you. It's important, however, to know what product it is you are using.
    – Larnu
    Nov 10 at 21:22










  • It'd be a good idea to separate out your PHP and JS code into separate blocks in your question - will make it a bit easier for others to read :)
    – Rich Court
    Nov 10 at 21:26










  • @Larnu sorry I must have clicked it by accident
    – Dylan Wade
    Nov 10 at 21:35






  • 1




    @RichCourt ok thanks I did so, do you know any solutions?
    – Dylan Wade
    Nov 10 at 21:37










  • I've posted an answer below - should hopefully be enough to get you started :)
    – Rich Court
    Nov 10 at 21:41












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











So my database does not add this data into the database when the button is pressed.
I have created a form, and all the id's are perfect and the email is a foreign key so it is taken from sessionStorage of the logged in user. I need help with why it is not working, I have no idea. The page alerts me "the order was successful" when I press submit but the data does not get stored in the database.
My SQL statement also works definitely, I tried it in my database.
Here are my php and js:



<?php
header("Content-Type: application/json; charset=UTF-8");
$servername = "localhost";
$username = "root";
$password = "leaf123";
$dbname = "laxmi";

// Create Connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn)

die("Connection failed:" . mysqli_connect_error());

else


// Obtain the contents

$request = file_get_contents('php://input');

// decode json so PHP can use it

$jsonRequest = json_decode($request);

// Query
$sql = "INSERT INTO checkout(email, ccName, ccNumber, month, year, cvc) VALUES ('$jsonRequest->email', '$jsonRequest->ccName', '$jsonRequest->ccNumber', '$jsonRequest->month', '$jsonRequest->year', '$jsonRequest->cvc')"






// Execute Query

$results = mysqli_query($conn, $sql);
echo json_encode("success");
mysqli_close($conn);


my javascript



$(document).ready(function () 
//When the submit button on the checkout form is pressed.
$("#SubmitOrder").on("click", function ()
//store each individual entry into a separate variable.
var email = sessionStorage.getItem("loggedInUser");
var ccname = document.getElementById("ccName").value;
var ccnum = document.getElementById("ccNumber").value;
var month = document.getElementById("month").value;
var year = document.getElementById("year").value;
var cvc = document.getElementById("cvc").value;


//create an array with the details in.
var checkout =
email: email,
ccname: ccname,
ccnum: ccnum,
month: month,
cvc: cvc,

//direct the user to the login page and alert them that their registration was successful.
alert("Your order was successful.")
window.location.href = "../index.html"
//posts the JSON object to the php file so it can fill the database, and converts the checkout array into JSON so it can be read.
var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
)


)










share|improve this question















So my database does not add this data into the database when the button is pressed.
I have created a form, and all the id's are perfect and the email is a foreign key so it is taken from sessionStorage of the logged in user. I need help with why it is not working, I have no idea. The page alerts me "the order was successful" when I press submit but the data does not get stored in the database.
My SQL statement also works definitely, I tried it in my database.
Here are my php and js:



<?php
header("Content-Type: application/json; charset=UTF-8");
$servername = "localhost";
$username = "root";
$password = "leaf123";
$dbname = "laxmi";

// Create Connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn)

die("Connection failed:" . mysqli_connect_error());

else


// Obtain the contents

$request = file_get_contents('php://input');

// decode json so PHP can use it

$jsonRequest = json_decode($request);

// Query
$sql = "INSERT INTO checkout(email, ccName, ccNumber, month, year, cvc) VALUES ('$jsonRequest->email', '$jsonRequest->ccName', '$jsonRequest->ccNumber', '$jsonRequest->month', '$jsonRequest->year', '$jsonRequest->cvc')"






// Execute Query

$results = mysqli_query($conn, $sql);
echo json_encode("success");
mysqli_close($conn);


my javascript



$(document).ready(function () 
//When the submit button on the checkout form is pressed.
$("#SubmitOrder").on("click", function ()
//store each individual entry into a separate variable.
var email = sessionStorage.getItem("loggedInUser");
var ccname = document.getElementById("ccName").value;
var ccnum = document.getElementById("ccNumber").value;
var month = document.getElementById("month").value;
var year = document.getElementById("year").value;
var cvc = document.getElementById("cvc").value;


//create an array with the details in.
var checkout =
email: email,
ccname: ccname,
ccnum: ccnum,
month: month,
cvc: cvc,

//direct the user to the login page and alert them that their registration was successful.
alert("Your order was successful.")
window.location.href = "../index.html"
//posts the JSON object to the php file so it can fill the database, and converts the checkout array into JSON so it can be read.
var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
)


)







javascript php sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:36

























asked Nov 10 at 21:04









Dylan Wade

11




11











  • Please dont tag SQL Server and then ask about MySQL, they are 2 very different products. I've corrected these for you. It's important, however, to know what product it is you are using.
    – Larnu
    Nov 10 at 21:22










  • It'd be a good idea to separate out your PHP and JS code into separate blocks in your question - will make it a bit easier for others to read :)
    – Rich Court
    Nov 10 at 21:26










  • @Larnu sorry I must have clicked it by accident
    – Dylan Wade
    Nov 10 at 21:35






  • 1




    @RichCourt ok thanks I did so, do you know any solutions?
    – Dylan Wade
    Nov 10 at 21:37










  • I've posted an answer below - should hopefully be enough to get you started :)
    – Rich Court
    Nov 10 at 21:41
















  • Please dont tag SQL Server and then ask about MySQL, they are 2 very different products. I've corrected these for you. It's important, however, to know what product it is you are using.
    – Larnu
    Nov 10 at 21:22










  • It'd be a good idea to separate out your PHP and JS code into separate blocks in your question - will make it a bit easier for others to read :)
    – Rich Court
    Nov 10 at 21:26










  • @Larnu sorry I must have clicked it by accident
    – Dylan Wade
    Nov 10 at 21:35






  • 1




    @RichCourt ok thanks I did so, do you know any solutions?
    – Dylan Wade
    Nov 10 at 21:37










  • I've posted an answer below - should hopefully be enough to get you started :)
    – Rich Court
    Nov 10 at 21:41















Please dont tag SQL Server and then ask about MySQL, they are 2 very different products. I've corrected these for you. It's important, however, to know what product it is you are using.
– Larnu
Nov 10 at 21:22




Please dont tag SQL Server and then ask about MySQL, they are 2 very different products. I've corrected these for you. It's important, however, to know what product it is you are using.
– Larnu
Nov 10 at 21:22












It'd be a good idea to separate out your PHP and JS code into separate blocks in your question - will make it a bit easier for others to read :)
– Rich Court
Nov 10 at 21:26




It'd be a good idea to separate out your PHP and JS code into separate blocks in your question - will make it a bit easier for others to read :)
– Rich Court
Nov 10 at 21:26












@Larnu sorry I must have clicked it by accident
– Dylan Wade
Nov 10 at 21:35




@Larnu sorry I must have clicked it by accident
– Dylan Wade
Nov 10 at 21:35




1




1




@RichCourt ok thanks I did so, do you know any solutions?
– Dylan Wade
Nov 10 at 21:37




@RichCourt ok thanks I did so, do you know any solutions?
– Dylan Wade
Nov 10 at 21:37












I've posted an answer below - should hopefully be enough to get you started :)
– Rich Court
Nov 10 at 21:41




I've posted an answer below - should hopefully be enough to get you started :)
– Rich Court
Nov 10 at 21:41












1 Answer
1






active

oldest

votes

















up vote
0
down vote













First off, you're displaying the success message before even trying to send the post request to your PHP file. So your first job is to re-order things



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout));
alert("Your order was successful.");
window.location.href = "../index.html";


Secondly, you're currently not checking for a response from the server as to whether the request was successful or not. I've modified the example from the jQuery docs https://api.jquery.com/jquery.post/



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
.done(function()
alert("Your order was successful.");
window.location.href = "../index.html";
)
.fail(function()
alert( "error" );
)
.always(function()
alert( "finished" );
);


Once you're done with that, you'll want to look into returning a response from PHP to say whether the query worked etc, but the above is at least enough to get you something that works for now :)






share|improve this answer




















  • thanks, ill try find a solution...
    – Dylan Wade
    Nov 10 at 21:54










  • No worries. Did the above solve the problem for you?
    – Rich Court
    Nov 10 at 21:58










  • no, I am really unsure on what to do here I won't lie. I feel i am missing something small but I can't figure it out.
    – Dylan Wade
    Nov 10 at 22:09










  • Okay. Try just removing the line window.location.href = "../index.html"; from your solution. You'll then be able to look at the console (press F12 in Chrome) and see what errors are coming up. The problem you have at the moment is that you're navigating away to another page before every trying to send anything Hong to the database. That should be the last thing you do, and you should only do it once you're 100% certain that the phone side has done what it's supposed to.
    – Rich Court
    Nov 12 at 6:34











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243393%2fdatabase-not-responding-to-form-when-input-added%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













First off, you're displaying the success message before even trying to send the post request to your PHP file. So your first job is to re-order things



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout));
alert("Your order was successful.");
window.location.href = "../index.html";


Secondly, you're currently not checking for a response from the server as to whether the request was successful or not. I've modified the example from the jQuery docs https://api.jquery.com/jquery.post/



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
.done(function()
alert("Your order was successful.");
window.location.href = "../index.html";
)
.fail(function()
alert( "error" );
)
.always(function()
alert( "finished" );
);


Once you're done with that, you'll want to look into returning a response from PHP to say whether the query worked etc, but the above is at least enough to get you something that works for now :)






share|improve this answer




















  • thanks, ill try find a solution...
    – Dylan Wade
    Nov 10 at 21:54










  • No worries. Did the above solve the problem for you?
    – Rich Court
    Nov 10 at 21:58










  • no, I am really unsure on what to do here I won't lie. I feel i am missing something small but I can't figure it out.
    – Dylan Wade
    Nov 10 at 22:09










  • Okay. Try just removing the line window.location.href = "../index.html"; from your solution. You'll then be able to look at the console (press F12 in Chrome) and see what errors are coming up. The problem you have at the moment is that you're navigating away to another page before every trying to send anything Hong to the database. That should be the last thing you do, and you should only do it once you're 100% certain that the phone side has done what it's supposed to.
    – Rich Court
    Nov 12 at 6:34















up vote
0
down vote













First off, you're displaying the success message before even trying to send the post request to your PHP file. So your first job is to re-order things



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout));
alert("Your order was successful.");
window.location.href = "../index.html";


Secondly, you're currently not checking for a response from the server as to whether the request was successful or not. I've modified the example from the jQuery docs https://api.jquery.com/jquery.post/



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
.done(function()
alert("Your order was successful.");
window.location.href = "../index.html";
)
.fail(function()
alert( "error" );
)
.always(function()
alert( "finished" );
);


Once you're done with that, you'll want to look into returning a response from PHP to say whether the query worked etc, but the above is at least enough to get you something that works for now :)






share|improve this answer




















  • thanks, ill try find a solution...
    – Dylan Wade
    Nov 10 at 21:54










  • No worries. Did the above solve the problem for you?
    – Rich Court
    Nov 10 at 21:58










  • no, I am really unsure on what to do here I won't lie. I feel i am missing something small but I can't figure it out.
    – Dylan Wade
    Nov 10 at 22:09










  • Okay. Try just removing the line window.location.href = "../index.html"; from your solution. You'll then be able to look at the console (press F12 in Chrome) and see what errors are coming up. The problem you have at the moment is that you're navigating away to another page before every trying to send anything Hong to the database. That should be the last thing you do, and you should only do it once you're 100% certain that the phone side has done what it's supposed to.
    – Rich Court
    Nov 12 at 6:34













up vote
0
down vote










up vote
0
down vote









First off, you're displaying the success message before even trying to send the post request to your PHP file. So your first job is to re-order things



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout));
alert("Your order was successful.");
window.location.href = "../index.html";


Secondly, you're currently not checking for a response from the server as to whether the request was successful or not. I've modified the example from the jQuery docs https://api.jquery.com/jquery.post/



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
.done(function()
alert("Your order was successful.");
window.location.href = "../index.html";
)
.fail(function()
alert( "error" );
)
.always(function()
alert( "finished" );
);


Once you're done with that, you'll want to look into returning a response from PHP to say whether the query worked etc, but the above is at least enough to get you something that works for now :)






share|improve this answer












First off, you're displaying the success message before even trying to send the post request to your PHP file. So your first job is to re-order things



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout));
alert("Your order was successful.");
window.location.href = "../index.html";


Secondly, you're currently not checking for a response from the server as to whether the request was successful or not. I've modified the example from the jQuery docs https://api.jquery.com/jquery.post/



var jqxhr = $.post("../php/checkoutOrder.php", JSON.stringify(checkout))
.done(function()
alert("Your order was successful.");
window.location.href = "../index.html";
)
.fail(function()
alert( "error" );
)
.always(function()
alert( "finished" );
);


Once you're done with that, you'll want to look into returning a response from PHP to say whether the query worked etc, but the above is at least enough to get you something that works for now :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:39









Rich Court

688




688











  • thanks, ill try find a solution...
    – Dylan Wade
    Nov 10 at 21:54










  • No worries. Did the above solve the problem for you?
    – Rich Court
    Nov 10 at 21:58










  • no, I am really unsure on what to do here I won't lie. I feel i am missing something small but I can't figure it out.
    – Dylan Wade
    Nov 10 at 22:09










  • Okay. Try just removing the line window.location.href = "../index.html"; from your solution. You'll then be able to look at the console (press F12 in Chrome) and see what errors are coming up. The problem you have at the moment is that you're navigating away to another page before every trying to send anything Hong to the database. That should be the last thing you do, and you should only do it once you're 100% certain that the phone side has done what it's supposed to.
    – Rich Court
    Nov 12 at 6:34

















  • thanks, ill try find a solution...
    – Dylan Wade
    Nov 10 at 21:54










  • No worries. Did the above solve the problem for you?
    – Rich Court
    Nov 10 at 21:58










  • no, I am really unsure on what to do here I won't lie. I feel i am missing something small but I can't figure it out.
    – Dylan Wade
    Nov 10 at 22:09










  • Okay. Try just removing the line window.location.href = "../index.html"; from your solution. You'll then be able to look at the console (press F12 in Chrome) and see what errors are coming up. The problem you have at the moment is that you're navigating away to another page before every trying to send anything Hong to the database. That should be the last thing you do, and you should only do it once you're 100% certain that the phone side has done what it's supposed to.
    – Rich Court
    Nov 12 at 6:34
















thanks, ill try find a solution...
– Dylan Wade
Nov 10 at 21:54




thanks, ill try find a solution...
– Dylan Wade
Nov 10 at 21:54












No worries. Did the above solve the problem for you?
– Rich Court
Nov 10 at 21:58




No worries. Did the above solve the problem for you?
– Rich Court
Nov 10 at 21:58












no, I am really unsure on what to do here I won't lie. I feel i am missing something small but I can't figure it out.
– Dylan Wade
Nov 10 at 22:09




no, I am really unsure on what to do here I won't lie. I feel i am missing something small but I can't figure it out.
– Dylan Wade
Nov 10 at 22:09












Okay. Try just removing the line window.location.href = "../index.html"; from your solution. You'll then be able to look at the console (press F12 in Chrome) and see what errors are coming up. The problem you have at the moment is that you're navigating away to another page before every trying to send anything Hong to the database. That should be the last thing you do, and you should only do it once you're 100% certain that the phone side has done what it's supposed to.
– Rich Court
Nov 12 at 6:34





Okay. Try just removing the line window.location.href = "../index.html"; from your solution. You'll then be able to look at the console (press F12 in Chrome) and see what errors are coming up. The problem you have at the moment is that you're navigating away to another page before every trying to send anything Hong to the database. That should be the last thing you do, and you should only do it once you're 100% certain that the phone side has done what it's supposed to.
– Rich Court
Nov 12 at 6:34


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243393%2fdatabase-not-responding-to-form-when-input-added%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto