Im new to php/html and I'm trying to get a value from a html form and set this as a variable in an external php script.
This variable is used to run a SQL in a postgres database. The php script is triggered by clicking a button.
My guess was to use where get value is the text in the html form:
$SQL = $_GET['get_value'];
But I can't get it working. Can somebody help me and explain what to do?
My html code is as following:
<!DOCTYPE html>
<html>
<body>
<form name="SQL" action="query_map.php" method="get">
SQL:<br>
<input type="text" name="SQL" id="get_value">
<br>
</form>
<button type="submit" id="script-button">
Run the script
</button>
<script>
function runScript() {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
alert('Ran the script, result was ' + request.responseText);
} else {
alert('Something went wrong, status was ' + request.status);
}
}
};
request.open('POST', 'http://localhost:8076/query_map.php', true);
request.send(null);
return false;
};
document.getElementById('script-button').onclick = runScript;
</script>
</body>
</html>
The php code is as following:
<?php
$SQL = $_GET['get_value'];
$db = pg_connect("host=localhost dbname=kopse_hof_put_25 user=postgres password=baf45baf")
or die ("Could not connect to server\n");
$query = pg_query($db, "create or replace view resultaat as
select *
from put_25_vlak_1_vulling
where id = $SQL");
?>
I dont see any errors in my chrome console. I tested the php and that is working fine.