Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass/compare a variable to an SQL database from within a javascript if statement?

Basically my code declares a variable which checks to see if a specific string is entered into a textbox, when the user presses enter, then using substring, it declares a variable which contains a string after the specific word entered (shown below).

I would like to pass this variable to php and ultimately compare it against an SQL database or (but i've heard this is insecure/impossible) compare it directly against an SQL database from javascript.

Is this 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, to perform a check and return true or false.

I hope this is clear and thanks in advance for the help.

like image 273
stefannew Avatar asked Jul 30 '26 07:07

stefannew


1 Answers

Use AJAX to pass the value to a PHP request handler that uses the value in the appropriate database call. For safety, escape the parameter with mysql_real_escape_string when building the SQL. Then have the handler return a response to indicate the outcome (JSON/XML/text/...).

like image 181
Marcelo Cantos Avatar answered Aug 01 '26 21:08

Marcelo Cantos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!