我创建了一个表单条目网页,用于存储输入到本地数据库的数据。唯一的问题是,在表单上单击提交按钮后,它将转到另一个名为" info1.php"的文件中。将数据存储在本地数据库中,在该文件中,它还验证输入的数据是否有效,例如电子邮件。如果数据无效,则将页面刷新到表单输入站点或网页" home.php"
同样的用户不必再从一开始就输入整个东西会好得多,但是之前输入的相同值仍然存在于网页(即)表单中谢谢你们提前了:-) 表单代码是:
<?php
session_start();
?>
<head>
<php
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Entry Form</title>
<link rel="stylesheet" href="css/normalize.css">
<link href='http://fonts.googleapis.com/css?family=Nunito:400,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<form action="info1.php" method="post">
<h1>DATA ENTRY</h1>
<fieldset>
<label for="name">Name:</label>
<input type="text" id="name" name="user_name" required/>
<label for="rollnumber">Roll number</label>
<input type="text" id="rollnum" name="roll_number" required/>
<label for="batch">Batch</label>
<select id="batch" name="user_batch" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="2013-2017">2013-2017</option>
<option value="2014-2018">2014-2018</option>
<option value="2015-2019">2015-2019</option>
<option value="2016-2020">2016-2020</option>
</select>
</fieldset>
<label for="from">Came from</label>
<select id="from" name="user_from" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="+2">+2</option>
<option value="Diploma">Diploma</option>
</select>
<label for="year">Year</label>
<select id="year" name="user_year" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<label for="level">Level</label>
<select id="level" name="user_level"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="UG">UG</option>
<option value="PG">PG</option>
</select>
<label for="email">Email:</label>
<input type="text" id="email_id" name="email_id" required/>
<label for="name">Father`s Name:</label>
<input type="text" id="fname" name="F_name" required/>
<label for="name">Mother`s Name:</label>
<input type="text" id="mname" name="m_name" required/>
<label for="name">Guardian`s Name:</label>
<input type="text" id="gname" name="g_name" >
<label for="mob_number">Students Mobile Number</label>
<input type="number" id="mob_number" name="mob_number"required/>
</select>
<label for="addressline1">Address Line 1</label>
<input type="text" id="addressline1" name="addressline1" required/>
</select>
<label for="addressline2">Address Line2</label>
<input type="text" id="addressline2" name="addressline2" required/>
</select>
<label for="city">City</label>
<input type="text" id="city" name="city" required/>
<label for="pmobnumber">Parents Mobile Number</label>
<input type="number" id="pmob_num" name="pmob_number" required/>
<label for="landnumber">LandLine Number or Parents Number</label>
<input type="number" id="landnum" name="land_number"required/>
<label for="cutoff">+2 cutoff</label>
<input type="number" id="cutoff" name="cutoff">
<label for="dept">Department</label>
<select id="dept" name="dept" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="CSE">CSE</option>
<option value="IT">IT</option>
<option value="ECE">ECE</option>
<option value="EEE">EEE</option>
<option value="MECH">MECH</option>
<option value="CIVIL">CIVIL</option>
</select>
<label for="medium">Medium of Instruction</label>
<select id="medium" name="medium"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="ENGLISH">English</option>
<option value="TAMIL">Tamil</option>
</select>
<label for="locality">Locality</label>
<select id="locality" name="locality"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="RURAL">Rural</option>
<option value="URBAN">Urban</option>
</select>
<label for="intrest">Intrested In</label>
<select id="intrest" name="intrest"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="PLACEMENTS">Placements</option>
<option value="HIGHER STUDIES">Higher Studies</option>
<option value="Entrepreneurship">Entrepreneurship</option>
</select>
<label for="scholar">Scholarship</label>
<select id="scholar" name="scholar"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="GOVERNMENT">Government</option>
<option value="PRIVATE">Private</option>
</select>
<label for="income">Parents Annual Income</label>
<select id="income" name="income" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="Less than 50,000">Less than 50,000</option>
<option value="Less than 1,00,000">Less than 1,00,000</option>
<option value="Less than 2,00,000">Less than 2,00,000</option>
<option value="Less than 5,00,000">Less than 5,00,000</option>
<option value="Less than 10,00,000">Less than 10,00,000</option>
<option value="OTHERS">others</option>
</select>
<label for="year">Caste</label>
<select id="caste" name="user_caste" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="OC">OC</option>
<option value="BC">BC</option>
<option value="MBC">MBC</option>
<option value="BCM">BCM</option>
<option value="SC">SC</option>
<option value="ST">ST</option>
</select>
<label for="admit">Admitted through</label>
<select id="admit" name="user_admit"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="Government Quota">Government Quota</option>
<option value="Manegement Quota">Manegement Quota</option>
</select>
<label for="stay">Staying In</label>
<select id="stay" name="user_stay"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="With parents">With parents</option>
<option value="College Hostel">College Hostel</option>
<option value="Outside Room">Outside Room</option>
</select>
</fieldset>
<button type="submit" onclick="phonenumber(pmob_number)">ENTER DATA</button>
</form>
<script src="js/mob_valid.js"></script>
</body>
文件info1.php的代码是:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect ($servername , $username , $password) or die("unable to connect to host");
$sql = mysqli_select_db ($conn,'vcet') or die("unable to connect to database");
//code to insert into db...
$user_name=$_POST['user_name'];
$roll_number=$_POST['roll_number'];
$user_batch=$_POST['user_batch'];
$user_from=$_POST['user_from'];
$user_year=$_POST['user_year'];
$user_level=$_POST['user_level'];
$email_id=$_POST['email_id'];
$F_name=$_POST['F_name'];
$m_name=$_POST['m_name'];
$g_name=$_POST['g_name'];
$mob_number=$_POST['mob_number'];
$addressline1=$_POST['addressline1'];
$addressline2=$_POST['addressline2'];
$city=$_POST['city'];
$pmob_number=$_POST['pmob_number'];
$land_number=$_POST['land_number'];
$cutoff=$_POST['cutoff'];
$dept=$_POST['dept'];
$medium = $_POST['medium'];
$locality=$_POST['locality'];
$intrest=$_POST['intrest'];
$scholar=$_POST['scholar'];
$income=$_POST['income'];
$user_caste=$_POST['user_caste'];
$user_admit=$_POST['user_admit'];
$user_stay=$_POST['user_stay'];
if (!filter_var($email_id, FILTER_VALIDATE_EMAIL)) {
echo '<script language="javascript">';
echo 'alert("Sorry...You entered an invalid email id..!!!")';
echo '</script>';
//mysqli_query("delete from studentinfo where roll_number='$roll_number'");
header( "refresh:1;url=http://localhost/DB/home.php" );
}else
{
//executes the rest of the code
$mob_number1=$mob_number;
if(!preg_match('/^\d{10}$/',$mob_number1)) // phone number is valid
{
echo '<script language="javascript">';
echo 'alert("Sorry...You entered a wrong Mobile number..!!!")';
echo '</script>';
//mysqli_query("delete from studentinfo where roll_number='$roll_number'");
header( "refresh:1;url=http://localhost/DB/home.php" );
}
else // phone number is valid
{
$msg="";
$num_length = strlen((string)$pmob_number);
if($num_length!=10){
echo '<script language="javascript">';
echo 'alert("Sorry...You entered a wrong Mobile number..!!!")';
echo '</script>';
//mysqli_query("delete from studentinfo where roll_number='$roll_number'");
header( "refresh:1;url=http://localhost/DB/home.php" );
}else
{
$user_info = "INSERT INTO studentinfo"." (user_name,roll_number,user_batch,user_from,user_year,user_level,email_id,F_name,m_name,g_name,mob_number,addressline1,addressline2,city,pmob_number,land_number,cutoff,dept,medium,locality,intrest,scholar,income,user_caste,user_admit, user_stay)". "VALUES ('$user_name', '$roll_number','$user_batch','$user_from','$user_year','$user_level','$email_id','$F_name','$m_name','$g_name','".$mob_number."','$addressline1','$addressline2','$city','$pmob_number','$land_number','$cutoff','$dept','$medium','$locality','$intrest','$scholar','$income','$user_caste','$user_admit','$user_stay')";
if (!mysqli_query($conn,$user_info)) { die('Error: ' . mysqli_error($conn)); } echo '<script language="javascript">';
echo 'alert("Awesome!! your INFORMATION was added to the DATABASE")';
echo '</script>';
header( "refresh:1;url=http://localhost/DB/home.php" );
mysqli_close($conn);
}
}
}
?>
答案 0 :(得分:1)
正如RiggsFolly在评论中所说,您可以使用SESSION保留值,使用SESSION变量,您必须在每个文件的开头打开会话,您将使用它。
基本上,在您的表单页面上:(您应该检查$ _SESSION [&#39; input_value&#39;] var是否存在)
<?php session_start();
if(!isset($_SESSION['input_value']) {
$_SESSION['input_value'] = "Default value for this input";
}
?>
some code ...
<input value='<?php echo $_SESSION['input_value']; ?>'>
在您的表格处理页面上:
<?php session_start();
some code ...
$_SESSION['input_value'] = $_POST['input_value']; // ( or $_GET['input_value'] )
?>