0

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.

6
  • Instead of using id attribute of html tag use name attribute $SQL = $_REQUEST['SQL']; Commented Jan 25, 2016 at 11:32
  • id attrribute is used in javascript to get value as getElementById . You need to use name attribute to each input field which will pass value to php script. Commented Jan 25, 2016 at 11:35
  • first call this function via submit button . JavaScript function is not called. For ex. <button onclick="myFunction()">Click me</button> Commented Jan 25, 2016 at 11:38
  • i believe the function is called. See the bottum of the code: document.getElementById('script-button').onclick = runScript; Commented Jan 25, 2016 at 12:32
  • Possible duplicate of Sending POST data with a XMLHttpRequest Commented Jan 25, 2016 at 12:42

3 Answers 3

1

you read GET and POST by input name not id, so it should be:

$SQL = $_GET['SQL'];
Sign up to request clarification or add additional context in comments.

1 Comment

still not working, i believe the problem is in the button click
1

Use universal POST/GET

$SQL = $_REQUEST["SQL"];

1 Comment

still not working, i believe the problem is in the button click
0

I believe I found the solution thanks to @Rahul Dambare

I added a to my form. So my form looks as following:

<form name="SQL" action="query_map.php" method="get">
  SQL:<br>
  <input type="text" name="SQL" id="SQL">
  <br>
  <input type="submit">
 </form>

I got then the following error:

ERROR: syntax error at end of input LINE 4: where id = ^ in C:\xampp\htdocs\query_map.php on line 9.

The reason that this error exist is because my SQL think my response is a text. There are two possible solutions for this. The first solution is to change in the form the type of text to integer.

The second solution is to add a pg_escape_string to have quotes in the SQL. The php is as followed:

<?php 
$test = $_REQUEST['SQL'];
$SQL = pg_escape_string($test);
        $db = pg_connect("host=localhost dbname=kopse_hof_put_25 user=postgres password=password")
		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");

Thanks for all the help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.