I have 3 file :
1. index.php
2. form.php
3. validasi form

i have a problem if I include file form.php into index.php,
why if I included form.php into index.php all field that I fill is inserted to database, but why only field picture is not inserted to database.

but if I not use index.php, just call form.php in browser all field are inserted also field picture...

I need your help please...

this is my code :

Quoted Text Here

index.php

<!DOCTYPE html>
<html>
<head>
<title>Informasi Peralatan</title>
  <div id="container">
  <div id="text">
  <h1>Data informasi aset <br> Gedung BOP</h1>
  <h2>PT.PLN (PERSERO) SEKTOR PEMBANGKITAN TARAHAN</h2>
  </div>
  <div id="center">
  <link rel="stylesheet" href="../CSS/alat.css" type="text/css" media="screen" />
  <link href="../jquery-ui-1.8.16.custom/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css"/>
  <script src="../jquery-ui-1.8.16.custom/js/jquery-1.6.2.min.js"></script>
  <script src="../jquery-ui-1.8.16.custom/js/jquery-ui-1.8.16.custom.min.js"></script>


  <script>
  $(document).ready(function() {
    $("#tabs").tabs();
  });
  </script>
</head>
<body style="font-size:62.5%;">
<div id="tabs">
    <ul>
        <li><a href="#fragment-1"><span>Tambah Data Peralatan</span></a></li>

    </ul>
    <div id="fragment-1">
        <p></p>


        <pre><code></code></pre>
        /* <?php include "form_bop.php"; ?> */
    </div>
    <style type="text/css">
    #slider { margin: 10px; }
  </style>
  <script>
  $(document).ready(function() {
    $("#slider").slider();
  });
  </script>
</head>
<body style="font-size:62.5%;">
<div id="slider"></div>
<div id="footer"> 
<script>
    $(function(){
    $( "button,input:submit,a".".demo").button();
    $("a",".demo").click(function() {return false;});
    });
</script>
<div class="demo">
  <input type="submit" value="Tambah">
 <a href="#">tambah.php</a>

 <input type="submit" value="Edit">
 <a href="#"></a>

  <input type="submit" value="Hapus">
 <a href="#"></a>
 </div>
</div>
</div>
</div>
</div>
</body>
</html>

Quoted Text Here
and this file form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include "koneksi.php" ?>
<head>
<title>Input data peralatan</title>


  <link href="../jquery-ui-1.8.16.custom/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css"/>
  <script src="../jquery-ui-1.8.16.custom/js/jquery-1.6.2.min.js"></script>
  <script type="text/javascript"> var jY = $.noConflict(true); </script>
  <script src="../jquery-ui-1.8.16.custom/js/jquery-ui-1.8.16.custom.min.js"></script>  


<!--script jqery form!-->
<script src="jquery-1.2.3.min.js"></script> 
<script type="text/javascript"> var jQ = $.noConflict(true); </script>
<script type="text/javascript">
jQ(document).ready(function() {

     jQ().ajaxStart(function() {
         jQ('#loading').show();
         jQ('#result').hide();
    }).ajaxStop(function() {
         jQ('#loading').hide();
         jQ('#result').fadeIn('slow');
    });

     jQ('#frm').submit(function() {
        jQ.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function(data) {
                jQ('#result').html(data);
            }
        })
        return false;
    });
})
</script>
<!--Akhir dari jquery form!-->


  <!---datepicker!-->
  <script>

  $(document).ready(function() {
$( "#tanggal" ).datepicker({ altFormat: 'yy-mm-dd' });
        $( "#tanggal" ).change(function() {
             $( "#tanggal" ).datepicker( "option", "dateFormat","yy-mm-dd" );
         });
    });
  </script>
<body style="font-size:60.5%;">  
<div id="datepicker"></div>
<!--akhir dari datepicker!-->

<!--CSS Form dan Validasi form !-->
<style type="text/css">
body, table, input, select, textarea { font: 11px/20px Verdana, sans-serif; }
h4 { font-size: 18px; }
input { padding: 3px; border: 1px solid #999; }
td { padding: 3px; }
#result { background-color: #F0FFED; border: 1px solid #215800; padding: 10px; width: 400px; margin-bottom: 20px; }
</style>

<style type="text/css">

.error {
font-size:small;
color:red;
}
</style>
<!--akhir CSS Form dan Validasi form !-->

<!-- awal ajax ->
<script type="text/javascript">
$(document).ready(function() {
//aktifkan ajax di form
var options = {
success : showResponse,
resetForm : true,
clearForm : true,
dataType : 'json'
};
$('#frm').ajaxForm(options);
});
function showResponse(responseText, statusText) {
var data = responseText['data'];
var pesan = responseText['pesan'];
alert(pesan);
}
</script>
<!-- akhir!-->
<h4>Input Data Peralatan</h4>
<div id="loading" style="display:none;"><img src="loading.gif" alt="loading..." /></div>
<div id="result" style="display:none;"></div>
<form action="input.php" enctype="multipart/form-data" method="post" id="frm">
    <table>

        <tr>

            <td><label for="nama" class="labelfrm">Nama Alat</label></td>
            <td>
                <input name="nama" id="nama"size="40" type="text" class="required"/>
            </td>
        </tr>
        <tr>
            <td><label for="part_no" class="labelfrm">Part No</label></td>
            <td>
                <input name="part_no" id="part_no" size="40" type="text" />
            </td>
        </tr>
            <tr>
            <td><label for="jenis" class="labelfrm">Jenis</label></td>
            <td>
            <select name="jenis">
            <?php
            $tampil=("SELECT * FROM jenis_aset");
            $query_hasil=mysql_query($tampil);
            while($r=mysql_fetch_object($query_hasil))
            {
            echo "<option value=$r->Id_jenis>$r->Jenis</option>";
            }
            ?>
            </select>
            </td>
        </tr>
        <tr>
            <td><label for="lokasi" class="labelfrm">Lokasi</label></td>
            <td>
                <input name="lokasi" id="lokasi" size="60" type="text" />
            </td>
        </tr>
        <tr>
            <td><label for="label" class="labelfrm">Merek<label></td>
            <td>
                <input name="merek" id="merek" size="30" type="text" />
            </td>
        </tr>
        <tr>
            <td><label for="satuan" class="labelfrm">Satuan</label></td>
            <td>
                <input name="satuan" id="satuan" size="30" type="text" />
            </td>
        </tr>
        <tr>
            <td><label for="jumlah" class="labelfrm">Jumlah</td>
            <td>
                <input name="jumlah" id="jumlah" size="30" type="text" />
            </td>
        </tr>
        <tr>
            <td><label for="tanggal" class="labelfrm">Expired</label></td>
            <td>
                <input name="tanggal" id="tanggal" type="text" />
            </td>
        </tr>
        <tr>
            <td><label for="fungsi" class="labelfrm">Fungsi</label></td>
            <td>
                <textarea name="fungsi" id="fungsi" cols="40" rows="3">

Quoted Text Here

this file validasi_form.php

<?php
include ("koneksi.php");

/* penanganan form */

if (trim($_POST['nama']) == '') {
    $error[] = '- Nama alat masih kosong';
}
if (trim($_POST['fungsi']) == '') {
    $error[] = '- Fungsi harus diisi';
}
if (trim($_POST['keterangan']) == '') {
    $error[] = '- Keterangan harus diisi';
}

if (isset($error)) {
    echo '<b>Error</b>: <br />'.implode('<br />', $error);
}

else {
$Jenis          = strip_tags($_POST['jenis']);
$Nama_peralatan = strip_tags($_POST['nama']);
$Part_no        = strip_tags($_POST['part_no']);
$Lokasi         = strip_tags($_POST['lokasi']);
$Merk           = strip_tags($_POST['merek']);
$Satuan         = strip_tags($_POST['satuan']);
$Jumlah         = strip_tags($_POST['jumlah']);
$Expired        = strip_tags($_POST['tanggal']);
$Fungsi         = strip_tags($_POST['fungsi']);
$Keterangan     = strip_tags($_POST['keterangan']);
$nama_file      = strip_tags($_POST['gambar']);

//input ke db
//menentukan letak direktori file
  $lokasi_file = $_FILES['gambar']['tmp_name'];
  $nama_file   = $_FILES['gambar']['name'];
  $direktori   ="images/$nama_file"; 

// Apabila tidak ada gambar yang di upload
if (empty($lokasi_file)){
$query = sprintf("INSERT INTO lab_batubara (id,
                                            jenis,
                                            Nama_peralatan,
                                            Part_No,
                                            Lokasi,
                                            Merk,
                                            Satuan,
                                            Jumlah,
                                            Expired,
                                            Fungsi,
                                            Keterangan,
                                            Gambar)
                                    VALUES ('',
                                           '$Jenis',
                                           '$Nama_peralatan',
                                           '$Part_no', 
                                           '$Lokasi',
                                           '$Merk',
                                           '$Satuan',
                                           '$Jumlah',
                                           '$Expired',
                                           '$Fungsi',
                                           '$Keterangan',
                                           '$nama_file')",

mysql_escape_string($Nama_peralatan),
mysql_escape_string($Part_no),
mysql_escape_string($Lokasi),
mysql_escape_string($Merk),
mysql_escape_string($Satuan),
mysql_escape_string($Jumlah),
mysql_escape_string($Expired),
mysql_escape_string($Fungsi),
mysql_escape_string($Keterangan)
);
}                   
// Apabila ada gambar yang diupload
else { 
$move= move_uploaded_file($lokasi_file,"$direktori");
$query = sprintf("INSERT INTO lab_batubara  (id,
                                            jenis,
                                            Nama_peralatan,
                                            Part_No,
                                            Lokasi,
                                            Merk,
                                            Satuan,
                                            Jumlah,
                                            Expired,
                                            Fungsi,
                                            Keterangan,
                                            Gambar)
                                    VALUES ('',
                                           '$Jenis',
                                           '$Nama_peralatan',
                                           '$Part_no', 
                                           '$Lokasi',
                                           '$Merk',
                                           '$Satuan',
                                           '$Jumlah',
                                           '$Expired',
                                           '$Fungsi',
                                           '$Keterangan',
                                           '$nama_file')",



mysql_escape_string($Nama_peralatan),
mysql_escape_string($Part_no),
mysql_escape_string($Lokasi),
mysql_escape_string($Merk),
mysql_escape_string($Satuan),
mysql_escape_string($Jumlah),
mysql_escape_string($Expired),
mysql_escape_string($Fungsi),
mysql_escape_string($Keterangan)
);

if ($move) {
echo("Done! File saved...");
}
else{
echo"gagal";
}
}

$sql = mysql_query($query);
if ($sql) {
$data = '';
    foreach ($_POST as $k => $v) {
        $data .= "$k : $v<br />";
        echo "$data";
        }
    echo '<b>Form berhasil disubmit. Berikut ini data anda:</b><br />';
    echo $data;} else {
$pesan = "Data gagal disimpan ";
$pesan.= mysql_error();
}


    exit;
    }
?>

Recommended Answers

All 10 Replies

Member Avatar for LastMitch

@cokerz_written

i have a problem if I include file form.php into index.php,
why if I included form.php into index.php all field that I fill is inserted to database, but why only field picture is not inserted to database.

Most of your code is in JQuery

I am a bit confused.

1) Do you want to upload the image to the Database then select from image table?
Is this what you are asking?

2) You are very problems uploading an image?

3) You don't know how to upload the image in the folder?

Can you pick which number? So I can take a look.

I have succes to upload image to database, without index. php,
by why if I use index.php, with include form.php to index.php, its not working...?
Thanks..

Member Avatar for LastMitch

@cokerz_written

I have succes to upload image to database, without index. php,
by why if I use index.php, with include form.php to index.php, its not working...?

If you put the whole code from form.php into index.php it should work!

  $lokasi_file = $_FILES['gambar']['tmp_name'];
  $nama_file   = $_FILES['gambar']['name'];
  $direktori   ="images/$nama_file"; 

This is code where you upload the the images to your folder. If you place code in the index.php then you need to link it to that folder. I assume that form.php is not in the same folder as index.php?

it still not working, I had moved all of my script to index.php...
I thinks its realy2 weird,

but thanks for your reply...

Member Avatar for LastMitch

@cokerz_written

it still not working, I had moved all of my script to index.php...
I thinks its realy2 weird,

I don't think weird because you can upload the before it's just that you need to link to that folder.

If you move all of the code into that index.php it should work. What error do you get?

I got no idea how you put your files but if the index.php is by itself then you should try this

$direktori ="../images/$nama_file";

I be honestly with you, I don't know how you put your files? If you want to upload images from your index.php then you have to do what I did above to see how many level ../../ in order to reach that folder.

I don't have any error because all of field except Images is inserted to database,

Its weird because this code is not reading using index.php,

else {
$move= move_uploaded_file($lokasi_file,"$direktori");
$query = sprintf("INSERT INTO lab_batubara (id,
jenis,
Nama_peralatan,
Part_No,
Lokasi,
Merk,
Satuan,
Jumlah,
Expired,
Fungsi,
Keterangan,
Gambar)
VALUES ('',
'$Jenis',
'$Nama_peralatan',
'$Part_no',
'$Lokasi',
'$Merk',
'$Satuan',
'$Jumlah',
'$Expired',
'$Fungsi',
'$Keterangan',
'$nama_file')",
mysql_escape_string($Nama_peralatan),
mysql_escape_string($Part_no),
mysql_escape_string($Lokasi),
mysql_escape_string($Merk),
mysql_escape_string($Satuan),
mysql_escape_string($Jumlah),
mysql_escape_string($Expired),
mysql_escape_string($Fungsi),
mysql_escape_string($Keterangan)
);
if ($move) {
echo("Done! File saved...");
}
else{
echo"gagal";
}
}

is it because the jquery?

Member Avatar for LastMitch

@cokerz_written

is it because the jquery?

No, it's the php script:

$move= move_uploaded_file($lokasi_file,"$direktori");

it's not reading because you move the $lokasi_file you need go located it:

$lokasi_file = $_FILES['gambar']['tmp_name'];
$nama_file   = $_FILES['gambar']['name'];
$direktori   ="images/$nama_file"; 

when you put everything in 1 file you want it to close to that folder. so you can link it but now I do think it's the $move not the query because you mention that it works fine from the original file.

I think its not there, but after I see a code again I think the problem is here

jQ('#frm').submit(function() {
        jQ.ajax({
            **type: 'POST',**
            url: jQ(this).attr('action'),
            data: jQ(this).serialize(),
            success: function(data) {
                jQ('#result').html(data);
            }
        })
        return false;
    });
})

I think the problem is that bold...
only insert just type 'POST' not 'FILES'

so the question is how to use type 'POST' and 'FILES' together so type 'POST' and 'FILES' will insert to database...

Member Avatar for LastMitch

so the question is how to use type 'POST' and 'FILES' together so type 'POST' and 'FILES' will insert to database...

I think I ask you before if you can insert the info into the database?

1) Do you want to upload the image to the Database then select from image table?

You mention Yes.

But now you mention you can't? You mention before that the code was working fine separately but now you put all the code into index.php and it doesn't work? What is the issue? I'm not familiar with JQuery unless you mislead me thinking it's the database issue?

ok, thanks before for your answer...
i think, the mistake in not in php code,
but in validation code using jquery ajax..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.