在同一页面上从HTML表单调用PHP函数

时间:2016-02-08 21:06:10

标签: php html wordpress forms

所以我在网上看了一下,发现了一些关于它的问题,但似乎没有什么能适合我的情况。

我有一个表格应该调用页面上设置的功能。

表单的html如下:

<form method="post" action="">
      <textarea id="input-box" placeholder="What's on your mind?" maxlength="10000" name="quick-post-area" class="col-md-offset-2 col-md-6"></textarea>
      <input type="submit" value="Post" class="col-md-2" id="quick-post-submit">
</form>

和功能:

// Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    $author_id = 1;
    $slug = 'post';
    global $current_user;        

    // If the page doesn't already exist, then create it
    if( null == get_page_by_title( $title ) ) {

        // Set the post ID so that we know the post was created successfully
        $pollq_question = wp_kses_post( trim( $_POST['pollq_question'] ) );
        $post_id = wp_insert_post(
            array(
                'comment_status'    =>  'open',
                'ping_status'       =>  'closed',
                'post_author'       =>  $current_user->ID,
                'post_name'         =>  $slug,
                'post_title'        =>  'Posted By:' . $current_user->ID,
                'post_status'       =>  'publish',
                'post_type'         =>  'post',
                'post_content'      =>  $_POST['quick-post-area']
            )
        );

    // Otherwise, we'll stop
    } else {

            // Arbitrarily use -2 to indicate that the page with the title already exists
            $post_id = -2;

    } // end if

} // end programmatically_create_post

最后是我试图用来调用函数的假设:

if(isset($_POST['submit']))
{
   quick_post();
}

任何人对我需要改变什么都有任何想法才能开火?

2 个答案:

答案 0 :(得分:1)

此行正在搜索名称为“提交”的对象

if(isset($_POST['submit']))
{

但是你的表单没有这样的对象,所以在输入中添加一个名称,该功能应该有效:

<input type="submit" name="submit" value="Post" class="col-md-2" id="quick-post-submit">

答案 1 :(得分:1)

<input type="submit" value="Post" class="col-md-2" id="quick-post-submit">

vs

if(isset($_POST['submit']))
{
   quick_post();
}

您没有为提交按钮提供name="submit"属性