php - Insert and select data from MySQL in WordPress Page -
i designing wordpress website, thorough design part, next phase database related things.
1. insert data database. 2. select data , display in page. 3. need create php page if dont want install widget.? need place code?
i have googled plugin , got php code widget
plugin , inserted 1 of pages widget
i tried installing insert_php plugin not working continuing php_code_widget
pages-> pages-> home page -> add row -> add widget -> php code widget.
now in mysql database have simple table called rituals having 3 columns
ritual id-> int -> auto increament. ritual_name-> varchar ritual_active-> varchar.
now need insert ritual name database, , reference got code , have put in php code widget window.
<?php require_once('../../../wp-load.php'); function insertuser(){ if(isset($_post['submit']){ global $wpdb; $rname=$_post['rname']; $ractive=$_post['ractive']; $table_name = $wpdb->prefix . "mahathiwp"; $wpdb->insert($table_name, array ('ritual_name' => $rname, 'ritual_active' => $ractive) ); } ?> <form action="" method="post"> ritual name: <input type="text" name="rname" /><br><br> ritual active: <input type="text" name="ractive" /><br><br> <input type="submit" name="submit"/> </form> <?php } insertuser(); ?>
data not getting inserted.
can suggest proper , faster way insert data database , retrieve data , show in wordpress page. appreciated.
you have customize theme,
add code make action while form submitted.
functions.php
function childtheme_style_andscripts(){ //wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css' ); wp_enqueue_script('ajax-function', get_stylesheet_directory_uri() . '/js/ajaxfunction.js', array('jquery'), '1.0', true ); wp_localize_script( 'ajax-function', 'usersubmitform', array( 'url'=> admin_url('admin-ajax.php'), 'security'=> wp_create_nonce('our-nonce') ) ); } add_action('wp_enqueue_scripts','childtheme_style_andscripts'); function form_action_function(){ require_once(dirname( __file__ ).'/../../../wp-load.php'); $data = $_post['data']; global $wpdb; if( !check_ajax_referer('our-nonce', 'security' ) ){ wp_send_json_error('security failed'); return; } //var_dump($data); $rname=$data['rname']; $ractive=$data['ractive']; $table_name = "rituals"; $wpdb->insert($table_name, array ('rname' => $rname, 'ractive' => $ractive) ); $wpdb->show_errors(); $wpdb->print_error(); echo 'from submitted successfully'; die(); } add_action('wp_ajax_nopriv_form_action_function','form_action_function'); add_action('wp_ajax_form_action_function','form_action_function');
custom page template
<?php /** template name: form user */ get_header(); ?> <div id="main-content" class="main-content"> <?php if ( is_front_page() && twentyfourteen_has_featured_posts() ) { // include featured content template. get_template_part( 'featured-content' ); } ?> <div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <h1 class="headingform">user form</h1> <div class="msg"></div> <form class="userform"> ritual name: <input type="text" id="rname" name="rname" /><br><br> ritual active: <input type="text" id="ractive" name="ractive" /><br><br> <input id="usersubmit"type="submit" value="submit" /> </form> </div><!-- #content --> </div><!-- #primary --> <?php get_sidebar( 'content' ); ?> </div><!-- #main-content --> <?php get_sidebar(); get_footer();
ajax-admin.js
here have use ajax why file created.put file theme js folder.
jquery(document).ready(function($){ var submitbutton = document.getelementbyid('usersubmit'); var ajaxfunctionformprocess = function(fromdata, action){ $.ajax({ type:'post', url: usersubmitform.url, data:{ action:action, data:fromdata, security:usersubmitform.security, }, success:function(reponse){ $('div.msg').html(reponse); }, error:function(response){ alert(response); } }); } submitbutton.addeventlistener('click', function(event){ event.preventdefault(); var fromdata = { 'rname':document.getelementbyid('rname').value, 'ractive':document.getelementbyid('ractive').value, }; ajaxfunctionformprocess(fromdata, 'form_action_function'); }); });
Comments
Post a Comment