Created
June 19, 2014 10:38
-
-
Save ahmadnaser/43819098008907afc5e7 to your computer and use it in GitHub Desktop.
custom ajax , database insert in php wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // custom Ajax function | |
add_action( 'wp_footer', 'addvisit_javascript' ); | |
function addvisit_javascript() { | |
?> | |
<script type="text/javascript" > | |
var thestartercounter=0; | |
jQuery(document).ready(function($) { | |
$('.ans-starterbutton').click(function(){ | |
thestartercounter++; | |
if(thestartercounter==3){ | |
thestartercounter=0; | |
var data = { | |
'action': 'addvisit_action', | |
'whatever': thestartercounter | |
}; | |
$.post('<?php get_site_url(); ?>/wp-admin/admin-ajax.php', data, function(response) | |
{ | |
console.log(response); | |
} | |
); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'wp_ajax_addvisit_action', 'js_to_php_add_visit_callback' ); | |
add_action( 'wp_ajax_nopriv_addvisit_action', 'addvisit_action_callback' ); | |
function js_to_php_add_visit_callback() { | |
global $wpdb; // this is how you get access to the database | |
$whatever = intval( $_POST['whatever'] ); | |
$whatever += 10; | |
$thisx = new ANS_Visit_Custom(); | |
$thisx->pharmacyId=1; | |
$thisx->username=1; | |
$thisx->visitDate=1; | |
$thisx->visitTime=1; | |
$thisx->notes=1; | |
$thisx->VisitTitle=1; | |
$thisx->VisitLinkType=1; | |
$thisx->VisitLink=1; | |
$thisx->VisitCat=1; | |
$thisx->VisitStatus=1; | |
$thisx->VisitOrder=1; | |
$thisx->dateCreated=1; | |
$thisx->dateModified=1; | |
$thisx->userId=1; | |
$thisx->ANSInsertVisit_Custom($thisx); | |
echo $whatever; | |
die(); | |
} | |
?> | |
<?php | |
class ANS_Visit_Custom { | |
public $id; | |
public $pharmacyId; | |
public $username; | |
public $visitDate; | |
public $visitTime; | |
public $notes; | |
public $VisitTitle; | |
public $VisitLinkType; | |
public $VisitLink; | |
public $VisitCat; | |
public $VisitStatus; | |
public $VisitOrder; | |
public $dateCreated; | |
public $dateModified; | |
public $userId; | |
function ANSInsertVisit_Custom($thisx) | |
{ | |
$result = ""; | |
global $wpdb; | |
$tableName = $wpdb->prefix."ANS_visits"; | |
if($thisx->VisitTitle == "" || $thisx->VisitLink == "") | |
{ | |
$result = 0; | |
} | |
else | |
{ | |
$wpdb->insert( | |
$tableName, | |
array( | |
'pharmacyId' => $thisx->pharmacyId, | |
'username' => $thisx->username, | |
'visitDate' => $thisx->visitDate, | |
'visitTime' => $thisx->visitTime, | |
'notes' => $thisx->notes, | |
'VisitTitle' => $thisx->VisitTitle, | |
'VisitLinkType' => $thisx->VisitLinkType, | |
'VisitLink' => $thisx->VisitLink, | |
'VisitCat' => $thisx->VisitCat, | |
'VisitStatus' => $thisx->VisitStatus, | |
'VisitOrder' => $thisx->VisitOrder, | |
'dateCreated' => $thisx->dateCreated, | |
'dateModified' => $thisx->dateModified, | |
'userId' => $thisx->userId | |
), | |
array( | |
'%d', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s' | |
) | |
); | |
$result = $wpdb->insert_id; | |
} | |
return $result; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment