3

I don't know why my html form is not sending data. I have 3 file called default.php, prosesbacasoal.php and bacasoal.php. Because the default.php is too long I just write the html form I get from inspect element

<form method="post" action="prosesbacasoal.php"><div class="head-main-   recenttest-result">
<input type="hidden" name="nomor" value="2">
<button class="head-main-recenttest-result-wait" style="text-decoration:none;" type="submit" name="submit">2.Soal Kedua</button> </div></form>

prosesbacasoal.php

 <?php
session_start();
if(isset($_POST['submit'])) {
if(isset($_POST['nomor'])) {
$_SESSION['submitsoal'] = true;
$_SESSION['nomorsoal'] = $_POST['nomor'];
header("Location:bacasoal.php");
exit;
} else {
header("Location:bacasoal.php");
exit;
}
} else {
header("Location:bacasoal.php");
exit;
}
?>

Also the bacasoal.php is too long so I just write the part of it:

<?php
session_start();
if(isset($_SESSION['submitsoal'])) {
if(isset($_SESSION['nomorsoal'])) {
$nomorsoal = $_SESSION['nomorsoal'];
$queryjudulnya = "SELECT nomorsoal,judul,soal FROM soal WHERE   nomorsoal='".$nomorsoal."'";
$runqueryjudulnya = mysqli_query($konek,$queryjudulnya);
$countqueryjudulnya = mysqli_num_rows($runqueryjudulnya);
if($countqueryjudulnya != 0) {
$assocqueryjudulnya = mysqli_fetch_assoc($runqueryjudulnya);
$juduldatabase = mysqli_real_escape_string($assocqueryjudulnya['judul']);
$soaldatabase = mysqli_real_escape_string($assocqueryjudulnya['soal']);
$nomorsoaldatabase =      mysqli_real_escape_string($assocqueryjudulnya['nomorsoal']);
} else {}
} else {}
} else {}
?>

<?php 
if(isset($juduldatabase) && isset($nomorsoaldatabase)) {
echo "<div class=\"head-main-recent\"> ".$nomorsoaldatabase.$juduldatabase."  </div>";
} else {
echo "<div class=\"head-main-recent\">Judul soal tidak ditemukan!</div>";
}
?>

bacasoal.php keep echo the fail statement "Judul soal tidak ditemukan!"

Does anyone know why? (live demo : http://english-lesson.16mb.com/)

5
  • mysqli_real_escape_string requires the connection object as the 1st parameter if using it in procedural style as you do here - but why use that AFTER querying the db? Commented Aug 17, 2016 at 6:01
  • Anant that's unsafe tho Commented Aug 17, 2016 at 6:03
  • i put connect method to mysqli real escape string but still no change Commented Aug 17, 2016 at 6:06
  • @FajarMerahDiwangkaraa nope, since you are not going to use those values in query again (as shown) so that is not at all required Commented Aug 17, 2016 at 6:10
  • After <?php session_start(); add error_reporting(E_ALL);ini_set('display_errors',1); and check any error is coming or not? Commented Aug 17, 2016 at 6:14

3 Answers 3

1

You can do it like below so that if error is there then it will display or any how at-least some useful information will display:-

default.php:-

<form method="post" action="prosesbacasoal.php">
<div class="head-main-recenttest-result">
    <input type="hidden" name="nomor" value="2">
    <button class="head-main-recenttest-result-wait" style="text-decoration:none;" type="submit" name="submit">2.Soal Kedua</button> 
</div>
</form>

prosesbacasoal.php:-

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['nomor'])) {
$_SESSION['submitsoal'] = 'true';
$_SESSION['nomorsoal'] = $_POST['nomor'];
header("location:bacasoal.php");
exit;
} else {
header("location:default.php");
exit;
}
?>

bacasoal.php:-

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);
$juduldatabase = '';
$soaldatabase = '';
$nomorsoaldatabase = '';
if(isset($_SESSION['submitsoal']) && isset($_SESSION['nomorsoal'])) {
    $nomorsoal = $_SESSION['nomorsoal'];
    $queryjudulnya = "SELECT nomorsoal,judul,soal FROM soal WHERE   nomorsoal='".$nomorsoal."'";
    echo $queryjudulnya;
    $runqueryjudulnya = mysqli_query($konek,$queryjudulnya);
    if($runqueryjudulnya){
        $countqueryjudulnya = mysqli_num_rows($runqueryjudulnya);
        if($countqueryjudulnya > 0) {
            while($assocqueryjudulnya = mysqli_fetch_assoc($runqueryjudulnya)){
                $juduldatabase = $assocqueryjudulnya['judul'];
                $soaldatabase = $assocqueryjudulnya['soal'];
                $nomorsoaldatabase =  $assocqueryjudulnya['nomorsoal'];
            }
        } else {
            echo "No matching record found";
        }
    }else{
        echo "Query execution failed because of:-".mysqli_error($konek);
    }
}else {
    echo "Session variables are not set";
}
?>
<?php 
if(isset($juduldatabase) && isset($nomorsoaldatabase)) {
echo "<div class="head-main-recent"> ".$nomorsoaldatabase.$juduldatabase."</div>";
} else {
echo "<div class="head-main-recent">Judul soal tidak ditemukan!</div>";
}
?>

Note:- if still no error and no records,then echo query and run that query manually in db and check any record are coming or not?

Sign up to request clarification or add additional context in comments.

3 Comments

sorry for late answer i having a lunch , ok i will try it first
go to english-lesson.16mb.com and click one button below " Tunggu soal berikutnya! " you will see the result tho
found the error , after i use inspect element to find hidden echo , i saw this result : Session variable are not set , but i already set it in prosesbacasoal.php , okay this is going very far i guess
1

In 3rd line of your HTML code I can see </div> before form tag ending. I cant see dive start tag after form tag

<button class="head-main-recenttest-result-wait" style="text-decoration:none;" type="submit" name="submit">2.Soal Kedua</button> </div></form>

Replace by

<button class="head-main-recenttest-result-wait" style="text-decoration:none;" type="submit" name="submit">2.Soal Kedua</button></form>

1 Comment

@FajarMerahDiwangkaraa check once again my edited answer and let me know worked or not?
0

it was php session problem , fixed it after i session_destroy(); it using logout.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.