简体   繁体   English

从输入标签获取布尔值到Postgres DB

[英]Getting boolean value from input label to Postgres DB

I have a little problem. 我有一点问题。 I have a HTML form that fills a Postgres database like this : 我有一个HTML表单,它填充像这样的Postgres数据库:

<form method="post">
                        <label for="shortcode"> Shortcode </label>
                        <input type="text" name="shortcode" id="shortcode">

                        <label for="prix"> Price </label>   
                        <input type="number" name="prix" id="prix" >        

                        <label for="taxe"> Tax </label>
                        <input type="texte" name="taxe" id="taxe">  
    </form>

The problem is that the 'taxe' label is a boolean type on the database, so when I fill the 'taxe' input with values except "TRUE" and "FALSE" or "T" and "F" I will have an error. 问题是“ taxe”标签是数据库上的布尔类型,因此当我用“ TRUE”和“ FALSE”或“ T”和“ F”以外的值填充“ taxe”输入时,将出现错误。 I'm not so good on HTML but I tried to add an input checkbox instead of text : 我对HTML不太满意,但尝试添加输入复选框而不是文本:

<input type="checkbox" id="tax_checkbox" onclick="validate()" name="taxe">

and I tried this function : 我尝试了这个功能:

function validate() {
  var isChecked = false;
  if (document.getElementById('tax_checkbox').checked) {
    isChecked = true;
    console.log(isChecked);
  } else {
    isChecked = false;
    console.log(isChecked);
  }
}

This is my Pgsql code to take input values to the Database : 这是我的Pgsql代码,用于将输入值输入数据库:

$shortcode= $_POST['shortcode'];
        $prix= $_POST['prix'];
        $taxe= $_POST['taxe'];

        $sql = "INSERT INTO shortcode (sc_shortcode,prix_ttc_shortcode,taxe_shortcode) VALUES ('".$shortcode."','".$prix."','".$taxe."')";
        $q=pg_query($sql);

Finally I get this error : Warning: pg_query() [function.pg-query]: Query failed: ERROR: invalid input syntax for type boolean: "on" in C:\\Users\\aspis\\Documents\\WWW\\Create.php on line 17 最后,我得到此错误:警告: pg_query() [function.pg-query]: Query failed: ERROR: invalid input syntax for type boolean: "on" in C:\\Users\\aspis\\Documents\\WWW\\Create.php on line 17

Try adding value attribute to your checkbox and add a hidden input to set the default value for unchecked checkbox: 尝试将value属性添加到您的复选框,并添加一个隐藏的输入以设置未选中复选框的默认值:

<input type="hidden" name="taxe" value="false" /> 
<input type="checkbox" name="taxe" value="true" />

If the checkbox is unchecked, then it will submit the value from the hidden input. 如果未选中该复选框,则它将从隐藏的输入中提交值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM