Forum Replies Created

Viewing 15 replies - 16 through 30 (of 34 total)
  • Thread Starter gshell

    (@gshell)

    @sjaure,
    Thank you for the great response. It has been very helpful. It is difficult for me to diagnose something like the ajax exchange as I can’t tell whether the problem is with the ajax data being sent to the server or the response coming back. Your response has helped me out in that regard. Thank you very much.

    By looking https://your-site-url/wp-admin/admin-ajax.php I was able to see that nothing changed when the submit button was clicked, even though I included a jquery alert in the JS file that let me know that it had been clicked. I am now reviewing information console.log to see if I can determine why. (Network panel F12 key on Edge Browser). I will let you know what I learn. Thanks for the help.

    Thread Starter gshell

    (@gshell)

    Hi @sjaure,
    Thank you. That helped very much. Particularly the simple example.

    I’ve modified the JS file as follows:

    var $jq = jQuery.noConflict();
    var g_name = document.getElementById("ajax_guest_name").value;
    $jq.ajax({
    	url : ajax_test.ajax_url,
    	type : 'post',
    	data : {
    		'action': 'my-ajax-test',
    		'ajax_guest_name' : g_name
    	},
    	success:function(data) {
    		// This outputs the result of the ajax request
    		console.log(data);
    	},
    	error: function(errorThrown){
    		console.log(errorThrown);
    	}
    }); // End of AJAX function
    

    A couple of questions:
    1 – What does the URL name ajax_test.ajax_url mean? Reading the documentation states that wordpress ‘gives us a unified file to use – wp-admin/admin-ajax.php’ which is included in the localize script command.
    2 – I believe I have the type and the data array correct now
    3 – what is the ‘console.log’ and how do I view/display it?

    The my-ajax-test.php file has the following immediately below the HTML form creation.

    <?php
    add_action( 'wp_ajax_my-ajax-test', 'my_ajax_callback' );
    
    function my_ajax_callback() {
    	$guest_name = $_POST[ajax_guest_name];
    	echo $guest_name;
    	die();
    }
    

    Note in the add_action line, I used dashes instead of underlines as I did in the my-ajax-test.php file. I’m not sure if they were supposed to match or not.

    No errors when clicking the Register button, but no results echoed either.

    Thread Starter gshell

    (@gshell)

    I feel like I am learning to play the piano and have found 8 keys in a single octave. I can plunk out a melody, but it isn’t vey rich and there are another 80 keys that I have no idea where they are or what they sound like. Anyway those 8 keys will help me plunk out a few more melodies until I hit the next hurdle or decide to explore what some of the other keys do.

    I wound up using one page for the input form and a second to process the data. Given the page approach I am using, I could make a single page update the database table but it didn’t update the input form page the user was viewing until you refreshed that page. From what I have read, that is where the AJAX approach would be more useful. I’ll read up on that and I also have access to another plugin code, which I understand how it works on the user side, that does something slightly similar (at least it reads and writes to the database tables) that uses the AJAX approach that will be of help in that learning process.

    Anyway, I have the site working functionally where I can retrieve and update values in the SQL database table, and it is not too ‘clunky’ from the user perspective. That will allow me to do a lot of what I want to do near term.

    Again, thank you for all your help explaining how this worked. In all the reading and research I had done, nothing or no one explained it as well as you did.

    Thread Starter gshell

    (@gshell)

    Viola! Finally got it to work. I don’t know how to thank you. I believe I now understand better what is going on. I was thinking that ‘submit’ or ‘save’ was actually storing data somewhere that could be viewed, edited, or manipulated later. In fact, the data appears to be simply appended to the communication string between the client and server (HTTP request). Since the data values are appended to the request sent to the server, the server (via php) can strip out the data for it’s own processing if needed. Am I even close to right?

    I was confused by the form action=”xxxx” command requiring a ‘valid URL’. That made me think the data was actually going to that URL. It isn’t. It’s just tagging along with a request to display that URL and if you want the data to physically show up in that URL, you can add some php code to process the data that was tagging along and display it, or do whatever you want with it. Now I can trash the output pages and simply use action=”” to return to the current page, have the php code process the data, and append some text to the page content like “Your Profile Has Been Updated”.

    I understand the error about omitting the method statement in the form. If I hadn’t been so frustrated, I should have caught that. Probably came from simply copying working code from W3Schools examples without looking closely at what they were doing. Just lazy on my part.

    Now that I understand more of what is happening with ‘submit’ I want to think about how I process the data. I suspect it will all be in one template. I was just using the shortcode approach to verify that I was getting data somewhere.

    Yes, all of this is being done on pages that are member only. Presently the menus don’t show up unless the user is logged in, but I plan to additional verification check at the beginning of each function later that confirms the user is logged in (just in case someone accesses the pages without being logged in). For this particular task (editing the member’s profile data). I plan to put the data into a database table called wp_club_members that I have created with phpmyadmin. I could have added necessary columns to the wp_users table, or the usermeta table, but I didn’t want to mess around with the WP default tables until I was sure I knew what was going on. I didn’t want to mess things up. Much easier to just dump my own table and start over than screwing up one of the WP default tables. However, there will be similar functions I want to create later that may go into different tables or maybe go directly to a different page.

    I do have one question. What is the advantage of creating a template page for the form input versus simply writing a function that creates the form? Then assign it a shortcode and add that shortcode to a new page. This form page (Edit My Profile) is only going to be used one time on one page. I think of a template as something I plan to use in more than one place for a number of similar pages. For instance I already created a template page that removes the column options and sidebar in the twentyseventeen theme, as any page that needs to display a large table of information needs to use all the space it can whereas a blog post or other pages look much better in the twenty seventeen default format. I suspect I will use that template several times.

    Again, I want to thank you for all the time you have spent on this. I will keep you informed as to my progress. I am sure there will be other hurdles to overcome. I appreciated the link regarding AJAX. Long term I am sure that will become something I want to learn as well. Thank you! Thank you! Thank you!

    Thread Starter gshell

    (@gshell)

    bcworkz,
    Thanks for tolerating my lack of knowledge, but I am beginning to understand more things. Since the form code states to ‘post’ data via an ‘action’ = some URL page, I expected to be able to see it. From what I understand now, the submit simply creates an HTTP request on the server that can not be viewed directly. I still can not get it to work. Here is what I’ve done:
    I created a new page in WP called action_php.php. I created a template page called template-with-simple-form.php in my child theme folder. The form code in that template looks like this:
    When I first attempted it I was using action=”/action_php.php”, but I kept getting an error message that said URL not found, so I hardcoded the full URL with what was actually displayed in my browser.

    /* Start the Loop */
    while ( have_posts() ) : the_post();
    get_template_part( 'template-parts/post/content', get_post_format() );
    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :
    comments_template();
    endif;
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    <h2>HTML Forms</h2>
    <form action="https://localhost:81/wordpress/action_page-php/">
      First name:<br>
      <input type="text" name="firstname" value="Mickey">
      <br>
      Last name:<br>
      <input type="text" name="lastname" value="Mouse">
      <br><br>
      <input type="submit" value="Submit">
    </form> 
    <p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>
    </body>
    </html>
    <?php
    endwhile; // End of the loop.

    I then created a new page using this template. All looked fine and ‘submit’ seemed to work. At least there were no error messages.

    I added the following function to functions.php in my child theme.

    function my_test_function() {
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    Welcome <?php echo $_POST["firstname"]; ?><br>
    Your last name is: <?php echo $_POST["lastname"]; ?>
    </body>
    </html> 
    <?php
    }
    add_shortcode( 'My-Test-Code', 'my_test_function');

    I placed the shortcode on a new page. It displayed the text strings properly, but did not have any data where the form values of firstname and lastname should have been echoed from $_POST.

    How does $_POST know which URL the submitted data is to be found? Clearly each form can have a different action=URL.

    What am I missing?

    I am not entirely sure the problem is with the code. A while back I installed Ultimate Member on an initial localhost environment and I also had problems getting any of their forms to update data. That may be related or not. This is a completely different install of WP.

    Thread Starter gshell

    (@gshell)

    bcworkz,
    Thanks for responding. What I meant by ‘temporary’ is that I don’t understand why I need to create another page/post to submit the form data to. I already have a page with the form filled out with all the variables I need. I just want to submit those variables to another routine that can write them into the database table. They’re already on the page being displayed, why do I need to create another output page for processing? It was relatively simple to pass information from the server side (PHP) to the client side (HTML). But I don’t understand how to pass it back.

    Let me try to explain more of what I am trying to do. I’ll probably have a lot of terminology incorrect, so bear with me. I am trying to build a simple membership website for a small social organization. I have created a new database table called wp_club_members using phpmyadmin with all the column fields we want to track. I have a few ‘members’ added to this database table for testing. When going live, I plan to bulk upload our current members. We only get a couple of new members each year, so there is no pressing need to automate ‘registration’ or the like. I would like the members to be able to edit their membership profile (most of ‘their’ fields in the database table). I can extract their email from their WP user information.
    $member_email = $current_user->user_email;
    Since their email address is also in the wp_club_members table, I can use $wpb to retrieve all of the other values in the table. i.e.:

    $my_profile = $wpdb->get_row("SELECT * FROM $table_name WHERE member_email = '$member_email' ", ARRAY_A, 0 );
    $member_first_name = $my_profile[first_name];
    $member_last_name = $my_profile[last_name];
    . . . etc.

    I know there could be a conflict if the member changes their member email and doesn’t change their WP user email, but it is a very very small organization and not a big deal. Once I get everything working, I suspect I can find a way to check this and handle it.

    I then create a form that inputs all of those variables into the input fields as the default.

    All of the above is in a xxxx.php file that also creates a shortcode for the above function. I created a WP Page titled Edit My Profile and added the shortcode. All is well until it comes time to submit any changes the member has made. So far I have been unable to get the form data to go anywhere that I can view it, edit it, or manipulate it. If I can get the output somewhere, then I believe I can extract it and process the data back into the database table via $wpdb.

    Alternatively, as I mentioned before is to not have a ‘Submit’ function inside the table at all, but rather add a normal HTML input button immediately following the form which can be made to execute a javascript function. I’ve never worked with javascript before, but have been doing some research and it doesn’t seem like a mountain too tall to climb, and I may just bite the bullet and go this direction, although I suspect in time, I will need to learn both approaches. I don’t know if I can use the WP $wpdb object from within javascript, or whether it would just be easier to connect to the database directly and use SQL to update the table values.

    Thanks for your assistance. And if you can recommend any tutorials that explain the ins and outs of simple WP database manipulation it would be greatly appreciated. The coding is rather simple, it’s just another language, the logic is all the same and with time you can get the syntax correct. The big hurdle for me is understanding how to manipulate all the wordpress functions and capabilities.

    Thread Starter gshell

    (@gshell)

    bcworkz,

    Thanks for your detailed reply. I have picked up a lot from existing plug-ins, but as you note, most of them are very complex. I’ve also picked up a lot from W3 Schools, but as you note, it doesn’t deal with the integration within WP. Clearly there is a hole in the learning tools. Many of the generic plugins don’t do exactly what I would like to do, and besides I would rather learn how to do things myself. It gives me a better ability to fix issues when I eventually go live if I understand what is happening. And there will be issues. Actually I feel like I have made a lot of progress working on this in my spare time over the last couple of weeks.

    I think I understand the gist of your recommended page template approach. I’ll work on that method.

    I am just surprised that it was so easy to create the database with phpmyadmin and extract that data via $wpdb->get_var into HTML and there is not as simple a method to do the inverse. Maybe after I understand what is going on, it will also seem simple. It just doesn’t come naturally to me that I need to take data from an input form and send it to a page or post within the website in order to then process it through php back into the database. The web site will likely become cluttered enough without adding temporary storage posts or pages.

    Again, thanks for your input.

    Thread Starter gshell

    (@gshell)

    The PHP code receiving the POSTed data

    That’s really my problem. How do I ‘submit’ the data array of variables to a function included a xxx.php file in the plugin directory so that it can extract the individual variables to feed into $wpdb?

    I’m not really sure ‘submit’ is what I really want to do in the first place. Right now, I have a php function which defines a variable for each data element in the database using $wpdb->get_var. That function then creates an HTML form and fills the default fields with those variables. The user can edit the information in any of the fields. The desire is to ‘Save’ changes to a particular row back into the database using $wpdb->update. I have explored the idea of creating an HTML input button immediately below the HTML form, rather than the ‘submit’ button inside the form, as it can be directed to an internal function via onclick=”myFunction()”. The problem I seem to have encountered with this approach is that it appears this function must be a javascript function rather than a php function and I have even less knowledge of javascript that I do php and HTML.

    Right now I am not worried about sanitizing and validating data as I am working in a localhost environment. That’s for later after I get things working.

    Forum: Developing with WordPress
    In reply to: wpdb help
    Thread Starter gshell

    (@gshell)

    OK, I think I got it.

    		<th>Home Phone</th> 
    		<th>Cell Phone</th> 
    	  </tr>
    <?php for ($cur_row = 0; $cur_row <= $last_row; $cur_row++) { ?>
    			  <tr>
    				<td><?php echo $wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 1, $cur_row); ?></td>
    				<td><?php echo $wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 2, $cur_row); ?></td>
    				<td><?php echo make_clickable($wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 3, $cur_row)); ?></td>
    				<td><?php echo $wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 4, $cur_row); ?></td>
    Forum: Developing with WordPress
    In reply to: wpdb help
    Thread Starter gshell

    (@gshell)

    certainly not as clear as what others post. I’ll try some more
    ` global $wpdb;

    $table_name = $wpdb->prefix . “club_members”;
    $members = $wpdb->get_var( “SELECT COUNT(*) FROM $table_name” );
    echo “<p>The number of rows in the member table is {$members}</p>”;

    /*$field = $wpdb->get_var( “SELECT * FROM $table_name”, 1, 0); */
    /*return “<p>Field 1,0 in the member table is {$field}</p>”; */

    $first_row = 0;
    $last_row = $members – 1;
    /* echo “<p>Last row number in the member table is {$last_row}</p>”; */

    Forum: Developing with WordPress
    In reply to: wpdb help
    Thread Starter gshell

    (@gshell)

    I’ll try the code button without spaces

    <tr>
    		<th>First Name</th>
    		<th>Last Name</th> 
    		<th>Member Email</th> 
    		<th>Home Phone</th> 
    		<th>Cell Phone</th> 
    	  </tr>
    • This reply was modified 6 years, 7 months ago by gshell.
    Forum: Developing with WordPress
    In reply to: wpdb help
    Thread Starter gshell

    (@gshell)

    I believe I found my error. I now have it working.

    Can anyone explain how to enter code properly in a forum post? I tried clicking on the code button in the text box, and entering < pre > or < code > before the post, none of them seemed to display the pasted code properly. < pre > seemed to work the best (without spaces)

    Thanks

    • This reply was modified 6 years, 7 months ago by gshell.
    • This reply was modified 6 years, 7 months ago by gshell.
    • This reply was modified 6 years, 7 months ago by gshell.
    Forum: Developing with WordPress
    In reply to: wpdb help
    Thread Starter gshell

    (@gshell)

    Or this:

    global $wpdb;

    $table_name = $wpdb->prefix . "club_members";
    $members = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
    echo "<p>The number of rows in the member table is {$members}</p>";

    /* Determine the logged in member from email address*/
    global $current_user;
    get_currentuserinfo();
    echo "<p>'User email: $current_user->user_email'</p>";

    $member_email = $current_user->user_email;
    echo "<p>The member email is: {$member_email}</p>";

    /* Find member_id (row number) in club_members database from email address*/
    $tmp = $wpdb->get_var( "SELECT member_id FROM $table_name WHERE member_email=\'[email protected]\ " );
    echo "<p>Member ID is {$tmp}</p>";

    $email = $wpdb->get_var("SELECT user_email FROM wp_users WHERE user_login = 'guest' ");

    // Echo the user's email address
    echo "<p>Guest email is {$email}</p>";

    Forum: Developing with WordPress
    In reply to: wpdb help
    Thread Starter gshell

    (@gshell)

    I’m not sure how to get that code to display properly.

    Let me try this:

    	global $wpdb;
    	
    	$table_name = $wpdb->prefix . "club_members";
    	$members = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
    	echo "<p>The number of rows in the member table is {$members}</p>";
    	
    /* Determine the logged in member from email address*/
    	global $current_user;
        get_currentuserinfo();
    	echo "<p>'User email: $current_user->user_email'</p>";
    
    	$member_email = $current_user->user_email;
    	echo "<p>The member email is:  {$member_email}</p>";
    
    /* Find member_id (row number) in club_members database from email address*/
    	$tmp = $wpdb->get_var( "SELECT member_id FROM $table_name WHERE member_email=\'[email protected]\ " );
    	echo "<p>Member ID is {$tmp}</p>";
    	
    	$email = $wpdb->get_var("SELECT user_email FROM wp_users WHERE user_login = 'guest' ");
    
       // Echo the user's email address
       echo "<p>Guest email is {$email}</p>";
    	
    
    Thread Starter gshell

    (@gshell)

    Thanks,
    I got it working. I had entered the global $wpdb at the beginning of the php file that will eventually contain several functions. I did not realize that I needed to enter it again inside of each function.

    Also I didn’t notice the ‘code’ button. Thanks for the insight. I will use it in the future.

Viewing 15 replies - 16 through 30 (of 34 total)