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

Posted

技术标签:

【中文标题】如何使用 xampp 将单选按钮值提交到数据库【英文标题】:how to submit radio button values to database using xampp 【发布时间】:2018-03-24 00:51:31 【问题描述】:

我正在尝试将已选择的单选按钮值提交到数据库,但我无法这样做。 谁能帮帮我?

我浏览了 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>

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

请帮忙。

【问题讨论】:

XAMPP 并没有真正解决您的问题。顺便说一句,请停止使用 mysql_* 函数,它们已被弃用。请改用mysqli_*PDO_*。另外,在bobby-tables.com 了解 SQL 注入 - 现在,您的代码非常容易受到攻击,根本无法保存。您的数据库可能会在几秒钟内被黑客入侵,而无需深入了解您的系统。另外,请了解GETPOST方法的区别 【参考方案1】:
 if(isset($_REQUEST['g']))  $gender=$_REQUEST['g']; else $gender=""; 

【讨论】:

【参考方案2】:

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

 <input type="radio" name="rdb" value="male"/>

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

  $rdb_value = $_POST['rdb'];

【讨论】:

【参考方案3】:

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

并且 当您包含它时,它将是一种将数据传递到服务器的 GET 方法,因此请使用 $_GET 而不是 $_POST。

$gender = $_GET["g"];

【讨论】:

以上是关于如何使用 xampp 将单选按钮值提交到数据库的主要内容,如果未能解决你的问题,请参考以下文章

Anylogic:如何将单选按钮链接到 double 类型的变量?

将单选按钮数据发送到下一个活动

如何从 PHP 中的单选按钮发送值属性 [关闭]

将单选按钮关联到各个组

如何将单选按钮值与输入字段相关联

为啥jQuery将单选按钮的值返回为“ON”?如何纠正它?