WordPress.org

Ready to get started?Download WordPress

Forums

Frontend form add data to database (10 posts)

  1. dyrk
    Member
    Posted 3 years ago #

    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

  2. dyrk
    Member
    Posted 3 years ago #

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

  3. dyrk
    Member
    Posted 3 years ago #

    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.

  4. leehalliday
    Member
    Posted 3 years ago #

    Hi,

    Did you find tutorial or anything for this?

  5. dyrk
    Member
    Posted 3 years ago #

    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

  6. dyrk
    Member
    Posted 3 years ago #

    Oh yes and very important....

    Make sure your form's action is blank.
    Ie. <form action="" method="post">

  7. embaeva
    Member
    Posted 3 years ago #

    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,

  8. dyrk
    Member
    Posted 3 years ago #

    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

  9. embaeva
    Member
    Posted 3 years ago #

    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,

  10. Niraj@PHP
    Member
    Posted 2 years ago #

    Hi dyrk,

    function plugin_form()
    function save_data()

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

Topic Closed

This topic has been closed to new replies.

About this Topic