this is my jquery coding
$('#btnKirimPesan').click(function(){
$('#bantu').load('kirim_pesan.php', {'emailPenerima': $('#emailPenerima').val(), 'isiPesan': $('.isiPesan').val()});
});
then this is on kirim_pesan.php
<?
include('../../config/buka_koneksi_db.php');
$query = "INSERT INTO pesan(emailPenerima, emailPengirim, statusBaca, isiPesan, waktuPesan) VALUES((SELECT emailUser FROM user WHERE MD5(emailUser) = '".$_POST['emailPenerima']."'), '".$_SESSION['email']."', 0, '".$_POST['isiPesan']."', NOW('d/m/Y/H/i/s'))";
$hasilQuery = mysql_query($query);
if(mysql_affected_rows()>=1)
$hasil = 1;
else
$hasil = 0;
include('../../config/tutup_koneksi_db.php');
?>
The jquery coding and query inserting new row is in different file. I want to reload a div in jquery file(it's php file) if only $hasil = 1;
, if its == 0, I'll show dialog which say "your message has not been sent"...how can i do it??please help me...>,<...thx before
The load()
jQuery method places the HTML returned by the server inside the element. So, assuming that #bantu
is the ID of the div where you want to show the message, changing your kirim_pesan.php
file to the following should do it:
<?
include('../../config/buka_koneksi_db.php');
$query = "INSERT INTO pesan(emailPenerima, emailPengirim, statusBaca, isiPesan, waktuPesan) VALUES((SELECT emailUser FROM user WHERE MD5(emailUser) = '".$_POST['emailPenerima']."'), '".$_SESSION['email']."', 0, '".$_POST['isiPesan']."', NOW('d/m/Y/H/i/s'))";
$hasilQuery = mysql_query($query);
if(mysql_affected_rows()>=1)
echo "Message sent!";
else
echo "Your message has not been sent.";
?>
Documentation: http://api.jquery.com/load/
Using jQuery load()
you are loading the contents of the output of kirim_pesan.php
in the element with ID #bantu
. So anything you echo
for example in you php script, will appear in your #bantu
element:
if(mysql_affected_rows()>=1)
{
$hasil = 1;
echo "Data has been added";
}
else
{
$hasil = 0;
echo "Your message has not been sent";
}
Note that for a dialog to pop-up, you will need some additional javascript but if you are already using dialogs, that should not be that hard, just use the output of the php script.