0

I have a form in HTML5 on a PHP page. Some of the form elements contain PHP code for things such as populating a drop-down menu with options. Different options are put in each menu, but they all are populated by the same PHP code (called query.php).

I want to pass the name of the HTML element to query.php to determine which query to execute. query.php is coded generally:

<?php
$connection = pg_connect(...);
$query = "SELECT name FROM table ORDER BY name ASC;";
$results = pg_query($connection, $query);
$rows = pg_num_rows($results);

for ($i=0; $i < $rows; $i++) {
    ?>
    <option><?php echo pg_fetch_result($results, $i, 0); ?></option>
    <?php
}
?>

I want 'table' in $query to be a variable coming from the HTML. Here is an example line of HTML:

<p>Select a City: <select name="city"><?php include("query.php"); ?></select>

I've been trying to use the HTTP GET method replacing 'query.php' with query.php?table=$this.name. I understand that I should be able to use $_GET['table'] in query.php and get the passed value, but I don't know the function to get the name of the HTML element. What function, when used within HTML tags, will return the name of the HTML element? For example, if I use query.php?table=$this.name in the above HTML, $_GET['table'] in query.php should return "city". Only $this.name is not the correct function.

3 Answers 3

2

I suggest to create a function to do this:

<?php include("query.php"); ?>

<p>Select a City: <select name="city"><?php echo queryFunction("city"); ?></select></p>

in query.php:

<?php
function queryFunction($table) {
    $connection = pg_connect(...);
    $query = "SELECT name FROM $table ORDER BY name ASC;";
    $results = pg_query($connection, $query);
    $rows = pg_num_rows($results);
    $string = "";    

    for ($i=0; $i < $rows; $i++) {
        $string = $string . "<option>" . pg_fetch_result($results, $i, 0) . "</option>";
    }

    return $string;
}
?>

I'm pretty sure there is no way to get the name of the select box unless you provide it yourself. If this was done in JavaScript it would have been possible.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I'm just learning PHP and did not know how to make functions. Is there any reason to have this function in a separate PHP file rather than just in the page with the form? The form page is also .php
I've edited the code to reflect what worked for my question. $string += "<option>" ... did not work. I replaced it with $string = $string."<option>"... and it worked.
My example has the function in a different file, hence the include above the html part.
I left it as a separate file. I think it is easier to read and I can be certain my database location and login details are not visible on the web.
As i said, my example has them split up, the <?php include("query.php"); ?> includes the file called 'query.php' into the php file with the html.
1

the usual approach to this would be:

in query.php:

<?php
function generateTags() // you can put arguments here
{ 
    $connection = pg_connect(...);
    $query = "SELECT name FROM table ORDER BY name ASC;";
    $results = pg_query($connection, $query);
    $rows = pg_num_rows($results);

    for ($i=0; $i < $rows; $i++) {
            echo "<option>".pg_fetch_result($results, $i, 0)."</option>";
        }
}
?>

and then in php html:

<?php include("query.php"); ?><!-- do this just once at the beginning -->

<p>Select a City: <select name="city"><?php generateTags(/* here could be your arguments */); ?></select>

Comments

0

You can't access the html-tags in PHP, you have to write the table name manually in the call to query.php. For better solutions how to use the code in that file have a look at the other answers.

(btw. $this ist only for working with objects and the syntax for accessing object properties would be $this->name in PHP, not $this.name, but as there are no objects here, it is quite irrelevant ;)

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.