• Hi

    I’m building a custom plugin to capture customer data.
    I have a function that, when called from the front, shows the form – like this (simplified for sake of brevity):

    function plugin_form() {
    ?>
    
    <form name="customer" method="post" action="">
    	<input id="name" name="name" value=""/><br/>
    	<input id="surname" name="surname" value=""/><br/>
    	<input id="email" name="email" value=""/><br/>
    	<input type="submit" name="submit" value="submit"/>
    </form>	
    
    <?php
    }

    My plugin creates the appropriate database tables during activation.
    I have this function to save the data:

    function save_data() {
    	global $wpdb;
    		$name = $_POST['name'];
    		$surname = $_POST['surname'];
    		$email = $_POST['email']; 	
    
    		global $wpdb;
    		$wpdb->query = "INSERT INTO wp_plugin ('name', 'surname', 'email') VALUES ('$name', '$surname', '$email');";
    		echo 'thanks';
    }

    The question is:

    How do I get the form appearing on the frontpage to call the save_data function in the plugin file?

    Any help would be greatly appreciated.

    Thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter dyrk

    (@dyrk)

    Can anyone help me with this. I’m sure it’s a simple solution. Would love to fix this today.

    Thread Starter dyrk

    (@dyrk)

    Anyone?
    I can’t imagine this to be a difficult one. Just need advice on syntax I suppose. All my future plugin development…for whatever plugin…will most likely need this bit of information. So any help would be greatly appreciated.

    leehalliday

    (@leehalliday)

    Hi,

    Did you find tutorial or anything for this?

    Thread Starter dyrk

    (@dyrk)

    Hi leehalliday

    I did in fact find a solution.
    The key is to use if(isset)

    So…

    function form() {
    if (isset($_POST['submit']) {
    // ADD SAVE STUFF HRE
    }
    else {
    // ADD FORM HERE
    }
    }

    So if your form has a submit button with name of submit, the if isset will check if the form has been submitted and then perform whatever you like with the data. if not…the form will show.

    Works very well. Not sure what the steps would be to do the same thing with a nice ajax response – haven’t tried, but probably possible.

    Hope this helps

    Thread Starter dyrk

    (@dyrk)

    Oh yes and very important….

    Make sure your form’s action is blank.
    Ie. <form action=”” method=”post”>

    Hi dyrk,

    I need to do the same thing and i don’t have a good clue on tweaking this php files, is your plugin available for use? in case not i will be grateful if can give me more guidance or refer me a tutorial on how to get about it.

    Thanks,

    Thread Starter dyrk

    (@dyrk)

    Hi embaeva

    The answer would depend on what you are trying to achieve.
    Are you trying to capture information in the database or merely sending it as an e-mail?

    dyrk

    dyrk,

    I would like to save it to the database and link that with the users posts.e.g, capturing information on an idea sponsor and link the sponsor with the owner of an idea.

    thanks,

    Hi dyrk,

    function plugin_form()
    function save_data()

    i want to know that both the function should be in on file or separate file.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Frontend form add data to database’ is closed to new replies.