如何使用xampp向数据库提交单选按钮值

时间:2017-10-12 07:14:57

标签: javascript php html database xampp

我正在尝试将已选择的单选按钮值提交到数据库,但我无法执行此操作。 有人可以帮帮我吗?

我浏览了YouTube视频,但我还是不明白我真正需要做些什么。 起初我以为我需要在收音机按钮上添加一个ID,但后来当我浏览其他帖子时,我越来越困惑。

我成功地将表单链接到数据库,当我点击提交时它确实发布了所有输入值但是它不会将性别无线电输入放到数据库中。

请帮忙。 您的时间和帮助将受到高度赞赏!

first.html->

<!DOCTYPE html>
<html>

<head>

  <meta charset="utf-8">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Merienda" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Alegreya+SC|Merienda" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>  
  <style> 

  h1{
  color: #27292b !important;
  font-family: "Alegreya+SC";
  opacity: 0.85;


  }

  </style>


<title>Innoccura Technologies Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body style="background:url(Work.jpg);background-repeat:no-repeat;background-size:100% 120%">

<div align="center">

<form id="myForm" name="form1" action="" method="post"> 

<table >

<h1 align="center"> Innoccura Technologies Form Submission </h1>
    <tr>
        <td style="padding-bottom:20px"></td>

    </tr>
    <tr>
        <td style="padding-bottom:20px"></td>

    </tr>
    <tr>
    <div class="form-group">
    <div class="input-group">
        <td style=color:white;><b>First Name:</b></td>
        <td><input type="text" placeholder="Enter your first name" class="form-control" name="t1"  id="name"></td>
    </div>
    </tr>
</tr>
    <tr>
        <td style="padding-bottom:20px"></td>

    </tr>   



    <tr>

        <div class="form-group">

        <td style=color:white;><b>Last Name:</b></td>

        <td><input type="text" placeholder="Enter your last name" class="form-control" name="t0" id="lname"></td>

    </tr>
    <tr>
        <td style="padding-bottom:20px"></td>

    </tr>



    <tr>
        <td style=color:white;><b>Mobile Number:</b></td>
        <td><input type="number" placeholder="Enter your mobile no." class="form-control" name="num" id="numb"></td>
    </tr>

    <tr>
        <td style="padding-bottom:20px"></td>

    </tr>
    <tr>
        <td style=color:white;><b>gender</b></td>
        <td>  
              <input type="radio"  name="g"   value="male"> Male
              <input type="radio"  name="g"  value="female"> Female<br>
        </td>

    <tr>
    <tr>
        <td style="padding-bottom:20px"></td>

    </tr>

    <tr>
        <td style=color:white;><b>City :</b></td>
        <td>
            <select id="city" class="form-control">
              <option value="">Select</option>
              <option value="Los Angeles">Los Angeles</option>
              <option value="New York">New York</option>
              <option value="Detroit">Detroit</option>
              <option value="Ladson">Ladson</option>
            </select>
        </td>
</tr>

<tr>
        <td style="padding-bottom:20px"></td>

    </tr>


<tr>
<td style=color:white;><b>Enter Age</b></td>
<td><input type="number" class="form-control" placeholder="Enter your age" name="t3" id="t3"></td>
</tr>
<tr>
        <td style="padding-bottom:20px"></td>

    </tr>
<tr>
<td style=color:white;><b>Address:</b></td>
<td><input type="text" class="form-control" name="address" placeholder="Enter your address" id="address"></td>
</tr>
<tr>
        <td style="padding-bottom:20px"></td>

    </tr>
    <tr>
        <td style="padding-bottom:20px"></td>

    </tr>

<tr>
<td colspan=2 align="center"><input type="button" class="form-control" name="submit" value="Submit" onclick="aa();"></td>
</tr>
<tr>
        <td style="padding-bottom:20px"></td>

    </tr>

</div>
</form>



<script type="text/javascript">
function aa()
{
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","insert.php?nm="+document.getElementById("name").value+"&lt="+document.getElementById("lname").value+"&mb="+document.getElementById("numb").value+"&citi="+document.getElementById("city").value+"&age="+document.getElementById("t3").value+"&address="+document.getElementById("address").value,false);
xmlhttp.send(null);

}


</script>



</body>
</html>

<?php


if($_POST["submit"]=="submit")
{
$gender = $_POST["g"];

    mysql_connect("localhost","root","");
    mysql_select_db("test");

    $s="insert into tester(gender) values('".$gender."')";

    if(mysql_query($s))
        echo "Record Saved";
    else
        echo mysql_error();
}
?>

这是insert.php的代码

<html>
<head>
<title>Untitle Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">



</head>
<body>
<?php

$nm=$_GET["nm"];
$lastname=$_GET["lt"];
$mobileNumber=$_GET["mb"];
//$gender=$_GET["gen"];
//$gender=$_GET["gend"];

$gender = $_POST["g"];
$city=$_GET["citi"];
$age=$_GET["age"];
$address=$_GET["address"];

var_dump($nm);
var_dump($lastname);
var_dump($mobileNumber);
var_dump($gender);
var_dump($city);
var_dump($age);
var_dump($address);

mysql_connect("localhost","root","");
mysql_select_db("test");

mysql_query("INSERT INTO tester VALUES('$nm','$lastname','$mobileNumber','$gender','$city','$age','$address')");



?>
</body>

</html>

我搜索了很多,但我无法理解将单选按钮的输入发送到数据库的概念。

请帮助。

3 个答案:

答案 0 :(得分:0)

您没有在http请求的查询字符串中发布无线电g的值。这是第一个错误。

和 当你包含它时,它将是一个将数据传递给服务器的GET方法,所以使用$ _GET代替$ _POST。

$gender = $_GET["g"];

答案 1 :(得分:0)

当您选择单选按钮并单击提交按钮时,您需要使用$ _POST []处理PHP代码中任何选定值的提交。例如:如果您的单选按钮是:

  $rdb_value = $_POST['rdb'];

然后在你的PHP代码中你需要使用:

{{1}}

答案 2 :(得分:0)

 if(isset($_REQUEST['g'])) { $gender=$_REQUEST['g']; else{ $gender=""; }