dwinnbrown February 2016

Calling PHP function from HTML form on same page

So I have looked around online and found some questions on here about it, but nothing seemed to work for my situation.

I have a form which should call a function that is set on the page.

The form's html is as follows:

<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>

and the function:

// 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

and finally the isset that I am trying to use to call the function:

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

Anyone got any ideas about

Answers


Phiter Fernandes February 2016

This line is searching for an object with the name submit on it

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

But your form doesn't have such object, so add a name to your input and the function shall work:

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


Brad Kent February 2016

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

vs

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

you didn't give the submit button a name="submit" attribute

Post Status

Asked in February 2016
Viewed 3,609 times
Voted 7
Answered 2 times

Search




Leave an answer