【问题标题】:Calling PHP function from HTML form on same page从同一页面上的 HTML 表单调用 PHP 函数
【发布时间】:2016-05-18 16:29:36
【问题描述】:

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

我有一个表单,它应该调用页面上设置的函数。

表单的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

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

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

有人知道我需要改变什么才能触发它吗?

【问题讨论】:

  • 不,你不需要使用 ajax。
  • 另外,如果您不介意刷新页面,isset post submit 将在其上查找名称为submit 的对象,因此请将name="submit" 添加到您在表单上提交的输入中
  • 这不是完整的功能代码吗?因为它缺少基本知识function quick_post(){ ...

标签: php html wordpress forms


【解决方案1】:

此行正在搜索名称为 submit 的对象

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

但是你的表单没有这样的对象,所以在你的输入中添加一个名称,这个函数就可以工作了:

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

【讨论】:

    【解决方案2】:
    <input type="submit" value="Post" class="col-md-2" id="quick-post-submit">
    

    对比

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

    你没有给提交按钮一个name="submit"属性

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      相关资源
      最近更新 更多