В моей регистрационной форме все поля работают нормально, но поле деревни возвращает 0 в базе данных.
Я использовал ajax и jquery, чтобы создать список для губернатора, района и деревни.
Первые 2 работы отлично, но деревня ничего не вставляет
Я не знал, какой фрагмент кода отображать, чтобы люди поняли меня, поэтому я отобразил весь код
Это код
<?php require_once('for members/scripts/connect.php'); ?> <?php function specializationQuery(){ $specData = mysql_query("SELECT * FROM specialization"); while($recordJob = mysql_fetch_array($specData)){ echo'<option value="' . $recordJob['specialization_id'] . '">' . $recordJob['specialization_name'] . '</option>'; } } //default value $message = "Fields Marcked with an [*] are Required"; $username = ""; $fname = ""; $lname = ""; $specialization = ""; $email = ""; $pass1 = ""; $pass2 = ""; $governorate=""; $district = ""; $village = ""; if(isset($_POST['username'])){ $username = mysql_real_escape_string($_POST['username']); $fname = mysql_real_escape_string($_POST['fname']); $lname = mysql_real_escape_string($_POST['lname']); $email = mysql_real_escape_string($_POST['email']); $pass1 = mysql_real_escape_string($_POST['pass1']); $pass2 = mysql_real_escape_string($_POST['pass2']); $bdate = mysql_real_escape_string($_POST['birthdate']); $specialization = mysql_real_escape_string($_POST['specialization']); $governorate = mysql_real_escape_string($_POST['governorate']); $district = mysql_real_escape_string($_POST['district']); $village = mysql_real_escape_string($_POST['village']); //error handeling if((!$username)||(!$fname)||(!$lname)||(!$email)||(!$pass1)||(!$pass2)||(!$specialization)||(!$governorate)||(!$district)||(!$village)){ $message = "**** Please insert the Required Fields below ****<br />"; if($fname == "") { $message = $message . "Enter First name<br/>"; } if($lname == "") { $message = $message . "Enter Last name<br/>"; } if( $specialization == 0) { $message = $message . "Select Your Job<br />"; } if($governorate == 0) { $message = $message . "Select Your Governorate<br />"; } if($district == 0) { $message = $message . "Select Your District<br />"; } if($village == "") { $message = $message . "Select Your Village<br />"; } if($email == "") { $message = $message . "Enter Email Adress<br/>"; } if ($username == "") { $message = $message . "Enter User Name<br/>"; } if($pass1 == "") { $message = $message . "Enter password<br/>"; } if($pass2 == "") { $message = $message . "rechek the password <br/>"; } } elseif(strlen($pass1) <= 8) { $message = $message . "Your Password must be at least 8 charachters<br />"; } else if($pass1!=$pass2){ $message = "your password do not match!"; } else { //securing the data $username = preg_replace("#[^0-9a-z]#i","",$username); $fname = preg_replace("#[^0-9a-z]#i","",$fname); $lname = preg_replace("#[^0-9a-z]#i","",$lname); //$pass1 = sha1($pass1); $email = mysql_real_escape_string($email); // checking for duplicate $user_query = mysql_query("SELECT user_name FROM user WHERE user_name = '$username'LIMIT 1") or die("could not check the username"); $count_username = mysql_num_rows($user_query); $email_query = mysql_query("SELECT email_address FROM user WHERE email_address = '$email'LIMIT 1") or die("could not check the email"); $count_email = mysql_num_rows($email_query); if($count_username > 0){ $message = " your username is alredy in use"; }elseif($count_email > 0){ $message = "your email is alredy in use"; } else{ $query = mysql_query("INSERT INTO user(user_name, first_name, last_name, governorate, district, village, birth_date, email_address, specialization, password, registered_date)VALUES('$username', '$fname', '$lname', '$governorate', '$district', '$village', '$bdate', '$email', '$specialization', '$pass1', now())")or die("could not insert data"); //var_dump($query); $message = "you have now been registered"; //from the social website if ($query) { $_SESSION['user_id'] = mysql_insert_id(); $_SESSION['login'] = 'true'; $_SESSION['login_user'] = $username; } /* $getid = mysql_fetch_assoc($query); //$_SESSION['user_id'] = $getid['user_rid']; $_SESSION['user_id'] =mysql_insert_id(); $_SESSION['login'] = 'true'; $_SESSION['login_user'] = $getid['username']; */ //header("Location: home.php"); } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Register Page</title> <link href="style/stylesheet.css" rel="stylesheet" type="text/css" /> <link href="style/imagesGallery.css"rel="stylesheet" type="text/css"/> <script src="js/main.js" type="text/javascript"></script> <script src="js/jquery.js" type="text/javascript"></script> <script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("select#district").attr("disabled","disabled"); $("select#village").attr("disabled","disabled"); $("select#governorate").change(function(){ $("select#district").attr("disabled","disabled"); $("select#district").html("<option>wait...</option>"); var id = $("select#governorate option:selected").attr('value'); $.post("select_district.php", {id:id}, function(data){ $("select#district").removeAttr("disabled"); $("select#district").html(data); }); }); $("select#district").change(function(){ id = $(this).val(); $("select#village").attr("disabled","disabled"); $("select#village").html("<option>wait...</option>"); $.post("select_village.php", {id:id}, function(data){ $("select#village").removeAttr("disabled"); $("select#village").html(data); }); }); $("form#registerform").submit(function(){ var cat = $("select#governorate option:selected").attr('value'); var type = $("select#district option:selected").attr('value'); var village = $("select#village option:selected").attr('value'); }); }); </script> </head> <body> <img src="web_header copy.jpg" alt="visitor header" width="1080" height="128" /> <marquee direction="left" width="99%" behavior="alternate"> <img src="imggallery/images/akoura/akoura_small.jpg" width="119" height="91"> <img src="imggallery/images/ballaa2/ballaa2_small.jpg" width="119" height="91"> <img src="imggallery/images/baalbeck/baalbek_small.jpg" height="92"> <img src="imggallery/images/barouk/cedre_barouk_small.jpg" width="119" height="91"> <img src="imggallery/images/batroun/batroun_small.jpg" width="119" height="91"> <img src="imggallery/images/bchareh/bchareh_small.jpg" width="119" height="91"> <img src="imggallery/images/beiteldin/beiteldine_small.jpg" width="119" height="91"> <img src="imggallery/images/beyrouth/beyrouth_small.jpg" width="119" height="91"> <img src="imggallery/images/beyrouth/beyroyj frm sky/beyrouthfromSky_small.jpg" width="119" height="91"> <img src="imggallery/images/deir el mara/deirelamar_small.jpg" width="119" height="91"> </marquee> <hr /> <h2 class="registerTitle">Registration Fields</h2> <h4 class="registerTitle">Sign Up Today....</h4> <div class="container center"> <br /> <br /><br /><br /><br /><br /><br /> <br /><br /><br /><br /><br /><br /> <br /><br /><br /><br /><br /> <p style="color:#FF0000" align="center"><?php print("$message")?></p> <?php include "select.class.php"; ?> <form id="registerform" action="register.php" method="post"> first name <input type="text" name="fname" placeholder="Firstname" /> last name <input type="text" name="lname" placeholder="Lastname" /> <br /> <br /> Date of Birth <input type="date" name="birthdate" value= "YYYY_MM_DD" onfocus="if (this.value == 'YYYY_MM_DD') {this.value = '';}" onblur="if (this.value == '') {this.value = 'YYYY_MM_DD';}" /> Specialization: <select name="specialization" class="select"> <option value="0">-- Select Your Specialization --</option> <?php specializationQuery(); ?> </select> <br /> <br /> Governorate <select id="governorate" name = 'governorate'> <?php echo $opt->ShowGovernorate(); ?> </select> District <select id="district" name="district"> <option value="0">choose...</option> </select> Village <select id="village" name="village"> <option value="0">choose...</option> </select> <br /> <br /> Email: <input type="text" name="email" placeholder="Email Adress" /> Username: <input type="text" name="username" placeholder="Username" /> <br /><br /> Password: <input type="password" name="pass1" placeholder="Password" /> Re_Password: <input type="password" name="pass2" placeholder="Validate Password" /> <br /><br /> <input type="submit" value="Register"/> </form> </div> </body> </html>
@ user2326255 Проверьте, что вы выбрали деревню из выпадающего списка. В html вы выбрали опцию 0. Если пользователь не выбирает деревню, то значение по умолчанию будет отправлено. Изменить код
if($village == "") { $message = $message . "Select Your Village<br />"; }
к
if($village == '0') { $message = $message . "Select Your Village<br />"; }
Если пользователь не выбирает деревню, мы должны сообщить пользователю, что выберите деревню. Приведенное выше изменение кода вызовет ошибку пользователя.