Is it possible to pass / compare a variable to a SQL database from a javascript if statement?
Basically my code is declaring a variable that checks if a specific string is entered in the textbox when the user presses the enter button, and then using a substring declares a variable that contains the string after entering a certain word (shown below).
I would like to pass this variable to php and eventually compare it to a SQL database (but I heard it is unsafe / not possible) compare it directly to a SQL database from javascript.
Is it possible?
var match = textInput.value.indexOf("string");
if (keycode == 13 && match != -1) { <- checks to see if enter is pressed + "string" has been typed.
var some_string = text.input.value.substring(7); <- takes everything after the word "string"
and this is where I would like to pass the variable 'some_string' to the SQL database, do validation and return true or false.
I hope this is clear and in advance for the help.
source to share
You will need ajax request . You must do this asynchronously, otherwise the browser will hang.
But asynchronous request works with callbacks and doesn't return more than the promise can be. So instead of
function synchronous(){
// init xhr and send
return xhr.response; // or json-parse of response-text or something
}
try {
if (synchronous() == "wanted value")
// do something
else
// do something else
} catch(e) {
// alert a problem
}
you will need to use
function asychronous(success, error) {
// init xhr, register handlers and send
// return nothing
}
asynchronous(function scb(data) {
if (data == "wandted value")
// do something
else
// do something else
}, function ecb(e) {
// alert a problem
});
source to share
Use AJAX to pass the value to the PHP request processor, which uses the value in the corresponding database call. For security, avoid the mysql_real_escape_string option when building the SQL. The handler then returns a response to indicate the result (JSON / XML / text / ...).
source to share