第二次尝试。我的最后一个问题不是很清楚,所以我想尝试再次寻求帮助..我已经粘贴了我的完整代码,这样你们就可以看到我的代码看起来像什么以及问题出在哪里。
所以我正在建立一个基本的会员网站。人们可以在signup.php页面上注册。到目前为止,这完美地工作了。今天我想使用复选框添加一些选项。这些复选框都需要发布到我的表中的列,其格式为ENUM('0','1'), NOT NULL and DEFAULT value is 0
。
复选框是id '直升机' ($ heli),' fixedwing' ($ fw),'商业' ($ com),'滑翔机' (滑翔机),'气球' ($ bl),'超轻型' ($ ulm),' paraglider' ($ para),'水上飞机' ($ sp),' pilot' ($ pilot)和' terms' ($计)
注册页面使用AJAX,PHP和MYSQL来获取,验证表单值并将其发布到数据库。所有值都正确地发布到数据库,但所有复选框输入都将值1发布到数据库,即使它们未被单击也是如此。因此,无论我是否检查,它都会在提交表单后将值1发布到数据库。
我对PHP和MYSQL有基本的了解,但几乎不了解AJAX和JAVA,所以我很难找到代码的错误。
总结一下我的问题:我的正常表单值会正确发布到数据库,但是复选框都会发布值1,无论是否已选中。
提前谢谢!
<?php
session_start();
// If user is logged in, header them away
if(isset($_SESSION["username"])){
header("location: user.php?u=".$_SESSION["username"]);
exit();
}
?><?php
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["usernamecheck"])){
include_once("php_includes/db_conx.php");
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']);
$sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$uname_check = mysqli_num_rows($query);
if (strlen($username) < 3 || strlen($username) > 16) {
echo 'Your username should be 3-16 characters long..';
exit();
}
if (is_numeric($username[0])) {
echo 'Usernames must begin with a letter..';
exit();
}
if ($uname_check < 1) {
echo $username . ' is available!';
exit();
} else {
echo $username . ' is taken..';
exit();
}
}
?><?php
// Ajax calls this REGISTRATION code to execute
if(isset($_POST["u"])){
include_once("php_includes/db_conx.php");
$u = preg_replace('#[^a-z0-9]#i', '', $_POST['u']);
$e = mysqli_real_escape_string($db_conx, $_POST['e']);
$p = $_POST['p'];
$c = preg_replace('#[^a-z ]#i', '', $_POST['c']);
$s = mysqli_real_escape_string($db_conx, $_POST['s']);
$b = mysqli_real_escape_string($db_conx, $_POST['b']);
$ex = mysqli_real_escape_string($db_conx, $_POST['ex']);
$w = mysqli_real_escape_string($db_conx, $_POST['w']);
$bio = mysqli_real_escape_string($db_conx, $_POST['bio']);
$heli = $_POST['heli'];
$fw = $_POST['fw'];
$com = $_POST['com'];
$sp = $_POST['sp'];
$glider = $_POST['glider'];
$ulm = $_POST['ulm'];
$para = $_POST['para'];
$bl = $_POST['bl'];
$pilot = $_POST['pilot'];
$terms = $_POST['terms'];
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
$sql = "SELECT id FROM users WHERE username='$u' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$u_check = mysqli_num_rows($query);
// -------------------------------------------
$sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$e_check = mysqli_num_rows($query);
//--------------------------------------------
//at least one of the following must be clicked
$checks = $heli + $fw + $com + $sp + $glider + $ulm + $para + $bl;
// FORM DATA ERROR HANDLING
if($u == "" || $e == "" || $p == "" || $c == "" || $s == "" || $ex == "" || $b == ""){
echo 'You have not completed the form yet..';
exit();
} else if ($u_check > 0){
echo 'The username you entered is already taken..';
exit();
} else if ($e_check > 0){
echo 'That email adress is already in use in our system..';
exit();
} else if (strlen($u) < 3 || strlen($u) > 16) {
echo 'Usernames must be between 3 and 16 characters..';
exit();
} else if (is_numeric($u[0])) {
echo 'Usernames can not begin with a number..';
exit();
} else if($checks < 1) {
echo 'Please select at least one licence..';
exit();
} else if(isset($pilot)) {
echo 'Please verify that you are a real-life pilot..';
exit();
} else if(isset($terms)) {
echo 'Please indicate that you have read our terms of use..';
exit();
} else {
$p_hash = md5($p);
$sql = "INSERT INTO users (username, email, password, country, website, bio, flighttime, bday, sex, helicopter, glider, fixedwing, seaplane, commercial, balloon, ultralight, paraglider, pilot, terms, ip, signup, lastlogin, notescheck)
VALUES('$u','$e','$p_hash','$c','$w','$bio','$ex','$b','$s','$heli','$glider','$fw','$sp','$com','$bl','$ulm','$para','$pilot','$terms','$ip',now(),now(),now())";
$query = mysqli_query($db_conx, $sql);
$uid = mysqli_insert_id($db_conx);
// Create directory(folder) to hold each user's files(pics, MP3s, etc.)
if (!file_exists("user/$u")) {
mkdir("user/$u", 0755);
}
echo "signup_success";
exit();
}
exit();
}
?>
<html>
<head>
<script>
function restrict(elem){
var tf = _(elem);
var rx = new RegExp;
if(elem == "email"){
rx = /[' "]/gi;
} else if(elem == "username"){
rx = /[^a-z0-9]/gi;
}
tf.value = tf.value.replace(rx, "");
}
function emptyElement(x){
_(x).innerHTML = "";
}
function checkusername(){
var u = _("username").value;
if(u != ""){
_("unamestatus").innerHTML = 'Checking...';
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
_("unamestatus").innerHTML = ajax.responseText;
}
}
ajax.send("usernamecheck="+u);
}
}
function signup(){
var u = _("username").value;
var e = _("email").value;
var p1 = _("pass1").value;
var p2 = _("pass2").value;
var c = _("country").value;
var s = _("sex").value;
var b = _("bday").value;
var ex = _("experience").value;
var w = _("website").value;
var bio = _("bio").value;
var heli = _("helicopter").value;
var fw = _("fixedwing").value;
var com = _("commercial").value;
var sp = _("seaplane").value;
var glider = _("glider").value;
var ulm = _("ultralight").value;
var para = _("paraglider").value;
var bl = _("balloon").value;
var pilot = _("pilot").value;
var terms = _("terms").value;
var status = _("status");
if(u == "" || e == "" || p1 == "" || p2 == "" || c == "" || s == "" || ex == "" || b == ""){
status.innerHTML = 'Please fill out all the form data..';
} else if(p1 != p2){
status.innerHTML = 'Your password fields do not match..';
} else {
_("signupbtn").style.display = "none";
status.innerHTML = 'Please wait...';
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText != "signup_success"){
status.innerHTML = ajax.responseText;
_("signupbtn").style.display = "block";
} else {
window.scrollTo(0,0);
_("msform").innerHTML = "Thanks "+u+"! Check your email! ";
} ""
}
}
ajax.send("u="+u+"&e="+e+"&p="+p1+"&c="+c+"&s="+s+"&b="+b+"&ex="+ex+"&w="+w+"&bio="+bio+"&heli="+heli+"&fw="+fw+"&com="+com+"&sp="+sp+"&glider="+glider+"&ulm="+ulm+"¶="+para+"&bl="+bl+"&pilot="+pilot+"&terms="+terms);
}
}
</script>
</head>
<body>
<form name="signupform" id="msform" onsubmit="return false;">
<input type="text" id="username" onblur="checkusername()" onkeyup="restrict('username')" maxlength="16" placeholder="Username" />
<span style="width: 100%; margin-left:0" id="unamestatus"></span>
<input style="margin-top:-20px" type="text" id="email" placeholder="Email" onfocus="emptyElement('status')" onkeyup="restrict('email')" maxlength="88"/>
<input type="password" id="pass1" placeholder="Password" onfocus="emptyElement('status')" maxlength="16" />
<input type="password" id="pass2" placeholder="Confirm password" onfocus="emptyElement('status')" maxlength="16"/>
<select id="country" onfocus="emptyElement('status')">
<?php include_once("template_country_list.php"); ?>
</select>
<textarea id="bio" onfocus="emptyElement('status')" placeholder="Bio"></textarea>
<input type="text" onfocus="emptyElement('status')" id="website" placeholder="Website (optional)" />
<input type="checkbox" id="helicopter" name="helicopter" onfocus="emptyElement('status')" value="1"> <span>Helicopter</span><br>
<input type="checkbox" id="fixedwing" name="fixedwing" onfocus="emptyElement('status')" value="1" > <span>Fixed Wing</span><br>
<input type="checkbox" id="commercial" name="commercial" onfocus="emptyElement('status')" value="1" > <span>Commercial</span><br>
<input type="checkbox" id="seaplane" name="seaplane" onfocus="emptyElement('status')" value="1" > <span>Seaplane</span><br>
<input type="checkbox" id="glider" name="glider" onfocus="emptyElement('status')" value="1" > <span>Glider</span> <br>
<input type="checkbox" id="paraglider" name="paraglider" onfocus="emptyElement('status')" value="1" > <span>Para</span><br>
<input type="checkbox" id="balloon" name="balloon" onfocus="emptyElement('status')" value="1" > <span>Balloon</span><br>
<input type="checkbox" id="ultralight" name="ultralight" onfocus="emptyElement('status')"value="1"> <span>Ultralight</span>
<select onfocus="emptyElement('status')" id="experience">
<option value="0-500">0-500 hours</option>
<option value="500-1000">500-1000 hours</option>
<option value="1000-5000">1000-5000 hours</option>
<option value="5000+">5000+ hours</option>
</select>
<select onfocus="emptyElement('status')" id="sex" >
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="date" id="bday" onfocus="emptyElement('status')">
<input type="checkbox" id="pilot" value="1" > <span>I am a real-life pilot</span>
<input type="checkbox" id="terms" value="1" > <span>I accept the <a target="_blank" href="legal.php"><b>the terms of use</b></a></span>
<input type="submit" class="submit action-button" id="signupbtn" onclick="signup()" value="Sign up!" />
<br><div id="status"></div>
</fieldset>
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
在PHP中处理复选框的方法是......
如果您愿意,可以给他们值,但没有必要。验证php中的复选框只有一种方法。这是正确的方法。
// This checks to see if the checkbox is "checked"
if(isset($_POST['checkbox_name'])){
// Here you can set the value manually
$checkbox_name = 1;
// Or you can set it to the checkbox value
$checkbox_name = $_POST['checkbox_name'];
}else{
// Here is where you can set the value if it is NOT "checked"
$checkbox_name = 0;
}
在javascript方面你可以做到这一点......
// To set the value manually based on being checked or not
var heli = $("#helicopter").is(':checked') ? 1 : 0;
// Or get the value if checked
var heli = $("#helicopter").is(':checked') ? $("#helicopter").val() : 0;
答案 1 :(得分:0)
尝试查看复选框是否已选中而不是获取其值:
var heli = document.getElementById('helicopter').checked;
如果你想存储价值:
var heli = document.getElementById('helicopter').checked ? document.getElementById('helicopter').value : 0;
答案 2 :(得分:0)
您可以将元素ID传递给函数,然后使用它:
document.getElementById(this).click();
它将切换复选框&#34;选中&#34;属性。然后使用VIDesignz的解决方案作为PHP部分。