WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] front-end ajax form plugin (3 posts)

  1. seborgarsen
    Member
    Posted 4 years ago #

    Hi,

    I'm having some brain freeze getting around building a plugin that updates the database in some custom fields:

    <?php
    /*
    Plugin Name: Ajax Playlists
    Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
    Description: A brief description of the Plugin.
    Version: beta 0.1
    Author: seb
    Author URI: http://URI_Of_The_Plugin_Author
    */
    ?>
    
    <?php function playlist_jquery() { ?>
    
    <script type="text/javascript" language="javascript">
    $(document).ready(function(){
    	$("#submit").click(function(){
    	$(".error").hide();
    		var hasError = false;
    		var playlist_nameVal = $("#playlist_name").val();
    		if(playlist_nameVal == '') {
    			$("#playlist_name").after('<span class="error">You forgot to enter a playlist name.</span>');
    			hasError = true;
    		}
    		if(hasError == false) {
    			$(this).hide();
    			$("#submit.button").append('<img src="<?php bloginfo('stylesheet_directory'); ?>/images/ajaxloader.gif" alt="Loading" id="loading" />');
    			$.post("<?php echo admin_url('admin-ajax.php'); ?>", $("#MakeNewList").serialize());
    		}
    		return false;
    	});
    });
    </script>
    <?php } ?>
    
    <?php
    add_action('wp_ajax_playlist_jquery', 'playlist_insert');
    ?>
    
    <?php function playlist_insert() {
    
    $table_name = $wpdb->prefix . "playlist_master";
    $playlist_name=$_POST['playlist_name'];
    $description=$_POST['description'];
    $setuserid=$_POST['setuserid'];
    $setadate=$_POST['setdate'];
    
    $insert = "INSERT INTO " . $table_name .
                " (playlist_name, user_id, description, date) " .
                "VALUES ('" . $playlist_name . "','" . $setuserid . "','" . $description . "','" . $setadate . "')";
    
    global $wpdb;
    $results = $wpdb->query( $insert );
    die();
    } ?>
    
    <?php
    add_action ('wp_footer', 'playlist_jquery');
    ?>

    the form on my page looks like this:

    <form action="" method="post" id="MakeNewList">
    Playlist Name:<br /><input type="text" name="playlist_name" id="playlist_name" /><br />
    Playlist Description:<br /><input type="text" size="25" name="description" id="description" /><br />
    <input type="hidden" value="<?php echo $getmother;?>" name="setuserid" />
    <input type="hidden" value="<?php echo $CurDate;?>" name="setdate" />
    <input type="submit" name="submit" id="submit" class="button" />
    </form>

    This works if I choose to put everything on the same page. Looking at firebug, the only response i get is a 0.

    I have a sneaking suspicion that $.post("<?php echo admin_url('admin-ajax.php'); ?>", $("#MakeNewList").serialize()); is the culprit here. I just don't have the mental capacity right now to see why.

  2. seborgarsen
    Member
    Posted 4 years ago #

    I did it!!!!

    jQuery('form#MakeNewList').submit(function() {
     var data = jQuery(this).serialize();
      jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data, function(response) {
  3. seborgarsen
    Member
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic