Skip to content

Instantly share code, notes, and snippets.

@beyazitkolemen
Last active December 10, 2015 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyazitkolemen/4512751 to your computer and use it in GitHub Desktop.
Save beyazitkolemen/4512751 to your computer and use it in GitHub Desktop.
jquery-json post vs
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
function onSuccess(data, status)
{
data = $.trim(data);
$("#basariylaeklendi").text(data);
$("#ekle").reset();
}
function onError(data, status)
{
// hata
}
$(document).ready(function() {
$("#submit").click(function(){
var formData = $("#ekle").serializeObject();
$.ajax({
type: "POST",
contentType: 'application/json; charset=utf-8',
url: "http://example.com/sadeada.php",
cache: false,
data: formData,
dataType: 'json',
success: onSuccess,
error: onError
});
return false;
});
});
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<title>xxx.Com</title>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header">
<h1>
</h1>
</div><!-- /header -->
<div data-role="content">
<p>xxxx</p>
            <form id="ekle">
<div data-role="fieldcontain">
<label for="baslik">Başlık:</label>
<input type="text" name="baslik" id="baslik" value="" />
</div>
<div data-role="fieldcontain">
<label for="aciklama">Söz:</label>
<textarea name="aciklama" id="aciklama"></textarea>
</div>
<p id="basariylaeklendi"></p>
<button data-theme="b" id="submit" type="submit">Ekle</button>
</form>
</div><!-- /content -->
</div><!-- /page -->
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
<?php
include_once('../wp-config.php');
$jsoncevir = json_decode(stripcslashes($_POST));
$vbaslik = $jsoncevir['baslik'];
$vaciklama = $jsoncevir['aciklama'];
if(!empty($vbaslik) && empty($vaciklama))
{
$ver=$wpdb->escape($ver);
$my_post = array();
$my_post['post_title'] = $vbaslik;
$my_post['post_content'] = $vaciklama;
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['filter']= true;
$my_post['post_category'] = array(5374);
wp_insert_post( $my_post );
echo "Başarıyla Eklendi";
}
else
{
echo "Hata";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment