WordPress.org

Ready to get started?Download WordPress

Forums

Retrieving table data for inclusion in a form (2 posts)

  1. jac0bean9
    Member
    Posted 1 year ago #

    Hi,

    I am so sorry to ask for a lot of help, but I am just starting out with coding and would love any aid you could offer.

    I am working on my first plugin, one for keeping track of sports statistics for my school newspaper. I am up to the point of working on the parts which I had put off because I wasn't sure how to do them, which is why I have so many questions. Right now, I am trying to work on the actual part where the user inputs statistics.

    The idea would be that the user (editor) would input the name of a team and what year it is playing in, which would be enough information to find a table including all of the players and statistic types of the team. Then, potentially, a table would load to allow the user to input what amount of each statistic each player had in the game, hit a "submit" button, and send the data off to the table (which I have built already) for retrieval later.

    The trouble is, I do not have enough expertise to know how to accomplish these steps. I'll explain my problems step-by-step:

    1. User enters team name and year
    2. How should the data be sent off? Should I use an html form for getting the name and year? Something else? Once that data is sent, how can I have the page interact with the database? I'm afraid that I don't know AJAX or javascript well enough to know what method to use.

    3. Team table is found, extracting players and statistics
    4. I assume that this would be done with php and SQL/$wpdb, but how would the data be returned to the page and written?

    5. Statistic and player list is used to allow user to input data
    6. Is it possible to create a table for the statistics and players in an html form? Should I use a dropdown menu and "add players" below somehow?

    7. Data from statistics is sent to the SQL table
    8. I think I know how to do this part. I would think that an html form sending data to a php file to add to the table would work, unless I am missing something.

    Seeing as I am just starting up, I would love to hear any alternative ways of accomplishing any of this. To be clear, I have already written the code for creating team tables, adding both the player and statistic information to those tables in accessible ways using html forms, but if I absolutely need to, I could rewrite all of that code in a better way. Thank you so much for any help you can offer!

  2. bcworkz
    Member
    Posted 1 year ago #

    Always remember there are limited ways the user's browser and your server can interact. The server sends out html and related text only on a request from the browser. For our purposes, the only valid requests are GET and POST. We'll ignore others, these two will serve all our purposes. Forms can use either method. Field values are sent as URL parameters for GET, so this only works for simple forms. POST fields are sent as a structured data packet, so can be used for any size form.

    All the following functions can be handled by a single php page by doing different things based on request type and the presence or lack of parameters. Or you can use multiple pages if that makes more sense to you. You don't need AJAX to do any of this, you could add this functionality later to enhance the interactivity of the forms.

    1. Yes, when browser requests initial page, a GET with no parameters, send a html form where the target is your php page and the method is GET. When the form is submitted, the url will look something like http://example.com/teamstats.php?name=panthers&year=2010.

    2. The fact the above request is a GET with name and year parameters, your page will know to retrieve the associated data (using $wpdb methods) and format it and send it to the browser, along with any required input forms. The data is sent by either: A) Closing the php code block with ?>, the remaining text will be sent as html to the browser until another php code block is started with <?php. Or B) By using echo or one of the print variants to send html to the browser from within a php code block. For our purposes to avoid AJAX, this will be sent as a new page, complete including doctype declaration and <html> tags.

    3. You can use any kind of html you want. Input will be by forms. You can use hidden fields so your php page knows which form is submitted in what context. The form action will be by POST, the target being the same php page, or a different one, as you prefer. It's best that your page not try to remember what data was previously sent. All needed info should be included in the current POST.

    4. If your php page gets a POST request, it knows it must update the tables using the posted data. Be sure you sanitize any input from any user to avoid SQL injection attacks. Only allow characters needed to convey the data, and limit the maximum length of text as well. In particular, disallow any special characters such as pluses and slashes unless they are really needed. For example, if you receive data in a field for average goals per game, only allow numbers and dots and commas, no letters or anything else. Limit the field size to say 6 or 7 numeric characters.

    You're on the right path, just need to flesh out all the details. The main thing is to keep in mind the limited ways browsers and servers can interact.

Topic Closed

This topic has been closed to new replies.

About this Topic