linkedin facebook linkedin facebook nod32

Php tilida MySql va JQuer da saytda ma`lumot qoldirish

Muallif: Mengliyev Sh.

Qo`shilgan sana: 2016-10-25

Php tilida MySql va JQuer da saytda ma`lumot qoldirish

Qadam1 –Saytda xat qoldirish uchun mysql tizimida jadval yaratish

PHP tilida xat yoki xabar qoldirish uchun bazada jadval yaratib olamiz. Buning uchun
http://localhost/openserver/phpmyadmin/index.php
Базы данных bo`limidan test1 baza yaratib sql bo`limiga quyidagi kodni joylashtiramiz.

<?php
$hostname="localhost"; // host nomi
$login="root"; // serverni yoqish admini
$pwd=""; // serverni himoyalash paroli
$db_name="test1";  // baza nomi
//baza bilan bog`lash
$con = @mysql_connect($hostname, $login, $pwd) or die("Error! connect-database");
mysql_select_db($db_name, $con) or die ("Error! select-database");                 
?>

id –xat yuboruvchining tartib raqani
id_article –Xat yuboruvchining id raqam.
name –xat qoldiruvchining ismi.
mail – e-mail manzili.
text – Xat qoldiruvchining mavzu matni.
date_add –Xat qoldiruvchining sanasini qayt etadi: misol uchun 22.07.2011 da 14:03.
public –muallifga : 1 yoki 0 qiymat beradi foydalanuvchi o`zing matnini qoldirganda 0 qiymatni qabul qiladi sayt
adminstraori tekshirib 1 qiymat berib matnni saytga chiqaradi .
config.php fayli baza bilan ma`lumotlarni bog`lydi.
Quyida kodi keltirilgan:

<?php
$hostname="localhost"; // host nomi
$login="root"; // serverni yoqish admini
$pwd=""; // serverni himoyalash paroli
$db_name="test1";  // baza nomi
 //baza bilan bog`lash
$con = @mysql_connect($hostname, $login, $pwd) or die("Error! connect-database");
 mysql_select_db($db_name, $con) or die ("Error! select-database");                 
?>

Qadam 2 – PHP tilida ma`lumor qoldirish.

Ma’lumot qo’shish

Ma’lumot qoldirish bo`limini tanlash add_comment.php faylni yaratish yordamida amalga oshiriladi. Quyida kodi keltirilgan:

<?php
// Ajax script yordamida ishlaydi
sleep(1);
header("Content-type: text/plain; charset=utf-8");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// yuboruvchi ma’lumotini utf-8 da tavsiflaydi
while(list ($key, $val) = each ($_POST)){$_POST[$key] = iconv("UTF-8","CP1251", $_POST[$key]);}
// sanani qo`shish
$date_add = date('d.m.Y da H:i');
// qo`shilish tavsifi
$nl = strlen($_POST['name']);
$ml = strlen($_POST['mail']);
$tl = strlen($_POST['text']);
$id_article = $_GET['id_article'];
$name = $_POST['name'];
$mail = $_POST['mail'];
$text = $_POST['text'];
if($nl<0 or $nl>60 or $ml<0 or $ml>60 or $tl<0 or $tl>500 or $_POST['nr']!='nerobot')
{$validate = false;}
elseif(!eregi('^[a-z0-9]+(([a-z0-9_.-]+)?)@[a-z0-9+](([a-z0-9_.-]+)?)+\.+[a-z]{2,4}$',$_POST['mail']))
{$validate = false;}
else{$validate = true;}
// agar ma`lumot yuborilgan bo`lsa
if($validate)
{
// Xabar qo`shish
include("config.php");
mysql_query("insert into comments (id_article, name, mail, text, date_add, public)
values ('{$id_article}', '{$name}', '{$mail}', '{$text}', '{$date_add}', '0')") or die ("Error! query - add_comment");
echo '<font color="green">Xabarni tekshirilayotibdi !</font>';
}
else
{
echo '<font color="red">To`ldirishda xato!</font>';
}
?>

Xatni saytga qo`shish

show_comments.php fayl yordamida saytga ma`’lumot chiqarish imkonini beradi.
Quyida kodi keltirilgan:

<?php
function show_comments($id_article)//xat qoldiruvchi sxemasi
{
 include("config.php");
 $res = mysql_query("select * from comments where id_article like $id_article and public = 1 order by id", $con) 
or die ("Error! query – show comments");
 while($arr = mysql_fetch_array($res, MYSQL_NUM))
 {  echo "
<div class=main>
    <img src=images/comentator.jpg>
      <div class=block_name>
 <span class=name>[2]</span>
 <span class=date>[5]</span>
</div>
 <div class=coment>
<div>$arr[4]</div>
 </div>
 </div> ";
}
}
?>

Qadam 3 –Javascript va Ajax funksiyasi


js_comments.js fayl yordamida quyidagi kodni kiritamiz
.
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

function ajax(param)
{
 var req = createObject();
 method=(!param.method ? "POST" : param.method.toUpperCase());
if(method=="GET")
{ send=null;
 param.url=param.url+"&ajax=true";
}
 else
 {
  send="";
 for (var i in param.data) send+= i+"="+param.data[i]+"&";
  send=send+"ajax=true";
}
req.open(method, param.url, true);
if(param.statbox)document.getElementById(param.statbox).innerHTML = '<img src="images/wait.gif"> Iltimos kutib turing...';
 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 req.send(send);
req.onreadystatechange = function()
{
  if (req.readyState == 4 && req.status == 200) 
 {
          if(param.success)param.success(req.responseText);
          }
      }
}
function toggle(id)
{
      var e = document.getElementById(id);
      var dh = gh(id);
      var elems = e.getElementsByTagName('*');
      
      if (e.style.display == "none")
      {

 for(var i=0; i=0;i-=5)
{ (function()
 {
              var pos=i;
              setTimeout(function()
              {
         e.style.height = (pos/100)*dh+"px";
        if (pos<=0)
        {
          e.style.display = "none";
          e.style.height=lh;
            }
         },1000-(pos*5));
        }
          )();
          }
          return true;
      }
      return false;
}

function vhe(obj, vh){obj.style.visibility=vh;}

function gh(id)
{
      var e = document.getElementById(id);
      if(e.style.display == "none")
      {
          e.style.visibility = "hidden";
          e.style.display = "block";
          dh = e.clientHeight||e.offsetHeight+5; // Высота
          e.style.display = "none";
          e.style.visibility = "visible";
      }
      else
      {
          dh = e.clientHeight||e.offsetHeight+5; // Высота
      }
      return dh;
}
function clearText(field)
{
      if(field.defaultValue == field.value)field.value = '';else if(field.value == '')field.value = field.defaultValue;
}

Qadam 4– Xat qoldirish bo`limi

comments.php faylini hosil qilish quyida keltirilgan.

<script Language="JavaScript" src="js_comments.js"> </script>
<center>
<div id="coments">
<div class="title">
<span>
<h2>Xabar qodirish:</h2>

</span>

</div>
<?php
include("show_comments.php");
show_comments('1');
?>
<div class="top">
<img id="addcomentbutton" onClick="toggle('addcoment'); location.href='#addcoment';" src="images/add_coment.png"/>
</div>
<div id="addcoment" class="addcoment" style="display:none;">
<form name="comment">
<div id="statusbox">Xabar qoldirish bo`limi!</div>
<input id="name" type="text" name="name" value="Ismi " maxlength="60" onfocus="clearText(this)" onblur="clearText(this)"/>
<input id="mail" type="text" name="mail" value="E-mail " maxlength="60" onfocus="clearText(this)" onblur="clearText(this)"/>
<textarea id="text" name="text" onfocus="clearText(this)" onblur="clearText(this)"></textarea>
<span>
<br/><input id="nr" onClick="document.getElementById('nr').value='nerobot';" type="checkbox" name="nr"/>
<b>Tanishdim...</b>
</span>
<img class="button_add" src="images/button_add.png" onclick='ajax({
url:"add_comment.php?id_article=1",
statbox:"statusbox",
method:"POST",
data:
{
name:document.getElementById("name").value,
mail:document.getElementById("mail").value,
text:document.getElementById("text").value,
nr:document.getElementById("nr").value,
},
success:function(data){document.getElementById("statusbox").innerHTML=data;}
})'
/>
</form>
</div>

</div>
</center>

Qadam 5 – Xat qo`shish fayli bu fayl yordamida dizaynini yaratish imkoni mavjud style.css fayli quyida keltirilgan.
style.css:
*{ margin: 0; padding: 0; }* 
a {color: #266ba8; text-decoration: none; cursor:pointer;}
a:hover { color: #995;text-decoration: none; }
h2 {font-size: 18px; font-weight:bold; 
color:#133b5e; padding:4px; 
margin-bottom:12px; 
border-bottom:1px solid #CCC;}
#coments {position:relative; left:0px;}
#coments  .title{position:relative;
 left:-12px; width:594px; 
height:70px; background: #EEE }
#coments  .title h2{margin:0;}
#coments  .title .left{float:left; padding:5px; width:500px;}
#coments .addcoment {position:relative; top:-15px; 
background: #EEE; border:5px 
solid #93afbc; display:none;}
#coments .addcoment img:hover{cursor:pointer;}
#coments  textarea{ width:535px; font-size:14px; 
height: 180px; margin:10px; overflow:hidden;
 background:#FFF; border:1px solid #DDD;}
#coments  input[type="text"]{padding-left:10px; 
width:525px; height:25px; font-size:15px;
 line-height: 1.8em; margin:10px 5px 0px 10px; 
border:1px solid #DDD;}
#coments  .addcoment span{position:relative; 
top:-10px; margin-right:180px; 
left:180px; font-size:20px; color:#292;}
#coments  #statusbox{padding-left:10px;
 line-height:55px; color:#888; font-size:18px; 
height:50px;}
#coments  img.button_add
{margin:10px 5px 0px 10px; 
padding:0; border:0; float:none;}
#coments .top 
{font-size:15px; 
height:47px; padding:15px 15px 0px 195px; 
color:#9e9968;}
#coments .main {position:relative;}
#coments .block_name {height:40px; 
position:relative; left:10px; top:10px;}
#coments .name {height:40px; font-size:17px;
 font-weight:normal; color: #070; float:left;}
#coments .date {height:40px; font-size:10px; 
position:relative; left:-10px; float:right; color: #555;}
#coments img{ float:left;}
#coments .coment{position:relative; 
left:-12px; width:554px; padding:19px 20px 10px 20px; 
border-bottom:1px solid #DDD; font-size:12px; 
background: #f7f4ca url(images/coment_top.png) 
left top no-repeat; text-align:justify;}

Quyida yaratilgan m`lumot qoldirish misolda aniq keltirilgan.

Ma`lumot qo`shish tugmasini bosish orqali qiyidagi javascript yordamida hosil qilinadi dizayn css usulida

Yuborilgan ma`lumotni sayt admini tekshirib ko`rib public 1 ni tanlab saytga qo`yishi mumkin.

Quyida kodni yuklab oling: yuklab olish 24,5 kb

 

402 marta o`qildi.

Parol:
Eslab qolish.


Ro`yhatdan o`tish

testing

+998915878681

Siz o`z maxsulotingizni 3D reklama ko`rinishda bo`lishini xohlaysizmi? Unda xamkorlik qilamiz.

3D Reklama


Рейтинг@Mail.ru
Рейтинг@Mail.ru

Besucherzahler
счетчик посещений