wpdb - Saving custom form data into database

I am a newbie to WordPress.I've created a custom form in WordPress and I need to know where to put the WPDB PHP c

I am a newbie to WordPress. I've created a custom form in WordPress and I need to know where to put the WPDB PHP code to save my form data.

I tried putting it directly in my page but that didn't work.

All of the research I've looked up says not to enter it in the function file so where do I put it?

Here is the code I put in my page:

Form Code

<form id="myForm" name="myform">
    <select id="brandSel" size="1">
        <option selected="selected" value="">-- Select Brand --</option>
        <option>Abba</option>
        <option>AG Hair</option>
    </select>

    <input type="submit" value="submit" />

</form>

PHP Code

<?php
    global $wpdb; 
    $inputValue = $_POST['newValue']; 
    $wpdb->insert( 
        'catalog', 
        array( 
            'brandSel' => $inputValue 
        ), 
        array( '%s' // if the field type is string ) 
    );
?>

I am a newbie to WordPress. I've created a custom form in WordPress and I need to know where to put the WPDB PHP code to save my form data.

I tried putting it directly in my page but that didn't work.

All of the research I've looked up says not to enter it in the function file so where do I put it?

Here is the code I put in my page:

Form Code

<form id="myForm" name="myform">
    <select id="brandSel" size="1">
        <option selected="selected" value="">-- Select Brand --</option>
        <option>Abba</option>
        <option>AG Hair</option>
    </select>

    <input type="submit" value="submit" />

</form>

PHP Code

<?php
    global $wpdb; 
    $inputValue = $_POST['newValue']; 
    $wpdb->insert( 
        'catalog', 
        array( 
            'brandSel' => $inputValue 
        ), 
        array( '%s' // if the field type is string ) 
    );
?>
Share Improve this question edited Jul 31, 2018 at 21:14 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jul 31, 2018 at 20:18 Abel SanzAbel Sanz 311 gold badge1 silver badge3 bronze badges 6
  • Could you update your question and add your current code? – Krzysiek Dróżdż Commented Jul 31, 2018 at 20:31
  • <form id="myForm" name="myform"><select id="brandSel" size="1"> <option selected="selected" value="">-- Select Brand --</option> <option>Abba</option> <option>AG Hair</option> <option>Agave</option> </select> <input type="submit" value="submit" /> </form> – Abel Sanz Commented Jul 31, 2018 at 20:42
  • OK, and where do you want to store that data? – Krzysiek Dróżdż Commented Jul 31, 2018 at 20:44
  • In a costum table I created in my wordpress database – Abel Sanz Commented Jul 31, 2018 at 20:45
  • Could you also add to your question the code you use for saving it in that custom table? – Krzysiek Dróżdż Commented Jul 31, 2018 at 20:46
 |  Show 1 more comment

1 Answer 1

Reset to default 3

OK, so here is how you should to this proper way...

In your template file you put your form:

<form id="myForm" name="myform" action="<?php echo esc_attr( admin_url('admin-post.php') ); ?>" method="POST">
    <input type="hidden" name="action" value="save_my_custom_form" />
    <select id="brandSel" size="1">
        <option selected="selected" value="">-- Select Brand --</option>
        <option>Abba</option>
        <option>AG Hair</option>
    </select>

    <input type="submit" value="submit" />
</form>

And in functions.php file (or in your plugin) you'll have to add admin_post_{action}:

function my_save_custom_form() {
    global $wpdb;

    $inputValue = $_POST['newValue'];
    $wpdb->insert(
        'catalog',
        array( 'brandSel' => $inputValue ),
        array( '%s' ),
    );

    wp_redirect( site_url('/') ); // <-- here goes address of site that user should be redirected after submitting that form
    die;
}

add_action( 'admin_post_nopriv_save_my_custom_form', 'my_save_custom_form' );
add_action( 'admin_post_save_my_custom_form', 'my_save_custom_form' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744883709a4598993.html

相关推荐

  • wpdb - Saving custom form data into database

    I am a newbie to WordPress.I've created a custom form in WordPress and I need to know where to put the WPDB PHP c

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信