我真的无法在网络上的任何地方找到解决方案,因为我的问题有点独特。
我制作了两个单选按钮,分区和外部。 单击时,“分区”单选按钮显示下拉列表,“外部”按钮显示输入文本框。
这是我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>   <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>这是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(); } ?>外部单选按钮和输入文本框正常工作,因为两个值都成功插入数据库中。 但是对于除法 ,它只插入值Division。
我做错了什么?
I really can't find a solution anywhere on the web because my problem is a bit unique.
I am made two radio buttons, Division and External. When it is clicked, the Division radio button displays drop down list and the External one displays an input text box.
This is the code I Html made.
<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>   <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>This is the php code.
<?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(); } ?>The external radio button and the input text box is working fine as both of the value successfully inserted in the database. But for the division, it inserts only the value Division.
What is it that I do wrong?
最满意答案
尝试这个 :
我已将类型的条件放在代码的开头并更改下拉列表的名称。
你的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(); } ?>您的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_details1" 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>   <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>Try this :
I have put the condition for type in the start of the code and change the name of the dropdown.
your php code
<?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(); } ?>Your HTML code
<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_details1" 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>   <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>更多推荐
发布评论