0

I'm trying to display some unique number for the user. when people submit the inquiry, they will get the number (its for the reviewing the inquiry later).

index.php (the form) --> submit.php(checks the db) --> thankyou.php(display the unique number to user)

it works fine, the inquiry stored in the database, but I can't display the number back.

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>
        &nbsp;Please keep this code for the purpose of reviewing.<br>
        &nbsp;Number: <?php echo "$semak"; ?>
        <p>&nbsp;Kembali ke <a class="a" href="index.php" target="_self">back.</a>
        </div>

<?php
}
else
{
}
?> 

</body>
</html>

the thankyou.php page are working but the number section are left blank with the errors. I didnt understand why did this happened?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Syaa
  • 79
  • 2
  • 13

2 Answers2

1

Use $_GET instead of $_POST in the thankyou.php

 $semak=$_GET["semak"];

:)

You are not posting data to the thankyou.php thats why $_POST is empty. From what i saw you are passing the data via the url. To extract the data, use $_GET variable.

Nikko Madrelijos
  • 545
  • 3
  • 10
  • ah that's why! very sily of me. the error are gone now thanks! but, the number still isnt showing :( – Syaa Apr 03 '14 at 01:24
  • Change also the `$_POST` in the `$sql = "SELECT semak FROM pemohon WHERE semak='".$_POST['semak']."'";` to `$sql = "SELECT semak FROM pemohon WHERE semak='".$_GET['semak']."'";` – Nikko Madrelijos Apr 03 '14 at 01:26
  • I did change both of them but the number still isnt showing – Syaa Apr 03 '14 at 01:27
  • Okay so what is the output when you submit the form? nothing? – Nikko Madrelijos Apr 03 '14 at 01:41
  • it shows up like this `Number: ;` ..its supposed to be `Number: 53193d19a6230` something like that. – Syaa Apr 03 '14 at 01:43
  • Or did you check the result of the query? the result of the query might be empty. thats why its giving you empty $semak, and `$semak == $semak` is useless because it will always be true. – Nikko Madrelijos Apr 03 '14 at 01:44
1

Edit

I tested my code below and it was successful.

However, you might encounter the following error message:

Notice: Use of undefined constant semak - assumed 'semak' in....

this will be due to this line: (I thought I would let you know ahead of time)

header("Location:thankyou.php?semak=$row[semak];");

Another note: You may want to make your semak colum UNIQUE in case of duplicate entries.


Try using sessions, which is more transportable throughout multiple pages than POST.

See comments throughout the code.

Important sidenote:

Before running this, make a copy of your working code and place it in another folder.


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>
        &nbsp;Please keep this code for the purpose of reviewing.<br>
        &nbsp;Number: <?php echo "$semak"; ?>
        <p>&nbsp;Kembali ke <a class="a" href="index.php" target="_self">back.</a>
        </div>

<?php
}
else
{
}
?> 

</body>
</html>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • thank you for your response, i tried running the code and i get a `syntax error, unexpected end of file in C:\wamp\www\FORM\thankyou.php on line 60` . – Syaa Apr 03 '14 at 03:47
  • Reload my answer. I forgot a closing brace `}` right after `echo $semak; // can be commented out after success` in the `thankyou.php` file/code @Syaa – Funk Forty Niner Apr 03 '14 at 03:50
  • ah I just realized about the duplicate, mmm. i cant set `semak` as `unique` because i already have a pk. maybe i'll fiqured something :) – Syaa Apr 03 '14 at 04:09
  • 1
    You're very much welcome, glad I could help. Sessions are indeed very powerful and as I stated in my answer, are far more transportable across pages than regular POST variables. Setting your `semak` column as `UNIQUE` will ensure that there are no duplicate entries made in the DB, should someone try and reload the page. @Syaa In turn, they will see the `Can't insert the values` error message. Or, change it to "Can't insert the values. This may be caused by a duplicate entry in our database, please try again." – Funk Forty Niner Apr 03 '14 at 04:15
  • 1
    What I did was that I have an `id` column set as `PRIMARY KEY`, and `semak` set as `UNIQUE` which worked. @Syaa Maybe something similar will work for you. – Funk Forty Niner Apr 03 '14 at 04:24