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))
)
)
javascript php sql
add a comment |
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))
)
)
javascript php sql
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
add a comment |
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))
)
)
javascript php sql
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
javascript php sql
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
add a comment |
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
add a comment |
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 :)
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 linewindow.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
add a comment |
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 :)
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 linewindow.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
add a comment |
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 :)
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 linewindow.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
add a comment |
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 :)
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 :)
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 linewindow.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
add a comment |
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 linewindow.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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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