Vishal Kumar
Vishal Kumar

Reputation: 4617

HTML form not sending post through submit

I have checked all the solutions on Stack overflow...and I am meeting all I need. Still my HTML form is not sending post request.here is my code.

This is create_blog.php page ..this includes

<?php include_once ('blog_controller.php'); ?>

at top.

<form action="create_blog.php" method="post">
   <div class="form-row form-row">
      <div class="form-controls">
         <input type="text" name="title" class="field form-control input-lg" required id="field-fname" value="" placeholder="Title*" />
      </div>
      <!-- /.form-controls --> 
   </div>
   <!-- /.form-row -->

   <div class="form-row">
      <div class="form-controls">
         <textarea class="textarea" name="body"  required id="field-message" placeholder="Content*"></textarea>
      </div>
      <!-- /.form-controls --> 
   </div>
   <!-- /.form-row -->

   <?php
   if (isset($error)) {
       echo '<div class="alert alert-error">' . $error . ' </div>';
   }
   if (isset($success)) {
       echo '<div class="alert alert-success">' . $success . ' </div>';
   }
   ?>
   <div class="form-actions">
      <input type="submit" value="CREATE NOW" name="add_blog" class="button btn-purple btn-small" />
   </div>
   <!-- /.form-actions -->
</form>

Here is blog_controller.php

<?php
include_once ('ManageBlogs.php');
$init = new ManageBlogs();


//CREATE NEW BLOG

if (isset($_POST['add_blog'])) {
    $title = $_POST['title'];
    $body = $_POST['body'];
    $date_created = date('Y-m-d H:i:s');


    if (empty($title)) {
        $error = 'You must provide a title to add this blog.';
    } else {
        $create_code = $init->addNewCode($title, $body, $date_created);

        if ($create_code == 1) {
            $success = 'Blog '.$title.' added Successfully';
        } else {
            $error = 'Error adding Blog. Please Try Again.';
        }
    }
}
?>

Still it's sending no POST request on submit button click.

Upvotes: 0

Views: 135

Answers (4)

Kamaldeep singh Bhatia
Kamaldeep singh Bhatia

Reputation: 732

Try this : Remove <?php include_once ('blog_controller.php'); ?> from top and change action from create_blog.php to blog_controller.php

As I got from your question you want data from current form to blog_controller.php where you have code to do something.

Upvotes: 0

Nikhil
Nikhil

Reputation: 1450

I've gone through your code and it is working without any issues. Since I'm not including ManageBlogs.php which means it may cause this error. Try to remove the file inclusion and check again. If you are getting the output which means, some unwanted internal redirects are removing the POST values.

Here is the code which I tried and it is working.

<?php 

include_once ('blog_controller.php'); 

if (isset($title)) {
   echo 'Title: '.$title;
}
?>
<form action="create_blog.php" method="post">
   <div class="form-row form-row">
      <div class="form-controls">
         <input type="text" name="title" class="field form-control input-lg" required id="field-fname" value="" placeholder="Title*" />
      </div>
      <!-- /.form-controls --> 
   </div>
   <!-- /.form-row -->

   <div class="form-row">
      <div class="form-controls">
         <textarea class="textarea" name="body"  required id="field-message" placeholder="Content*"></textarea>
      </div>
      <!-- /.form-controls --> 
   </div>
   <!-- /.form-row -->
    <?php
    if (isset($error)) {
       echo '<div class="alert alert-error">' . $error . ' </div>';
    }
    if (isset($success)) {
       echo '<div class="alert alert-success">' . $success . ' </div>';
    }
   ?>
   <div class="form-actions">
      <input type="submit" value="CREATE NOW" name="add_blog" class="button btn-purple btn-small" />
   </div>
   <!-- /.form-actions -->
</form>

blog_controller.php

<?php
// include_once ('ManageBlogs.php');
// $init = new ManageBlogs();

if (isset($_POST['title'])) {
    $title =  $_POST['title'];  
}
?>

Upvotes: 0

Amar Shukla
Amar Shukla

Reputation: 136

Change :

<form action = 'create_blog.php' ...>

To :

<form action = 'blog_controller.php' ...>

You have handled the form in blog_controller.php but the action of the form is create_blog.php

Also check if on javscript side you have this code :

event.preventDefault();

This javascript coding also prevents the form to transfer the control to PHP coding once the button is fired .

Upvotes: 1

Licky
Licky

Reputation: 53

What I understood is, you want to send data from login.php to login_controller.php, right? So the action in the form is going to be 'login_controller.php', not 'create_blog.php'.

Upvotes: 0

Related Questions