• Resolved SkyRanger

    (@skyranger)


    I am pretty new to coding wordpress plugins. What I am wondering is how I go about updating data from a form. I know it uses option_update but not sure in what syntax i require this. This is the code I am working with:

    <form id="wintersnow-settings" action="">
    
        <label>Set number of snowflakes:</label>  <input name="numofflakes" size="5"  value="<?php echo get_option('numofflakes') ?>" type="text" /> (more than 30 - 40 not recommended)<br />
    
        <label>Set max size of flakes:</label> <input name="maxflakesize" value="<?php echo get_option('maxflakesize') ?>" type="text" /> (recommended 30)<br />
    
        <label>Set min size of flakes</label> <input name="minflakesize" value="<?php echo get_option('minflakesize') ?>" type="text" /> (recommended 8)<br />
    
        <label>Snow Location:</label> <select name="snowloc" size="1">
    				<option selected="" value="1">All Over Snowing</option>
    				<option value="2">Left Side Snowing</option>
    				<option value="3">Center Snowing</option>
    				<option value="4">Right Size Snowing</option>
    			</select><br />
    
        <input type="submit" value="Update" />
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter SkyRanger

    (@skyranger)

    The problem I am having is figuring out how to get the data from the form and have it update the mysql. Any help would be greatly appreciated.

    Thread Starter SkyRanger

    (@skyranger)

    Anybody able to give me a hand or atleast point me in the right direction.

    Moderator bcworkz

    (@bcworkz)

    Your form action must be set to go to a .php page on your server that will handle the form submit. You also need to specify a method, normally either GET or POST. There are benefits and drawbacks to each that you can research further.

    The .php page should be in the WordPress folder structure, in a place where it is safe from updates. I would create a plugin, even though you may not need it for this particular purpose, it’s a good place to store your custom hacks in general. The php page needs to have the line require_once('yourpath/wp-load.php');
    near the top of the page inside a <?php ?> code block. Be sure the yourpath path spec is correct for your server environment. This will afford you access to all WP functions, including the global $wpdb object which is useful for adding data to the database.

    In php, you access the values passed from form submits using the associative superglobal arrays $_GET or $_POST, depending on your form method. The array values are associated to keys matching the tag names of your form fields. Thus your php code will extract the value from that array, process it some way, then insert it into the database by whatever means seem most appropriate.

    It’s extremely vital that you process form values in some way to validate the input and restrict spurious data that could be used in an SQL injection attack. For instance, if a field is asking for a positive integer that will always be less than 1000, then reject all input that is not numeric or is longer than 3 characters.

    That’s the general process, I’ve skipped over much detail which you should be able to research on your own. Happy Coding!

    Thread Starter SkyRanger

    (@skyranger)

    Thanks bcworkz. Writing forms to submit to database with PHP, is not very difficult. That form is part of a plugin that I am creating, I got the initial add and view working. I was just wondering if there is anything special I need to do for wordpress because I noticed that pulling and adding data to mysql is different than regular php. I was wondering if update is the same way?

    Thread Starter SkyRanger

    (@skyranger)

    too much of a headache, giving up. Will search to find something similar that is already created.

    Can’t get my head around wordpress coding.

    function updatewintersnow {

    update_option() <—Not sure what do do with form output here. Guess lots more to learn.

    Thanks to all for your help

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Update form’ is closed to new replies.