我正在尝试为用户显示一些唯一的号码。当人们提交询问时,他们会得到这个号码(以便稍后审查)。
index.php(表格) - > submit.php(检查数据库) - > thankyou.php(向用户显示唯一编号)
它运行正常,查询存储在数据库中,但我无法显示该号码。
Undefined index: semak in C:\wamp\www\FORM\thankyou.php on line 10
$semak=$_POST["semak"];
Undefined index: semak in C:\wamp\www\FORM\thankyou.php on line 14
$sql = "SELECT semak FROM pemohon WHERE semak='".$_POST['semak']."'";
UPDATE: the errors are now gone. but the number still isnt showing :(
的index.php
<span class="required_notification"><div style="position: absolute; right: -0px; top: -0px;">
<img src="img/logojata.jpg" alt="kpm" style="float:right">
<div style="position: absolute; left: 80px; top: -10px;"><br><br><br><br>* Denotes Required Field</span>
</li>
<li>
<label for="name">Nama Pemohon:</label>
<input type="text" name="name" required />
</li>
<li>
<label for="jawatan">Jawatan:</label>
<input type="text" name="jawatan" />
</li>
<li>
<label for="unit">Unit/Jabatan/Bahagian:</label>
<input type="text" name="unit" required />
</li>
<li>
<label for="kementerian">Kementerian/Institusi/Agensi:</label>
<input type="text" name="kementerian" required />
</li>
<li>
<label for="telefon">No. Telefon:</label>
<input type="number" name="telefon" placeholder="eg: 012-345-6789" required />
</li>
<li>
<label for="faks">No. Faks:</label>
<input type="number" name="faks" placeholder="eg: 03-12345678" />
</li>
<li>
<label for="email">E-mail:</label>
<input type="email" name="email" placeholder="name@something.com" required />
<span class="form_hint">proper format<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script></span>
</li>
<li>
<label for="data">Data/Laporan Yang Dipohon:</label>
<input name="data" type="text" required value="">
</li>
<li>
<label for="tujuan">Tujuan:</label>
<input type="text" name="tujuan" required/>
</li>
<input type="hidden" name="semak" value="<?php echo uniqid(); ?>" id="semak">
<li>
<button class="submit" type="submit">Submit</button>
</li>
submit.php
<?php
//debug mode
error_reporting(E_ALL);
ini_set('display_errors', '1');
//to show some error is smthng went wrong
$errors = array();
include('config.php');
//will run if user did submit the form
if (!empty($_POST)){
//connect sql server:
$name = $_POST['name'];
$jawatan = $_POST['jawatan'];
$unit = $_POST['unit'];
$kementerian = $_POST['kementerian'];
$telefon = $_POST['telefon'];
$faks = $_POST['faks'];
$email = $_POST['email'];
$data = $_POST['data'];
$tujuan = $_POST['tujuan'];
$semak = $_POST['semak'];
$tindakan = $_POST['tindakan'];
$agihan = $_POST['agihan'];
//no error til here
if (empty($error)){
//prevent SQL injection
$name = mysql_real_escape_string($name);
$jawatan = mysql_real_escape_string($jawatan);
$unit = mysql_real_escape_string($unit);
$kementerian = mysql_real_escape_string($kementerian);
$telefon = mysql_real_escape_string($telefon);
$faks = mysql_real_escape_string($faks);
$email = mysql_real_escape_string($email);
$data = mysql_real_escape_string($data);
$tujuan = mysql_real_escape_string($tujuan);
$semak = mysql_real_escape_string($semak);
$tindakan = mysql_real_escape_string($tindakan);
$agihan = mysql_real_escape_string($agihan);
}
//try insert value
$query = "INSERT INTO pemohon
(name,jawatan,unit,kementerian,telefon,faks,email,data,tujuan,semak)
VALUES ('$name', '$jawatan', '$unit', '$kementerian', '$telefon', '$faks', '$email', '$data', '$tujuan', '$semak')";
//try
if (!mysql_query($query)){
//
//die(mysql_error());
$errors[] = "Can't insert the values";
}
else {
//on success
header("Location:thankyou.php?semak=$row[semak];");
exit();
}
}
?>
thankyou.php
<html>
<head>
<meta charset="utf-8">
<title>terima kasih</title>
<link rel="stylesheet" media="screen" href="css/stylesphp.css" >
</head>
<body bgcolor="#13b4ff">
<?php
$semak=$_GET["semak"];
include('config.php');
$sql = "SELECT semak FROM pemohon WHERE semak='".$_GET['semak']."'";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))
{
$semak = $row['semak'];
}
if ($semak == $semak){
?>
<div class="boxed";>
<div id="thankyou" style= "text-align:center; font-size:50px;">THANK YOU</div><br>
<div style="text-align:center;">Your request has been sent.</div><br>
Please keep this code for the purpose of reviewing.<br>
Number: <?php echo "$semak"; ?>
<p> Kembali ke <a class="a" href="index.php" target="_self">back.</a>
</div>
<?php
}
else
{
}
?>
</body>
</html>
thankyou.php页面正在运行但数字部分留空并出现错误。我不明白为什么会这样?
答案 0 :(得分:1)
在thankyou.php
中使用$_GET
代替$_POST
$semak=$_GET["semak"];
:)
您没有将数据发布到thankyou.php,这就是$_POST
为空的原因。从我看到你通过网址传递数据。要提取数据,请使用$_GET
变量。
答案 1 :(得分:1)
我在下面测试了我的代码并且成功了。
但是,您可能会遇到以下错误消息:
注意:使用未定义的常量semak - 假设&#39; semak&#39;在....
这将是由于这一行:(我以为我会提前通知你)
header("Location:thankyou.php?semak=$row[semak];");
另一个注释:如果有重复的条目,您可能需要制作semak
列UNIQUE
。
尝试使用sessions,它比POST更易于在多个页面中传输。
在整个代码中查看评论。
重要的旁注:
在运行此代码之前,请复制您的工作代码并将其放在另一个文件夹中。
<强>的index.php 强>
<?php
session_start();
$_SESSION['semak_session'] = uniqid(); // assign the session to uniqid()
$semak = $_SESSION['semak_session']; // assign $semak from session variable
// you can comment out the following on success, or delete it
echo $semak;
// if you view source, you will see the same number
// appear as <input type="hidden" name="semak" value="633ccd4136r5fb1" id="semak">
// as an example
?>
<span class="required_notification"><div style="position: absolute; right: -0px; top: -0px;">
<img src="img/logojata.jpg" alt="kpm" style="float:right">
<div style="position: absolute; left: 80px; top: -10px;"><br><br><br><br>* Denotes Required Field</span>
</li>
<li>
<label for="name">Nama Pemohon:</label>
<input type="text" name="name" required />
</li>
<li>
<label for="jawatan">Jawatan:</label>
<input type="text" name="jawatan" />
</li>
<li>
<label for="unit">Unit/Jabatan/Bahagian:</label>
<input type="text" name="unit" required />
</li>
<li>
<label for="kementerian">Kementerian/Institusi/Agensi:</label>
<input type="text" name="kementerian" required />
</li>
<li>
<label for="telefon">No. Telefon:</label>
<input type="number" name="telefon" placeholder="eg: 012-345-6789" required />
</li>
<li>
<label for="faks">No. Faks:</label>
<input type="number" name="faks" placeholder="eg: 03-12345678" />
</li>
<li>
<label for="email">E-mail:</label>
<input type="email" name="email" placeholder="name@something.com" required />
<span class="form_hint">proper format<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script></span>
</li>
<li>
<label for="data">Data/Laporan Yang Dipohon:</label>
<input name="data" type="text" required value="">
</li>
<li>
<label for="tujuan">Tujuan:</label>
<input type="text" name="tujuan" required/>
</li>
<input type="hidden" name="semak" value="<?php echo $semak; ?>" id="semak">
<li>
<button class="submit" type="submit">Submit</button>
</li>
<强> submit.php 强>
<?php
//debug mode
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
if(isset($_SESSION['semak_session']) && !empty($_SESSION['semak_session'])){
echo $_SESSION['semak_session']; // can be commented out after success
$semak = $_SESSION['semak_session'];
echo "<hr>"; // can be commented out after success
echo $semak; // can be commented out after success
}
//to show some error is smthng went wrong
$errors = array();
include('config.php');
//will run if user did submit the form
if (!empty($_POST)){
//connect sql server:
$name = $_POST['name'];
$jawatan = $_POST['jawatan'];
$unit = $_POST['unit'];
$kementerian = $_POST['kementerian'];
$telefon = $_POST['telefon'];
$faks = $_POST['faks'];
$email = $_POST['email'];
$data = $_POST['data'];
$tujuan = $_POST['tujuan'];
// $semak = $_POST['semak']; // commenting out for now. May not be needed.
$tindakan = $_POST['tindakan'];
$agihan = $_POST['agihan'];
//no error til here
if (empty($error)){
//prevent SQL injection
$name = mysql_real_escape_string($name);
$jawatan = mysql_real_escape_string($jawatan);
$unit = mysql_real_escape_string($unit);
$kementerian = mysql_real_escape_string($kementerian);
$telefon = mysql_real_escape_string($telefon);
$faks = mysql_real_escape_string($faks);
$email = mysql_real_escape_string($email);
$data = mysql_real_escape_string($data);
$tujuan = mysql_real_escape_string($tujuan);
// $semak = mysql_real_escape_string($semak); // leaving this out for now
$tindakan = mysql_real_escape_string($tindakan);
$agihan = mysql_real_escape_string($agihan);
}
//try insert value
$query = "INSERT INTO pemohon
(name,jawatan,unit,kementerian,telefon,faks,email,data,tujuan,semak)
VALUES ('$name', '$jawatan', '$unit', '$kementerian', '$telefon', '$faks', '$email', '$data', '$tujuan', '$semak')";
//try
if (!mysql_query($query)){
//
//die(mysql_error());
$errors[] = "Can't insert the values";
}
else {
//on success
header("Location:thankyou.php?semak=$row[semak];");
exit();
}
}
?>
<强> thankyou.php 强>
<?php
//debug mode
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
if(isset($_SESSION['semak_session']) && !empty($_SESSION['semak_session'])){
echo $_SESSION['semak_session']; // can be commented out after success
$semak = $_SESSION['semak_session'];
echo "<hr>"; // can be commented out after success
echo $semak; // can be commented out after success
} // added in EDIT
?>
<html>
<head>
<meta charset="utf-8">
<title>terima kasih</title>
<link rel="stylesheet" media="screen" href="css/stylesphp.css" >
</head>
<body bgcolor="#13b4ff">
<?php
include('config.php');
$sql = "SELECT semak FROM pemohon WHERE semak='$semak'";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))
{
$semak = $row['semak'];
}
if ($semak == $semak){
?>
<div class="boxed";>
<div id="thankyou" style= "text-align:center; font-size:50px;">THANK YOU</div><br>
<div style="text-align:center;">Your request has been sent.</div><br>
Please keep this code for the purpose of reviewing.<br>
Number: <?php echo "$semak"; ?>
<p> Kembali ke <a class="a" href="index.php" target="_self">back.</a>
</div>
<?php
}
else
{
}
?>
</body>
</html>