I'm having some issues creating a little web page. The page gets the data from a mysql database and displays them in a select. What I want to do is getting the value of this select with JavaScript, create a string with this value and then giving it back to the PHP script. I know that JS and PHP cannot communicate, so is there a way to perform what I need?
this is the code of my form in PHP
echo "<select name = 'delFromWArea' onchange = 'categoryOfWArea(this)'>";
and this is the code of my JavaScript
function categoryOfWArea (warea) {
var area = warea.value;
var query = "SELECT Nome_C FROM Contiene WHERE Nome_AW = '" + area + "';";
return query;
}
Thanks to everyone!
-
2DO NOT CONSTRUCT AN SQL QUERY CLIENT SIDE Instead pass the plain value back to the server (regular form submit or ajax) validate it create the query server side (using prepared statements)Steve– Steve2016年05月19日 15:13:34 +00:00Commented May 19, 2016 at 15:13
-
1wow, there are some really strange questions today. 1st get rid of that query asap. 2nd, use ajax method, to send data to php.Peon– Peon2016年05月19日 15:14:51 +00:00Commented May 19, 2016 at 15:14
-
You are tring to write SERVER Side code to be run in the BROWSER. That I am afraid is not possible. See AJAXRiggsFolly– RiggsFolly2016年05月19日 15:15:55 +00:00Commented May 19, 2016 at 15:15
1 Answer 1
I have to agree with Steve. It's a bad way to perform the Database-Operations on client side, since the database is located on your server.
It's much better to fill your <select> at the very beginning when the user is directing to your website.
Then you can pass the chosen value to your php-script with AJAX. There are many examples out there like: Example for the work with php and ajax
Since AJAX provides a communication with the server an therefore loading content without refreshing the whole page, it's a beatiful opportunity to request data from the server.
Give your thinking a new direction:
- The php script plots your HTML
- In the header of the plotted HTML there's your script
- The script contains an AJAX-request to fill your
<select> - When the user choose one
<option>the script fires another AJAX-request - The php script gets called. With the
GET-type you can easily send the functions name to call - in your .php you can use
$_GET['functionname']to retrieve the name of the funtion you want to call - Do what you want to do
- Just return the result. AJAX will do the rest on his own!
- because of 8. you can just work with the result in your javascript
If you're interested in developing web pages, you might be interested in jQuery.