Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数返回包含PHP代码的HTML元素的名称_Php_Html_Forms - Fatal编程技术网

函数返回包含PHP代码的HTML元素的名称

函数返回包含PHP代码的HTML元素的名称,php,html,forms,Php,Html,Forms,我在PHP页面上有一个HTML5表单。一些表单元素包含PHP代码,例如用选项填充下拉菜单。每个菜单中都有不同的选项,但它们都由相同的PHP代码(称为query.PHP)填充 我想将HTML元素的名称传递给query.php,以确定执行哪个查询。query.php通常编码为: <?php $connection = pg_connect(...); $query = "SELECT name FROM table ORDER BY name ASC;"; $results = pg_quer

我在PHP页面上有一个HTML5表单。一些表单元素包含PHP代码,例如用选项填充下拉菜单。每个菜单中都有不同的选项,但它们都由相同的PHP代码(称为query.PHP)填充

我想将HTML元素的名称传递给query.php,以确定执行哪个查询。query.php通常编码为:

<?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
}
?>

我一直在尝试使用HTTPGET方法将“query.php”替换为
query.php?table=$this.name
。我知道我应该能够在query.php中使用
$\u GET['table']
,并获取传递的值,但我不知道获取HTML元素名称的函数。在HTML标记中使用什么函数将返回HTML元素的名称?例如,如果我在上面的HTML中使用
query.php?table=$this.name
,则query.php中的
$\u GET['table']
应该返回“city”。只有$this.name不是正确的函数。

通常的方法是:

在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>";
        }
}
?>
<?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;
}
?>

然后在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>

选择一个城市:

通常的方法是:

在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>";
        }
}
?>
<?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;
}
?>

然后在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>

选择一个城市:

我建议创建一个函数来执行此操作:

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

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

选择一个城市:

在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>";
        }
}
?>
<?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;
}
?>


我很确定,除非您自己提供,否则无法获取选择框的名称。如果这是在JavaScript中完成的,那么这是可能的。

我建议创建一个函数来实现这一点:

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

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

选择一个城市:

在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>";
        }
}
?>
<?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;
}
?>


我很确定,除非您自己提供,否则无法获取选择框的名称。如果这是在JavaScript中完成的,那么这是可能的。

您不能在PHP中访问html标记,必须在调用
query.PHP
时手动写入表名。要获得更好的解决方案,如何使用该文件中的代码,请查看其他答案


(顺便说一句,
$this
仅用于处理对象,访问对象属性的语法在PHP中是
$this->name
,而不是
$this.name
,但由于此处没有对象,因此与此无关;)

您无法访问PHP中的html标记,您必须在调用
query.php
时手动写入表名。要获得更好的解决方案,如何使用该文件中的代码,请查看其他答案


(顺便说一句,
$this
仅用于处理对象,在PHP中访问对象属性的语法将是
$this->name
,而不是
$this.name
,但由于这里没有对象,这与此无关;)

谢谢,我只是在学习PHP,不知道如何生成函数。是否有任何理由将此函数放在单独的PHP文件中,而不仅仅放在表单页面中?表单页面也是。phpI已经编辑了代码,以反映我的问题的有效性<代码>$string+=“”…不起作用。我用
$string=$string.”替换了它,它成功了。我的示例在另一个文件中有这个函数,因此html部分上面有include。我将它作为一个单独的文件保留。我认为它更容易阅读,而且我可以确定我的数据库位置和登录详细信息在web上不可见。正如我所说,我的示例将它们分开,
将名为“query.php”的文件与html一起包含到php文件中。谢谢,我只是在学习php,不知道如何制作函数。是否有任何理由将此函数放在单独的PHP文件中,而不仅仅放在表单页面中?表单页面也是。phpI已经编辑了代码,以反映我的问题的有效性<代码>$string+=“”…
不起作用。我用
$string=$string.”替换了它,它成功了。我的示例在另一个文件中有这个函数,因此html部分上面有include。我将它作为一个单独的文件保留。我认为它更容易阅读,而且我可以确定我的数据库位置和登录详细信息在web上不可见。正如我所说,我的示例将它们分开,
将名为“query.php”的文件与html一起包含到php文件中。