入力したデータをローカルデータベースに保存するフォーム入力Webページを作成しました。唯一の問題は、フォーム上で送信ボタンをクリックした後、ローカルデータベースにデータを保存するために "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);
}
}
}
?>
保存$:フォーム処理ページに
を(あなたは$ _SESSION [ 'input_valueが'] varが存在するかどうかを確認OFC必要があります)セッションの_POST値と、データ入力ページに戻ったときに、セッションを調べます。それらが存在する場合は、値を使用して、入力フィールドを以前に入力したもので再ロードします。 – RiggsFolly
ページをリロードする場合は、値を設定する必要があります。あなたはそれを実装しませんでした(つまり、入力の値属性を追加し、オプションの値を設定します)。なぜなら、すべてが空であるからです。 – Roger
@RiggsFolly申し訳ありませんが、私はちょっとこれで新しいです。そして私はセッションを使用することにあまり慣れていません。あなたは私に例を教えてくれますか? 。それは本当に役に立ちます。ありがとう –