0

I wanted to add an input field, where the user can input a name for example and it would be added to the relevant table in the database. However i want to create a new action for this, so far i have the basic input fields and submit button with the following code:

public function action_mynewaction() {
     $this->auto_render = false;
     if ($this->request->is_ajax()) 
     {
     $stmt = DB::query(Database::INSERT, 'INSERT INTO `mytable` (`name`) VALUES (:Name)');
     $view = View :: factory('my/form/file_path');
     echo $view->render();
     }
    }

This is my view file where the form is displayed

<table cellspacing="1" cellpadding="5" class="myclass noSort">
   <thead>
      <tr>
         <th>A</th>
         <th>B</th>
         <th>C</th>
      </tr>
    </thead>
        <tfoot>
            <tr>
              <td colspan="3">&nbsp;</td>
            </tr>
        </tfoot>
         <tbody>
            <tr>
                <form action="" method="post">
                 <td>Add Name</td>
                 <td><input type="text" name="Name"></td>
                 <td><input type="submit" value="Submit">
                </form>
             </tr>
          </tbody>
   </table>

But whenever i add in my

$stmt->param(':Name', $_POST['name']);

part it doen't show me the input/form field at all?

Doesn't display form/input field at all

       public function action_mynewaction() {
          $post = $this->request->post();

          $stmt = DB::query(Database::INSERT, 'INSERT INTO `mytable` (`name`) VALUES (:Name)');
          $stmt->param(':Name', $_POST['name']);
          $stmt->execute();

          $view = View :: factory('my/form/file_path');
          echo $view->render();
   }
5
  • 4
    For reference, you cannot put form elements directly inside tr elements. The only valid children are td and th. Commented Jan 10, 2014 at 16:25
  • @BenM That's good as html standard, but it allows you to put form isn't it? =/ Commented Jan 10, 2014 at 16:27
  • <td><input type="submit" value="Submit"> closing </td> missing Commented Jan 10, 2014 at 16:32
  • @Goikiu Sorry? I don't understand your comment? Commented Jan 10, 2014 at 16:37
  • @Goikiu well that depends on the browser. Some will outright remove the tag, others move it further up / down the DOM tree. It's unexpected behavior and you should not code around it. Commented Jan 10, 2014 at 17:09

2 Answers 2

2

forms data are case sensitive.

Into your html you have : name="Name" and you will retrieve data with $_POST['Name'] that said if the "receiver" php page isn't the same as the one is sending put an "action" to the form.

Sign up to request clarification or add additional context in comments.

2 Comments

Without an action, the <form> will just submit to the same page.
@RocketHazmat I didn't ever used that. Thanks for the info. I'll edit my post.
1

Please follow name convention so that it can bind corresponding element to browser get method.

    public function action_mynewaction() {
          $post = $this->request->post();

          $stmt = DB::query(Database::INSERT, 'INSERT INTO `mytable` (`name`) VALUES (:Name)');
          $stmt->param(':Name', $_POST['Name']); /* replace 'name' with 'Name' */
          $stmt->execute();

          $view = View :: factory('my/form/file_path');
          echo $view->render();
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.