Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] adding new select field with group members as a value dynamically


  • Kiciana
    Participant

    @kiciana

    Hi

    I want to be able to add a select field in my group panel extension, and in order to do that I have three files – main plugin file, JavaScript file and php to which I send the request with AJAX post function. The thing is it’s not working

    My html element with which I trigger the function

    <button class="addnew" onClick="return false" onmousedown="swapContent()">Add Another Field</button>
    			
    <div id="testing"></div>

    the jQuery part

    function swapContent() {
    	var url = "select.php";
    	$.post(url, function(data) {
    		$('#testing').html(data).show();
    	});
    }

    And the select.php file

    <?php	
    $group_id = bp_get_group_id();
    $has_members_str = "group_id=" . $group_id;
    if ( bp_group_has_members( $has_members_str ) )
    : ?>
    <select name ="member">
    <?php while ( bp_group_members() ) : bp_group_the_member(); ?>
    				
    <option value="<?php bp_group_member_name() ?>"> <?php bp_group_member_name() ?> </option>
    
    <?php endwhile; ?>
    </select>
    <?php else: ?>
    <h2>Not in any group.</h2>
    <?php endif;?>

    I have the javascript registered with:

    function formAdd_init() {
        wp_enqueue_script( 'formAdd-js', plugins_url( '/js/formAdd_.js', __FILE__ ));
    }

    Do I need to do smth similar with select.php?

    Usually I saw developers getting the content from php file with AJAX using only echo, might it be that $.post gets only echo content?

    Thanks in advance

Viewing 3 replies - 1 through 3 (of 3 total)

  • shanebp
    Moderator

    @shanebp

    Read re wordpress ajax.


    Kiciana
    Participant

    @kiciana

    Thanks for that. I read through it and I wanted to add the example function for admin footer to my plugin to check how it works but it crashes wordpress when I try to access the Dashboard.

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    I used this code:

    <?php
    add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here
    
    function my_action_javascript() { ?>
    	<script type="text/javascript" >
    	jQuery(document).ready(function($) {
    
    		var data = {
    			'action': 'my_action',
    			'whatever': 1234
    		};
    
    		// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    		$.post(ajaxurl, data, function(response) {
    			alert('Got this from the server: ' + response);
    		});
    	});
    	</script> <?php
    }
    <?php 
    
    add_action( 'wp_ajax_my_action', 'my_action_callback' );
    
    function my_action_callback() {
    	global $wpdb; // this is how you get access to the database
    
    	$whatever = intval( $_POST['whatever'] );
    
    	$whatever += 10;
    
            echo $whatever;
    
    	die(); // this is required to terminate immediately and return a proper response
    }

    Any ideas what goes wrong here?


    shanebp
    Moderator

    @shanebp

    Works properly here. Check your error logs.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] adding new select field with group members as a value dynamically’ is closed to new replies.
Skip to toolbar