Как получить значения, вставленные в базу данных по HTML < select > вариант? В моем случае это не работает

vio спросил: 27 августа 2018 в 01:19 в: php

Я хочу знать, как передать значение выбранного элемента в позиции ввода feild на другую страницу. Здесь переменная позиция пуста и не присваивается $ _POST ['position'].

addSMemberForm.php

    <!-- DB connection ---------------------------------------->
    <?php include 'includes/dbh.php';?>
<!--# end DB connection ----------------------------------->
	

   <!DOCTYPE html>
   <html>
   <head>



   <!-- Page head --------------------------------------->
   <?php include 'head.php';?>
   <!--# end Page Loader -------------------------------->
 

   </head>
	
   <body class="theme-blue">
       <!-- Page Loader ------------------------------------>
	   <?php include 'preloader.php';?>
       <!-- #END# Page Loader ------------------------------>
	
       <!-- Overlay For Sidebars --------------------------->
       <div class="overlay"></div>
    <!-- #END# Overlay For Sidebars --------------------->
	
    <!-- Search Bar ------------------------------------->
    <?php include 'searchbar.php';?>
    <!-- #END# Search Bar ------------------------------->
	
    <!-- Top Bar ---------------------------------------->
    <?php include 'topnav.php';?>
    <!-- #Top Bar --------------------------------------->
	
    <section>
        <!-- Left Sidebar -------------------------------->
        <aside id="leftsidebar" class="sidebar">
		
            <!-- User Info ---------------------->
			<?php include 'userinfo.php';?>
            <!-- #User Info ---------------------->
			
            <!-- Menu --------------------------->
			<?php include 'menu.php';?>
            <!-- #Menu -------------------------->
			
            <!-- Footer ------------------------->
            <?php include 'footer.php';?>
            <!-- #Footer ------------------------>
			
        </aside>
        <!-- #END# Left Sidebar -------------------------------->
		
        <!-- Right Sidebar ----------------------------->
        <?php include 'rightsidebar.php';?>
        <!-- #END# Right Sidebar -->
		
    </section>

	
	
    <section class="content">
	<!-- Main page content  ----------------------------->
        <div class="container-fluid">
				<div class="row clearfix js-sweetalert">
					<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
						<div class="card">
							<div class="header">
								<h2 align="center"><b>ADD A NEW STAFF MEMBER</h2></b>
							<div class = "col-sm-9"></div>
						</div>
							
						<div class = "body">
							<form method = "post" action = "includes/insertStaffQuery.php">
							<h2 class="card-inside-title">Personal Details</h2>
                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
									<div class = "form-control">ID*</div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                           <input type="text" class="form-control" name="emp_id" title="This field should contain 12 chracters. eg:SFXXXXXX123456" pattern="([S][F][A-Z][A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9])" minlength="12" maxlength="12"  placeholder="SFXXXXXX123456" required>
                                        <div class="help-info">Ex: SFXXXXXX123456</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
							
							<div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
									<div class = "form-control">
										Full name*
									</div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="name" class="form-control" name="full_name" minlength="10" required>
                                        </div>
                                    </div>
                                </div>
                            </div>
														
							<div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
									<div class = "form-control">
										Gender
									</div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                           <div class="demo-radio-button" >
                                                <input name="gender" type="radio" class="with-gap" id="radio_3" value="M" checked>
                                                <label for="radio_3">Male</label>
                                                <input name="gender" type="radio" id="radio_4" class="with-gap" value="F">
                                                <label for="radio_4">Female</label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
							<div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
									<div class = "form-control">
										National ID*
									</div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="nic" minlength="10" maxlength="10" placeholder="123456789V"required>
                                            <div class="help-info">Ex: 123456789V</div>
                                        </div>
                                    </div>
                                </div>
                            </div>	

                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                    <div class = "form-control">
                                        Date of birth*
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                             <input type="date" class="form-control" name="dob" required>
                                    </div>
                                </div>
                            </div>
                            </div>        
                            					
						</div>		
					</div>
				</div>
			<hr>
			


	</div>
	<div class="row clearfix">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class="card">

                        <div class="body">

                            <h2 class="card-inside-title">
                                Employment Details
                          
                            </h2>
							<div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                <div class = "form-control">
                                    Position*
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <select class="form-control show-tick" name="position" id="position" required>
                                                <option value="accountant" name="accountant" selected="selected" >Accountant</option>
                                                <option value="branchAdmin" bame="branchAdmin">Branch Admin</option>
                                                <option value="lecturer" name="lecturer">Lecturer</option>
                                                <option value="receptionst" name="receptionst">Receptionst</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                <div class = "form-control">
                                    Date joined*
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="date" class="form-control" name="date_join" required>
                                        </div>
                                    </div>
                                </div>
                            </div>
							<div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
								<div class = "form-control">
                                    managed_by*
									</div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="managed_by" title="This field should contain 12 chracters. eg:SFXXXXXX123456" minlength="12" maxlength="12" required>
                                            <div class="help-info">Ex: SFXXXXXX123456</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
							<!--<div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
								<div class = "form-control">
                                    Job Type*
									</div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <select class="form-control show-tick" name="job_type" required>
                                                <option value="permenent" selected="selected">permenent</option>
                                                <option value="contract">contract</option>
                                                <option value="partTime">Part time</option>
                                                <option value="internship">Internship</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>-->

  

							 <!--<div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
								<div class = "form-control">
                                    Job Status*
									</div>
                                </div>
                               <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <select class="form-control show-tick" name="job_status" required>
                                                <option value="confirmed" selected="selected">Confirmed</option>
                                                <option value="probation">Probation</option>
                                                <option value="resigned">Resigned</option>
                                                <option value="dismissed">Dismissed</option>
                                                <option value="retired">Retired</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>



                            </div>-->


                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                    <div class = "form-control">
                                        Basic salary*
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="basic" required>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                    <div class = "form-control">
                                        Allowances*
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="allow" required>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                    <div class = "form-control">
                                        Deductions*
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="deduct" required>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                    </div>


                    
    

    <div class="row clearfix">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class="card">

                        <div class="body">

                            <h2 class="card-inside-title">
                                Contact
                        </h2>
                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                <div class = "form-control">
                                    Web
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="email" class="form-control" name="email" required>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                <div class = "form-control">
                                    Phone
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="phone" minlength="10" maxlength="10" required>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                <div class = "form-control">
                                    Address
                                    </div>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                                    <div class="form-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="address" required>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!--submit button-->
							<div class = "col-lg-8 col-md-8 col-sm-8 col-xs-8">
                                <input type=hidden name="hiddenvalue" value="<?php echo $rows['staffID']; ?>"> 
							</div>

							<div class = "col-lg-4 col-md-4 col-sm-4 col-xs-4">
								<input type="submit" class="btn bg-blue btn-block waves-effect" name="addsm" value = "ADD STAFF MEMBER"/>
                            </div>

                        <br><br>
                    </div>
                </div>

            </div>
            </div>
		
        </div>
		</form>

 

    </section>

           </div>
    </div>
</div>





"insertStaffQuery.php'

        <?php 

            if(isset($_GET['addsm']))
            {
 
                $addsmCheck = $_GET['addsm'];

                if($addsmCheck == "empty")
                {
                    echo "<script type='text/javascript'>alert('Fill all required feilds'); </script>";
                    header("Location: addSMemberForm.php");
                }
                elseif($addsmCheck == "email") 
                {
                    echo "<script type='text/javascript'>alert('Email is not valid'); </script>";
                    "Location: addSMemberForm.php";
                } elseif($addsmCheck == "invalid") 
                {
                    echo "<script type='text/javascript'>alert('Staff ID is not valid'); </script>";
                    "Location: addSMemberForm.php";
                }
                elseif($addsmCheck == "idtaken")
                {
                    echo "<script type='text/javascript'>alert('Staff ID is taken'); </script>";
                    "Location: addSMemberForm.php"; 
                }



            }


         ?>

	
	
		<!-- Javascript --------------------------------->
		<?php include 'scripts.php'; ?>
		<!-- #END# Javascript  -->


		
</body>

</html>

        <?php

        if (isset($_POST['addsm']))
        {

	    	//db connection 
	        include_once 'dbh.php';

			$position = $_POST['position'];
			$job_type = $_POST['job_type'];
			$job_status = $_POST['job_status'];
			$emp_id = mysqli_real_escape_string($conn, $_POST['emp_id']);        
			$full_name = mysqli_real_escape_string($conn, $_POST['full_name']);       
			$gender = mysqli_real_escape_string($conn, $_POST['gender']);
			$dob = mysqli_real_escape_string($conn, $_POST['dob']);
			$nic = mysqli_real_escape_string($conn, $_POST['nic']);             
			$date_join = mysqli_real_escape_string($conn, $_POST['date_join']);
			$managed_by = mysqli_real_escape_string($conn, $_POST['managed_by']);      
			$email = mysqli_real_escape_string($conn, $_POST['email']);
			$phone = mysqli_real_escape_string($conn, $_POST['phone']);
			$address = mysqli_real_escape_string($conn, $_POST['address']);
			$basic = mysqli_real_escape_string($conn, $_POST['basic']);
			$allow = mysqli_real_escape_string($conn, $_POST['allow']);
			$deduct = mysqli_real_escape_string($conn, $_POST['deduct']); 
			$rawIdentifier = mysqli_real_escape_string($conn,$_POST['hiddenvalue']); 
		


			//error handlers

			if(empty($position))
			{
				header("Location: ../addSMemberForm.php?addsm=nopos");
				echo $position;
	    		exit();	
			}


			//check for empty feilds || empty($job_type) || empty($job_status) || empty($position)
			if(empty($emp_id) || empty($full_name) || empty($gender) || empty($dob) || empty($nic)  || empty($date_join) || empty($managed_by)  || empty($email) || empty($phone) || empty($address) || empty($basic) || empty($allow) || empty($deduct))
			{
	    		header("Location: ../addSMemberForm.php?addsm=empty");
	    		exit();		
			}
			else
			{

				//checking input values are valid
				if(!preg_match("/^SF([a-zA-Z)]){4}\d{6}/", $emp_id))
				{
	    			header("Location: ../addSMemberForm.php?addsm=invalid");
	    			exit();	
				}
				else
				{

				
					//check if email is valid
					if(!filter_var($email, FILTER_VALIDATE_EMAIL))
					{
						header("Location: ../addSMemberForm.php?addsm=email");
	    				exit();
					}
					else
					{
						$sqlidcheck = "SELECT * FROM staff WHERE staffID='$emp_id'";
						$result = mysqli_query($conn, $sqlidcheck);
						$resultCheck = mysqli_num_rows($result);

						if($resultCheck > 0)
						{
							header("Location: ../addSMemberForm.php?addsm=idtaken");
	    					exit();		
						}
						else
						{
				

							$sql1 = "INSERT INTO staff (staffID, nameInFull, managedBy, gender, post, dob, nic, datejoined, jobtype, jobstatus) VALUES
									('$emp_id', '$full_name','$managed_by', '$gender','$position', '$dob', '$nic', '$date_join' , '$job_type', '$job_status');";
					

							$sql3 = "INSERT INTO salary (staffID, basic, allowences, deduction) VALUES
									('$emp_id', '$basic', '$allow', '$deduct');";

							

							$sql5 = "INSERT INTO staffcontact (staffID, email, officephone, address) VALUES
									('$emp_id', '$email', '$phone', '$address');";


				            mysqli_query($conn, $sql1);
				                				            
                             mysqli_query($conn, $sql3);
				            
				            mysqli_query($conn, $sql5);

				            header("Location: ../addSMemberForm.php?addsm=sucess");
	    					exit();
	   					}

					}

				}
			}
		}   
	    else
	    {
	    	header("Location: ../addSMemberForm.php?");
	    	exit();
	    }
?>

Как получить значения, вставленные в базу данных с помощью HTML <select>? Это не работает в моем случае.

Add.php // содержит форму

<div class = "body">
     <form method = "post" action = "includes/insertStaffQuery.php">
          <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
          div class="form-group">
              <div class="form-line">
                <select class="form-control show-tick" name="position" 
                 required><option value="accountant" 
                  selected="selected" >Accountant</option>
                  <option value="branchAdmin">Branch Admin</option>
                  <option value="lecturer">Lecturer</option>
                  <option value="receptionst">Receptionst</option>
                </select>
             </div>
         </div>
   </div>

insertStaffQuery.php // фрагмент файла, в котором данные формы запрошены к

if (isset($_POST['addsm']))
{
    $position = $_POST['position'];    if(empty($position))//this if block always run assuming no data passed to to the position variable
    {
        header("Location: ../add.php?msg=error");
        exit(); 
    }
}

0 ответов