简体   繁体   中英

if(isset($_POST['submit'])) coding form action is not working

hi im a beginner in php and mysql here if have a problem, it's either my isset post submit is not working or i have some errors in my coding, can anyone tell me what have i missed in my coding? thank you in advance :)

this is my php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tempahperalatan";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if (isset($_POST['submit'])) {

    $pemohon = $_POST['namaPemohon'];
    $trkhMula = $_POST['tmula'];
    $trkhAkhir = $_POST['takhir'];
    $n_program = $_POST['namaProgram'];
    $lokasi = $_POST['lokasi'];
    $n_anjuran = $_POST['namaAnjuran'];
    $catatan = $_POST['catatan'];

    $sql = "INSERT INTO daftartempah (pemohon, trkhMula, trkhAkhir, n_program, lokasi, n_anjuran, catatan) VALUES ('$namaPemohon', '$tmula', '$takhir', '$namaprogram', '$lokasi', '$namaAnjuran', '$catatan')";

    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}


mysqli_close($conn);
?> 

and this is my form:

<form action="page1.php" method="POST">

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Nama Pemohon: </label>
        <div class="col-10">
            <input class="form-control" type="text" name="namaPemohon" id="namaPemohon">
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="example-date-input" class="col-2 col-form-label">Tarikh Mula: </label>
        <div class="col-10">
            <input class="form-control" type="date" value="0000-00-00" name="tmula" id="tmula">
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="example-date-input" class="col-2 col-form-label">Tarikh Akhir: </label>
        <div class="col-10">
            <input class="form-control" type="date" value="0000-00-00" name="takhir" id="takhir">
        </div>
    </div>  

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Nama Program: </label>
        <div class="col-10">
            <input class="form-control" type="text" name="namaProgram" id="namaProgram">
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Lokasi: </label>
        <div class="col-10">
            <input class="form-control" type="text" name="lokasi" id="lokasi">
        </div>
    </div>              

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Dept/Kelab/Anjuran: </label>
        <div class="col-10">
            <select class="form-control" name="namaAnjuran" id="namaAnjuran">
                <option name="1" value="1">BK - B. Kewangan</option>
                <option name="2" value="1">BP - B. Pentadbiran</option>
                <option name="3" value="1">BPK - B. Perancangan Hal Ehwal Korporat</option>
                <option name="4" value="1">BPPP - B. Pemb. Penilaian Projek</option>
                <option name="5" value="1">BPPS - B. Pemb. Projek Sosial</option>
                <option name="6" value="1">UAD - Unit Audit Dalaman</option>
                <option name="7" value="1">PWT - Pej. wilayah Temerloh</option>
                <option name="8" value="1">PWB - Pej. Wilayah Barat</option>
                <option name="9" value="1">KOOP - Koperasi LKPP</option>
                <option name="10" value="1">KSKLKPP - KSK LKPP Negeri Pahang</option>           
                <option name="11" value="1">KSKCIP - KSK LKPP Cawangan Ibu Pejabat</option>
                <option name="12" value="1">IBUPEJ - IBU PEJABAT</option>   
                <option name="13" value="1">KESATUAN - KESATUAN SEKERJA</option>                                    
            </select>
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="exampleTextarea" class="col-2 col-form-label">Catatan: </label>
        <div class="col-10">
            <textarea class="form-control" id="exampleTextarea" rows="3" name="catatan" id="catatan"></textarea>
        </div>
    </div>

    <center><button type="submit" class="btn btn-info">Submit</button></center>                     

</form>

when i hit submit, there are no errors, but the data are not insert into the database.

When you want to pass data from form to your php script, you have to remember that the name attribute is assigned for that data to find in php script.

<button type="submit" class="btn btn-info">Submit</button>

In your button you are not assigned the name attribute. So after post method in php script isset($_POST['submit']) does not find any submit attribute. So it returns false.

<button type="submit" class="btn btn-info" name="submit" value="submit" >Submit</button>

So you have to use this line for button.

像这样尝试:

<button type="submit" class="btn btn-info" name="submit" value="submit" >Submit</button>

if (isset($_POST['submit'])) { this is not working why because you have not provided name of the submit button <button type="submit" class="btn btn-info">Submit</button>

add name of submit button like below.

<button type="submit" name="submit" class="btn btn-info">Submit</button>

OR

if (isset($_POST)) {

Always remember that it is the name attribute that links your form to your php script. Thus it should be:

<button type="submit" name="submit" class="btn btn-info">Submit</button>

Simply check if $_POST isn't empty

if(!empty($_POST)) // form has been submitted

You have no form input with name submit in your case

Hi give name for the button as following

<center><button type="submit" name="submit" class="btn btn-info">Submit</button></center>

this will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM