gpt4 book ai didi

javascript - AJAX按钮: Male vs Female - Need 3rd choice (both)

转载 作者:行者123 更新时间:2023-11-29 13:17:51 24 4
gpt4 key购买 nike

我正在学习如何使用 AJAX,并使用教程中的脚本来显示存储在数据库表中的人员姓名。数据库表包含一个名为“性别”的字段。有两个可能的值 - M 和 F。

然后,页面会显示一个按钮,您可以在 M 和 F 之间切换,仅显示男性或女性。

Sex: <select id='Gender'>
<option value="M">M</option>
<option value="F">F</option>
</select>
<input type='button' onclick='ajaxFunction()'
value='Query MySQL'/>
</form>

有没有办法修改它,以便我可以提供第三种选择 - 显示所有人?我正在考虑这个想法,但我不知道如何使“M 和 F”成为选项值。

Sex: <select id='Gender'>
<option value="M">M</option>
<option value="F">F</option>
<option value="M and F">All</option>
</select>
<input type='button' onclick='ajaxFunction()'
value='Query MySQL'/>
</form>

我正在使用 PHP 和 MySQL。

<小时/>

这是第一页的代码:

<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}

ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var Gender = document.getElementById('Gender').value;
var Birth_Year = document.getElementById('Birth_Year').value;
var Died = document.getElementById('Died').value;
var queryString = "?Birth_Year=" + Birth_Year ;
queryString += "&Died=" + Died + "&Gender=" + Gender;
ajaxRequest.open("GET", "AjaxPeople2.php" +
queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Born Before: <input type='text' id='Birth_Year'> <br>
Died Before: <input type='text' id='Died'>
<br>
Sex: <select id='Gender'>
<option value="M">M</option>
<option value="F">F</option>
<option value="M_F">All</option>
</select>
<input type='button' onclick='ajaxFunction()'
value='Query MySQL'/>

<input type="radio" name="Men" value="M">M<br>
<input type="radio" name="Women" value="F">F<br>
<input type="radio" name="All" value="M_F">All<br>

</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>
<小时/>

第二页的代码...

$dsn = "mysql:host=localhost;dbname=db_new;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);

$pdo = new PDO($dsn,'(Username)','(Password)', $opt);

// Retrieve data from Query String
$Common = $_GET['Common'];
$Birth_Year = $_GET['Birth_Year'];
$Died = $_GET['Died'];
$Gender = $_GET['Gender'];
$Birth_ID = $_GET['Birth_ID'];
$Death_ID = $_GET['Death_ID'];

$sql= "SELECT * FROM people_bios PB
LEFT JOIN people P ON P.URL = PB.URL
WHERE Gender = :Gender AND Site = 'PX'";
if(is_numeric($Birth_Year)) {
$sql .= " AND Birth_Year <= :Birth_Year";
}
if(is_numeric($Died)) {
$sql .= " AND Died <= :Died";
}
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':Gender',$Gender,PDO::PARAM_STR);
if (is_numeric($Birth_Year)) {
$stmt->bindParam(':Birth_Year', $Birth_Year, PDO::PARAM_INT);
}
if(is_numeric($Died)) {
$stmt->bindParam(':Died', $Died, PDO::PARAM_INT);
}
$stmt->execute();

//Execute query
try {
$stmt->execute();
} catch (Exception $e) {
// print_r($e); // Do something more useful here, like log.
}

//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Born</th>";
$display_string .= "<th>Died</th>";
$display_string .= "<th>Birth Place</th>";
$display_string .= "<th>Death Place</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while ($row = $stmt->fetch())
{
$display_string .= "<tr>";
$display_string .= "<td>$row[Common]</td>";
$display_string .= "<td>$row[Birth_Year]</td>";
$display_string .= "<td>$row[Died]</td>";
$display_string .= "<td>$row[Birth_ID]</td>";
$display_string .= "<td>$row[Death_ID]</td>";
$display_string .= "</tr>";
}

echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;

最佳答案

“M_F”或什么都没有,给你一些引用

$where_gender = preg_match('{^[MF]$}',$Gender)? 'AND Gender = :Gender' : '';


$sql= "SELECT * FROM people_bios PB
LEFT JOIN people P ON P.URL = PB.URL
WHERE Site = 'PX' {$where_gender}";

...


if ($where_gender) {
$stmt->bindParam(':Gender',$Gender,PDO::PARAM_STR);
}

关于javascript - AJAX按钮: Male vs Female - Need 3rd choice (both),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21246239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com