【问题标题】:Not able to get form element's value after submitting form using this.form.submit();使用 this.form.submit() 提交表单后无法获取表单元素的值;
【发布时间】:2019-07-30 07:59:53
【问题描述】:

这是我用 PHP 编写的 HTML:

<form method="post" action="">
    <select name="seltemplate" id="seltemplate" style="width:150px" onChange="this.form.submit();">
        <option value="0">Select a Template</option>
        <?php
        // Running the sql query
        $query = "SELECT * FROM `stripo_email_templates`";
        $rs = mysqli_query($con, $query);

        // If at least one record found
        if(mysqli_num_rows($rs) > 0)
        {
            // Fetch table rows
            while($row = mysqli_fetch_array($rs))
            {
                $template_id = $row['template_id'];
                $template_name = $row['template_name'];
                echo "<option value='$template_id'>$template_name</option>";
            }
        }
        ?>
    </select>
</form>

我正在使用this.form.submit(); 提交表单,没有任何提交按钮。

因此,一旦选择列表选项发生更改,就会提交表单。

提交表单后,我想接收选择列表选定选项的值。

我在附和

echo $_POST['seltemplate'];

但没有收到任何值。为什么?

【问题讨论】:

  • 是否设置了$template_id&lt;option&gt;s 有值吗?
  • 哎呀!这就是问题所在。谢谢。

标签: javascript php


【解决方案1】:

this.form.submit() 不是在选择框元素中编写的好习惯,因为 "this" 表示选择框不是您尝试此代码的表单或文档。

function select_box_change()
{
    var f = document.getElementById('form1');
    f.action = "your_php_file";
    f.submit();
}

html

<form method="post" id="form1">
    <select id="seltemplate" style="width:150px" onChange="select_box_change();">
        <option value="0">Select a Template</option>
        <option>a</option>
        <option>b</option>
    </select>
</form>

【讨论】:

  • 我将表单提交到它出现的同一页面,那么 f.action 的值是什么?
  • 但你的页面可以包含 php 脚本
  • this.form.submit() 不是写在选择框元素中的好习惯,因为“this”意味着选择框不是表单” - 这就是为什么这不是 this.submit() 开始的原因与,但首先明确引用表单,使用this.form ...这没有错,说这不是“良好做法”完全没有根据和错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 2012-01-13
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2011-10-10
相关资源
最近更新 更多