Javascript PHP下拉列表未插入数据库中的选定值

Javascript PHP下拉列表未插入数据库中的选定值,javascript,php,html,mysqli,Javascript,Php,Html,Mysqli,我真的无法在网络上找到解决方案,因为我的问题有点独特 我有两个单选按钮,分区和外部。单击该按钮时,分区单选按钮显示下拉列表,外部单选按钮显示输入文本框 这是我用Html编写的代码 <html> <head> <title> Submit a Contract </title> <meta charset="UTF-8"> <meta name="viewport" content="width=device

我真的无法在网络上找到解决方案,因为我的问题有点独特

我有两个单选按钮,分区和外部。单击该按钮时,分区单选按钮显示下拉列表,外部单选按钮显示输入文本框

这是我用Html编写的代码

<html>
<head>
    <title> Submit a Contract </title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">



<script type="text/javascript">
function ShowHideDiv() {
    var client1 = document.getElementById("client1");
    var client2 = document.getElementById("client2");
    var division = document.getElementById("division");
    var external = document.getElementById("external");

    division.style.display = client1.checked ? "block" : "none";
    external.style.display = client2.checked ? "block" : "none";
}

function showfield(name){
if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="client_details" value="" />';
 else document.getElementById('div1').innerHTML='';
}
</script>
</head>

<body>
<form method="post" action="" enctype="multipart/form-data">

<label for = "client1">
        <input type="radio" name="client_type" id = "client1" value="Division" onclick="ShowHideDiv()"/> Division
    </label>
    <label for ="client2">
        <input type="radio" name="client_type" id = "client2" value="External" onclick="ShowHideDiv()"/> External
    </label>    
    <br><br>
<div id="division" style="display:none">
    Division:
        <select name="client_details" onchange="showfield(this.options[this.selectedIndex].value)">
        <option value="">Choose Division...</option>
        <option value="Distribution">Distribution</option>
        <option value="Transmission">Transmission</option>
        <option value="Generation">Generation</option>
        <option value="Procument">Procument</option>
        <option value="Other">Others</option>
        </select>   
        <br><br>
        <div id="div1"></div>
    </div>

    &nbsp

    <div id="external" style="display:none">
    External:
    <input type="text" name="client_details" value=""/> 
    </div>
    <br><br>    
    <input type="submit" name="submit" value="Submit"/>
    </form>     
</body>

</html>

提交合同
函数ShowHideDiv(){
var client1=document.getElementById(“client1”);
var client2=document.getElementById(“client2”);
var division=document.getElementById(“division”);
var external=document.getElementById(“外部”);
division.style.display=client1.checked?“块”:“无”;
external.style.display=client2.checked?“块”:“无”;
}
函数显示字段(名称){
if(name='Other')document.getElementById('div1')。innerHTML='';
else document.getElementById('div1')。innerHTML='';
}
分开
外部的


分部: 选择部门。。。 分配 传输 一代 采购 其他

  外部:

这是php代码

<?php
require("config.php");

if(isset($_POST['submit']))
{
$client_type = isset($_POST ['client_type']) ? $_POST['client_type'] :null;
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;

$sql = "SELECT * FROM contracts";
$query = "INSERT INTO contracts
(
`client_type`,
`client_details`) VALUES (
                                '$client_type',
                                '$client_details')" ;

if ($con->query($query) === TRUE) 
{
echo "<br><br> New record created successfully";

echo $query;


} 
else {
echo "Error: " . $query . "<br>" . $con->error;
}

$con->close(); 
}
?>
试试这个:

我在代码的开头添加了type的条件,并更改了下拉列表的名称

您的php代码

<?php
require("config.php");
if(isset($_POST['submit']))
{
echo $client_type = isset($_POST ['client_type']) ? $_POST['client_type'] :null;
if($client_type == 'Division'){
$client_details = isset($_POST ['client_details1']) ? $_POST['client_details1'] :null;

}
else{
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;

}

echo $query = "INSERT INTO contracts
(
`client_type`,
`client_details`) VALUES (
                                '$client_type',
                                '$client_details')" ;
if ($con->query($query) === TRUE) 
{
echo "<br><br> New record created successfully";

echo $query;


} 
else {
echo "Error: " . $query . "<br>" . $con->error;
}

$con->close();                                 
}

?>

prakash tank的版本是正确的,甚至可能更好。我发布这个解决方案是为了让你们知道还有其他的方法,我在笔记中向你们解释了你们做错了什么

 <html>
        <head>
            <title> Submit a Contract </title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">



        <script type="text/javascript">
        function ShowHideDiv() {
            var client1 = document.getElementById("client1");
            var client2 = document.getElementById("client2");
            var division = document.getElementById("division");
            var external = document.getElementById("external");

            division.style.display = client1.checked ? "block" : "none";
            external.style.display = client2.checked ? "block" : "none";
        }

        function showfield(name){
        if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="client_details" value="" />';
         else document.getElementById('div1').innerHTML='';
        }
        </script>
        </head>

        <body>

        <form method="post" action="" enctype="multipart/form-data">

        <label for = "client1">
                <input type="radio" name="client_type" id = "client1" value="Division" onclick="ShowHideDiv()"/> Division
            </label>
            <label for ="client2">
                <input type="radio" name="client_type" id = "client2" value="External" onclick="ShowHideDiv()"/> External
            </label>    
            <br><br>
        <div id="division" style="display:none">
            Division:
            <!-- client_details has to be changed to client_detailsDiv as after clicking "submit" data from Division and External were put in to the same variable "client_details" so even if External was empty PHP though that it's suppose to be empty and as long as external
            DO NOT MIX TWO TYPES OF FORM TYPES AS ONE VARIABLE = DON'T GIVE THEM THE SAME name - in other words you can't give same name for SELECT and TEXT and so on.-->
                <select name="client_detailsDiv" onchange="showfield(this.options[this.selectedIndex].value)">
                <option value="">Choose Division...</option>
                <option value="Distribution">Distribution</option>
                <option value="Transmission">Transmission</option>
                <option value="Generation">Generation</option>
                <option value="Procument">Procument</option>
                <option value="Other">Others</option>
                </select>   
                <br><br>
                <div id="div1"></div>
            </div>

            &nbsp

            <div id="external" style="display:none">
            External:
            <!--client_details has to be changed to  client_detailsExt for reason described above-->
            <input type="text" name="client_detailsExt" value=""/> 
            </div>
            <br><br>    
            <input type="submit" name="submit" value="Submit"/>
            </form>     
        </body>

        </html>


        <?php
        require("config.php");

        if(isset($_POST['submit']))
        {
        $client_type = isset($_POST ['client_type']) ? $_POST['client_type'] :null;
        /*
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
        above functions was wrong as I already explained it got $_POST['client_type'] for the External - PHP works the way that it gets variable from the first $_POST['same_name'] and then from second $_POST['same_name'] and then overwrites value of first $_POST['same_name'] and for this reason it displayed only value for External and none for Division as when user set External, Division was empty. So if you would switch form for External as first and put form for Division as second then you wouldn't get variable from External but from Division.
        $client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;

        so what we need to do is to to put this code:
        */
        if(!empty($_POST['client_detailsDiv'])) {$client_details = $_POST['client_detailsDiv'] ? $_POST['client_detailsDiv'] :null;}
        else {$client_details = $_POST['client_detailsExt'] ? $_POST['client_detailsExt'] :null;}
        // at fist we make sure that client_detailsDiv is not empty, if it's not than we set client_detailsDiv as $client_details. But if it's empty we use else statement to 

        $sql = "SELECT * FROM contracts";
        $query = "INSERT INTO contracts
        (
        `client_type`,
        `client_details`) VALUES (
                                        '$client_type',
                                        '$client_details')" ;

        if ($con->query($query) === TRUE) 
        {
        echo "<br><br> New record created successfully";

        echo $query;


        } 
        else {
        echo "Error: " . $query . "<br>" . $con->error;
        }

        $con->close(); 
        }
        ?>

提交合同
函数ShowHideDiv(){
var client1=document.getElementById(“client1”);
var client2=document.getElementById(“client2”);
var division=document.getElementById(“division”);
var external=document.getElementById(“外部”);
division.style.display=client1.checked?“块”:“无”;
external.style.display=client2.checked?“块”:“无”;
}
函数显示字段(名称){
if(name='Other')document.getElementById('div1')。innerHTML='';
else document.getElementById('div1')。innerHTML='';
}
分开
外部的


分部: 选择部门。。。 分配 传输 一代 采购 其他

  外部:


您也为该
文本框指定了相同的名称。因此,请更改名称并再次检查。我之前已经这样做了,但仍然给出了相同的结果。只需重命名
name=“client\u details”
并再次检查您在php文件print\r($\u POST)中得到了什么;模具();然后您可以检查哪个部分的问题仍然是相同的问题@prakashtanki认为这很好,如果你想改变客户端的价值,那么也会感谢@Prakashtank和其他人的帮助!经过这么长时间,它终于起作用了!:)谢谢@emil的解释!
 <html>
        <head>
            <title> Submit a Contract </title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">



        <script type="text/javascript">
        function ShowHideDiv() {
            var client1 = document.getElementById("client1");
            var client2 = document.getElementById("client2");
            var division = document.getElementById("division");
            var external = document.getElementById("external");

            division.style.display = client1.checked ? "block" : "none";
            external.style.display = client2.checked ? "block" : "none";
        }

        function showfield(name){
        if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="client_details" value="" />';
         else document.getElementById('div1').innerHTML='';
        }
        </script>
        </head>

        <body>

        <form method="post" action="" enctype="multipart/form-data">

        <label for = "client1">
                <input type="radio" name="client_type" id = "client1" value="Division" onclick="ShowHideDiv()"/> Division
            </label>
            <label for ="client2">
                <input type="radio" name="client_type" id = "client2" value="External" onclick="ShowHideDiv()"/> External
            </label>    
            <br><br>
        <div id="division" style="display:none">
            Division:
            <!-- client_details has to be changed to client_detailsDiv as after clicking "submit" data from Division and External were put in to the same variable "client_details" so even if External was empty PHP though that it's suppose to be empty and as long as external
            DO NOT MIX TWO TYPES OF FORM TYPES AS ONE VARIABLE = DON'T GIVE THEM THE SAME name - in other words you can't give same name for SELECT and TEXT and so on.-->
                <select name="client_detailsDiv" onchange="showfield(this.options[this.selectedIndex].value)">
                <option value="">Choose Division...</option>
                <option value="Distribution">Distribution</option>
                <option value="Transmission">Transmission</option>
                <option value="Generation">Generation</option>
                <option value="Procument">Procument</option>
                <option value="Other">Others</option>
                </select>   
                <br><br>
                <div id="div1"></div>
            </div>

            &nbsp

            <div id="external" style="display:none">
            External:
            <!--client_details has to be changed to  client_detailsExt for reason described above-->
            <input type="text" name="client_detailsExt" value=""/> 
            </div>
            <br><br>    
            <input type="submit" name="submit" value="Submit"/>
            </form>     
        </body>

        </html>


        <?php
        require("config.php");

        if(isset($_POST['submit']))
        {
        $client_type = isset($_POST ['client_type']) ? $_POST['client_type'] :null;
        /*
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
        above functions was wrong as I already explained it got $_POST['client_type'] for the External - PHP works the way that it gets variable from the first $_POST['same_name'] and then from second $_POST['same_name'] and then overwrites value of first $_POST['same_name'] and for this reason it displayed only value for External and none for Division as when user set External, Division was empty. So if you would switch form for External as first and put form for Division as second then you wouldn't get variable from External but from Division.
        $client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;

        so what we need to do is to to put this code:
        */
        if(!empty($_POST['client_detailsDiv'])) {$client_details = $_POST['client_detailsDiv'] ? $_POST['client_detailsDiv'] :null;}
        else {$client_details = $_POST['client_detailsExt'] ? $_POST['client_detailsExt'] :null;}
        // at fist we make sure that client_detailsDiv is not empty, if it's not than we set client_detailsDiv as $client_details. But if it's empty we use else statement to 

        $sql = "SELECT * FROM contracts";
        $query = "INSERT INTO contracts
        (
        `client_type`,
        `client_details`) VALUES (
                                        '$client_type',
                                        '$client_details')" ;

        if ($con->query($query) === TRUE) 
        {
        echo "<br><br> New record created successfully";

        echo $query;


        } 
        else {
        echo "Error: " . $query . "<br>" . $con->error;
        }

        $con->close(); 
        }
        ?>