【问题标题】:How to pass getElementById to PHP that can store data to database如何将getElementById传递给可以将数据存储到数据库的PHP
【发布时间】:2016-12-07 18:47:18
【问题描述】:

我写了一个代码,它可以根据性别自动生成总量。我是用javascript写的。但我不知道如何使用 php 将数据存储到数据库中。所有编码都在一页中。

这是我的代码

     <script type = "text/javascript">
     function updateTotal() {
     var genderPart=0;
     var totalPart =0;

    function checkGender(){
    if(document.getElementById('gender').value=='GIRL'){
    genderPart+=1;
    }
    if(document.getElementById('gender').value=='BOY'){
    genderPart+=2;
    }
    }//end of checking gender part

     checkGender();

     var totalPart= genderPart; 
     document.getElementById('totalPart').innerHTML = totalPart;

      }//end of my main  total function
    </script>

     <form action="" method="post" name="nomineeform">
     <table align="center" width="800" border="2" >
     <tr>
     <td align="right"><b>Gender :</b></td>
     <td><select name="gender" id="gender"  onchange="updateTotal()">
                  <option selected="selected" value="">--Choose--</option>
                  <option value="GIRL" >GIRL</option>
                  <option value="BOY" >BOY</option>
                </select></td> 
            </tr>

             <tr>
                <td align="right"><b>Type of Animal :</b></td>
                <td><select name="animal" id="animal" onchange="updateTotal()">
                <option selected="selected" value="">--Jenis Haiwan--</option>
                <?php
                    $get_animal = "select * from animal";

                    $run_animal = pg_query($get_animal);

                        while ($row_animal=pg_fetch_array($run_animal))
                        {
                            $animal_id = $row_animal['animal_id'];
                            $animal_name = $row_animal['typeanimal'];

                            echo "<option value='$animal_id'>$animal_name</option>";
                        }
                    ?>
                </select></td>
            </tr>

             <tr>
                <td align="right"><b>Quantity :</b></td>
                <td><input type="text" name="quantity" size="60" value = "<?php echo $_GET['totalPart'];?>" required="required"/></td>

            </tr>
            <tr></tr>
            <tr align="center">
                <td colspan="8"><input type="submit" name="nominee" class="btn btn-info" value=" TERUSKAN " onclick="return val();"/></td>


            </tr>


    </table>

</form>

            <?php 
if(isset($_POST['nominee']))
    {
        $connection = pg_connect("user = postgres password = syafiqah26 port = 5433 dbname = bengkel2 host = localhost");
          $name = pg_escape_string($_POST['name']);
        $gender =pg_escape_string($_POST['gender']);
        $age= pg_escape_string($_POST['age']);
        $cust_id= pg_escape_string($_POST['hidden']);
        $animal= pg_escape_string($_POST['animal']);
        $quantity= pg_escape_string($_POST['quantity']);


        $query = "INSERT INTO nominee(name,age,gender,cust_id,animal,quantity)
        VALUES('$name','$age','$gender','$cust_id','$animal','$quantity')";
        $result = pg_query($connection,$query);
       }

【问题讨论】:

  • PHP 在您的任何页面加载之前执行。它是一种服务器端脚本语言,与客户端的 Javascript 不同。发送变量供 PHP 处理的唯一方法是在加载页面之前传递它们。 (即使用GETPOST 参数)。你可以让你的网站说提交一个表单到另一个 PHP 页面来处理你​​的参数,但是另一个页面必须以一种或另一种方式加载。这能回答你的问题吗?
  • totalPart 元素在哪里?是您要发送到 php 的该元素的 innerHtml 吗?
  • @bugfroggy 你能给我一个示例代码吗?这样可以让我更清楚。
  • @leo_ap 是的,我想将 innerHtml 的 totalPart 的值发送到 php
  • @SySyBy 您想在什么时候发送此信息?什么时候提交表单?

标签: javascript php


【解决方案1】:

你需要做两件事:

给 INPUT 一个 ID。

使用不同的方法更新 INPUT (document.getElementById("totalPart").setAttribute("value", totalPart);)

来源:Javascript set value of an input field

我在代码中使用“

<script type = "text/javascript">
    function updateTotal() {
        var genderPart=0;
        var totalPart =0;

        function checkGender(){
            if(document.getElementById('gender').value=='GIRL'){
                genderPart+=1;
            }
            if(document.getElementById('gender').value=='BOY'){
                genderPart+=2;
            }
        }//end of checking gender part

        checkGender();

        totalPart = genderPart;
        document.getElementById("totalPart").setAttribute("value", totalPart); <-- I CHANGED THIS

    }//end of my main  total function
</script>

<form action="" method="post" name="nomineeform">
    <table align="center" width="800" border="2" >
        <tr>
            <td align="right"><b>Gender :</b></td>
            <td><select name="gender" id="gender"  onchange="updateTotal()">
                    <option selected="selected" value="">--Choose--</option>
                    <option value="GIRL" >GIRL</option>
                    <option value="BOY" >BOY</option>
                </select></td>
        </tr>

        <tr>
            <td align="right"><b>Type of Animal :</b></td>
            <td><select name="animal" id="animal" onchange="updateTotal()">
                    <option selected="selected" value="">--Jenis Haiwan--</option>
                    <?php 
                    $get_animal = "select * from animal";

                    $run_animal = pg_query($get_animal);

                    while ($row_animal=pg_fetch_array($run_animal))
                    {
                        $animal_id = $row_animal['animal_id'];
                        $animal_name = $row_animal['typeanimal'];

                        echo "<option value='$animal_id'>$animal_name</option>";
                    }
                    ?>
                </select></td>
        </tr>

        <tr>
            <td align="right"><b>Quantity :</b></td>
            <td><input type="text" name="quantity" size="60" id="totalPart" value = "0" required="required"/></td> <-- I CHANGED THIS

        </tr>
        <tr></tr>
        <tr align="center">
            <td colspan="8"><input type="submit" name="nominee" class="btn btn-info" value=" TERUSKAN " onclick="return val();"/></td>


        </tr>


    </table>

</form>

<?php
if(isset($_POST['nominee']))
{
    $connection = pg_connect("user = postgres password = syafiqah26 port = 5433 dbname = bengkel2 host = localhost");
    $name = pg_escape_string($_POST['name']);
    $gender =pg_escape_string($_POST['gender']);
    $age= pg_escape_string($_POST['age']);
    $cust_id= pg_escape_string($_POST['hidden']);
    $animal= pg_escape_string($_POST['animal']);
    $quantity= pg_escape_string($_POST['quantity']);


    $query = "INSERT INTO nominee(name,age,gender,cust_id,animal,quantity)
        VALUES('$name','$age','$gender','$cust_id','$animal','$quantity')";
    $result = pg_query($connection,$query);
}

【讨论】:

  • 非常感谢!!我使用此代码并成功输入数据!再次感谢您!
【解决方案2】:

做你想做的事的方法是通过 AJAX。这个想法是您可以使用您刚刚在 JavaScript 中执行的所有内容并调用 PHP 页面来发送结果,而无需实际呈现页面。

并举例说明这是如何工作的:

checkGender();
var totalPart= genderPart; 

sendTotalPart(totalPart);

function sendTotalPart(totalPart) {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
         //document.getElementById("demo").innerHTML = xhttp.responseText;
        }
      };
      xhttp.open("GET", "YourPHPPage.php?totalPart="+ totalPart, true);
      xhttp.send();
    }

【讨论】:

    【解决方案3】:

    由 OP 上的 cmets 提供:

    1º要显示totalPart的信息。

    为此,您只需在表单中创建一个输入,并将其设为只读以禁止用户编辑。

    <tr>
        <td align="right"><b>Total Parts:</b></td>
        <td><input id="totalPart" type="text" name="totalPart" size="60" readonly/></td>
    </tr>
    

    你需要给它设置ID属性,所以你可以像这样通过document.getElementById来设置它的值:

    var totalPart= genderPart; 
    document.getElementById('totalPart').value = totalPart;
    

    看到这个帖子:Set the value of an input field

    2º你想在表单提交时发送信息。

    这里没什么好做的,因为totalPart已经是你表单的输入,表单提交的时候自然会发送给PHP。

    【讨论】:

    • 感谢您的帮助!我已经运行了这段代码,但它不显示总部分。没关系,我已经找到答案了。再次感谢^_^v
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2017-04-09
    • 2022-10-14
    • 2023-04-02
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多