无法使用AJAX将数据传递到数据库

时间:2017-05-10 20:37:46

标签: php jquery

我正在尝试使用AJAX将一些数据保存到数据库中。它说它已成功完成但没有任何反应。没有数据被保存。只是说“成功”。

index.php //表单

<div id="cont">
<div id="kayit" style="display:none;">
Success
</div>
<div id="basarisiz" style="display:none;">
Empty or Fail
</div>
<form method="post" id="yaz" onsubmit="return false">
<input type="text" name="title" id="baslik" placeholder="Başlık" required><br/>
<textarea name="content" ></textarea><br/>
<input type="submit" name="gonder" value="Gönder" id="gonder" onclick="kayit()">
</form>

yaz.js

function kayit()
{
var baslik = $("input[name=title]").val();
var icerik = $("input[name=content]").val();
if (baslik =="" || icerik == "")
{
$('#basarisiz').show(1);
$('#kayit').hide(1);
}else
{
$.ajax ({
type: "POST",
url: "yazikaydet.php",
data: $("#yaz").serialize(),
success: function (sonuc) {
if (sonuc == "hata") {
alert ("unable to connect to db");
}else {
$('#kayit').show(1);
$('#basarisiz').hide(1);
$("input[name=title]").val("");
$("input[name=content]").val("");
}
}
}) }} 

yazikaydet.php //将保存数据而不是处理的文件

<?php 
include 'giris.php'; //sessions and connection strings
if(isset($_POST["gonder"]))
{
    $baslik = $_POST["title"];
    $icerik = $_POST["content"];
    $tarih = date("d/m/20y");
    $kull = $_SESSION["username"];
    $kaydet = mysqli_query($connect,"INSERT INTO gonderiler (yazar,tarih,baslik,icerik) VALUES ('$kull','$tarih','$baslik','$icerik')");
    if($kaydet)
    {
        echo "Yes";
    }   
    else
    {
        echo "No";
    }
}
?>

0 个答案:

没有答案