So, I am preparing my school project, I am struggling to find out, What's the error in this code ? When I remove the $sql = "UPDATE wp_assign_cards_numbers SET numbers ";
code is executing perfectly, when I use the above line. The Code is not executing, the blank is displayed,Even i am not aware where to check error ?? So I am not able to trace the error also.
function mySubmission(){
alert("Submission..");
var grid = document.getElementById("myTable");
//Reference the CheckBoxes in Table.
var checkBoxes = grid.getElementsByTagName("input");
var card = "";
var number ="";
//Loop through the CheckBoxes.
for (var i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked === false) {
var row = checkBoxes[i].parentNode.parentNode;
card = row.cells[0].innerHTML;
number = row.cells[1].innerHTML;
document.cookie = "myJavascriptVarCard = " + card;
document.cookie = "myJavascriptVarNumber = " + number;
<?php
$link = mysqli_connect($hostname, $username, $password, $database);
$card= $_COOKIE['myJavascriptVarCard'];
$number = $_COOKIE['myJavascriptVarNumber'];
$sql = "UPDATE wp_assign_cards_numbers SET numbers ";
?>
}
}
}
The short answer here is that you cannot mix PHP and JS like this without some kind of interface bridging the two.
Check out these basic examples of XHR requests: https://blog.garstasio.com/you-dont-need-jquery/ajax/
But the basic example is here (replace the php chunk you have with this):
var xhr = new XMLHttpRequest();
xhr.open('POST', '/your-php-script.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status === 200 && xhr.responseText !== newName) {
alert('Something went wrong. ' + xhr.responseText);
}
else if (xhr.status !== 200) {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send(encodeURI('card=' + card + '&number=' + number));
And at the root of your directory you have your-php-script.php with some basic handler:
<?php
if(isset($_POST['card'])){
$card = $_POST['card'];
$number = $_POST['number'];
// do something with these vars
}
However, we're not done. For one, this xhr request is in a loop. You will want to collect those values and pass them as one request outside that loop. Your php example code is in pretty bad shape as it is. We have no idea if you're setting your mysqli connection correctly, and the query itself is definitely not going to work that way. But those are for another question. A little googling will help you with that part.
This answer is simply to illustrate how you communicate with a php handler from javascript. You would still need to make sure those inputs are set and something is calling the mySubmission function, plus make sure the js, html, and php are organized in a sane way. Make use of console logs and php logs to help debug issues as you go along.
Also, if this has anything to do with credit cards, don't approach it this way. Never store credit info in a cookie, local storage, or anything else that can be intercepted.