Welcome Guest, Not a member yet? Register   Sign In
Can't read form values
#11

If the posted data from your AJAX call has empty values, then ofcourse var_dump($this->input->post(NULL, TRUE)) will be an empty array

Your problem lies in your JavaScript code. For some reason the serialize method is returning an empty form.
Find out what could cause that. The problem is not a CI related issue.

Try this out, and see what is alerted
Code:
<!DOCTYPE html>
<html>
   <head>
   
       <script
        src="https://code.jquery.com/jquery-1.12.4.min.js"
        integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
        crossorigin="anonymous"></script>
   </head>
   <body>

       <p>A simple form to test $(form).serialize()</p>

       <form method="post" action="/">
           <div>
               <label>Field 1</label>
               <input type="text" name="field1" value="123" />
           </div>
           <br>
           <div>
               <label>Field 2</label>
               <input type="text" name="field2" value="456" />
           </div>
           <br>
           <div>
               <label>Field 3</label>
               <input type="text" name="field3" value="789" />
           </div>
           <br>
           <button onclick="save(this.form); return false">Serialize it!</button>
       </form>

       <script>
           function save(form) {
               alert($(form).serialize());
           }
       </script>

   </body>
</html>
Reply
#12

(This post was last modified: 07-18-2017, 06:30 AM by Junie.)

(07-17-2017, 08:26 PM)Paradinight Wrote: console.log($('#agencyForm').serialize());
before $.ajax.

The browser has a debug console. Press the F12 on the keyboard and change to console tab and execute the button click.

This is the result from the console: Also I tried this on onclick event( onclick="save(this.form)" ) still the same result.
Code:
agency_id=&agency_name=&category=&address=&acronym=

I also attach the screenshot.

Attached Files Thumbnail(s)
   
Reply
#13

(07-18-2017, 02:01 AM)Martin7483 Wrote: If the posted data from your AJAX call has empty values, then ofcourse var_dump($this->input->post(NULL, TRUE)) will be an empty array

Your problem lies in your JavaScript code. For some reason the serialize method is returning an empty form.
Find out what could cause that. The problem is not a CI related issue.

Try this out, and see what is alerted
Code:
<!DOCTYPE html>
<html>
   <head>
   
       <script
        src="https://code.jquery.com/jquery-1.12.4.min.js"
        integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
        crossorigin="anonymous"></script>
   </head>
   <body>

       <p>A simple form to test $(form).serialize()</p>

       <form method="post" action="/">
           <div>
               <label>Field 1</label>
               <input type="text" name="field1" value="123" />
           </div>
           <br>
           <div>
               <label>Field 2</label>
               <input type="text" name="field2" value="456" />
           </div>
           <br>
           <div>
               <label>Field 3</label>
               <input type="text" name="field3" value="789" />
           </div>
           <br>
           <button onclick="save(this.form); return false">Serialize it!</button>
       </form>

       <script>
           function save(form) {
               alert($(form).serialize());
           }
       </script>

   </body>
</html>
Sir,

Still the same result as the follow the console log. The result is:
Code:
agency_id=&agency_name=&category=&address=&acronym=
Reply
#14

@Paradinight
@Martin7483

I have a working code with this problem by placing the action in the form like this:

<form method="POST" action="<?php echo base_url();?>agency/save_c">

But I wanted to use the ajax function and also I wanted to implement the whole CRUD operation. That's why I used my old working CRUD using MODAL. In this case, I don't want to use the MODAL I only want to use the one-page crud.
Reply
#15

(This post was last modified: 07-18-2017, 06:44 AM by Junie.)

@Paradinight
@Martin7483

I have a working code for this problem by placing action to the form:

<form method="POST" action="<?php echo base_url();?>agency/save">

I have a fully functional CRUD operation using modal but, I wanted not to use modal instead I want to create a one page CRUD operation which only consumes at least 5 input text. That's why I'm changing a little bit code in my old working CRUD.
Reply
#16

You are saying that when you post the form without the use of AJAX it works. What does that tell you?
Reply
#17

(07-18-2017, 06:50 AM)Martin7483 Wrote: You are saying that when you post the form without the use of AJAX it works. What does that tell you?

Confused... When I used ajax it contains the same URL but it throws an array of ZERO.
Reply
#18

I have taken your form and have tested it.

Code:
<form action="#" id="agencyForm" class="form-horizontal">                        
   <input type="hidden" name="agency_id" value="25" id="agency_id ">                            
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_name" class="form-control"  placeholder="Agency name">
           <span class="help-block"></span>
       </div>
   </div>
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_category" class="form-control"  placeholder="Category">
           <span class="help-block"></span>
       </div>
   </div>
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_address" class="form-control" placeholder="Address">
           <span class="help-block"></span>
       </div>
   </div>
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_acronym" class="form-control"  placeholder="Acronym">
           <span class="help-block"></span>
       </div>
   </div>
           
   <!-- <input type="submit " name="regisSubmit " class="btn-primary " value="Submit " /> -->
   <button type="submit" value="submit" onclick="save(this.form); return false" class="btn btn-primary">Save</button>
</form>

It worked. The field values are serialized and available in the $_POST array.

Maybe a stupid question but what values are you expecting?
Reply
#19

(This post was last modified: 07-18-2017, 07:28 AM by Junie.)

(07-18-2017, 07:14 AM)Martin7483 Wrote: I have taken your form and have tested it.

Code:
<form action="#" id="agencyForm" class="form-horizontal">                        
   <input type="hidden" name="agency_id" value="25" id="agency_id ">                            
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_name" class="form-control"  placeholder="Agency name">
           <span class="help-block"></span>
       </div>
   </div>
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_category" class="form-control"  placeholder="Category">
           <span class="help-block"></span>
       </div>
   </div>
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_address" class="form-control" placeholder="Address">
           <span class="help-block"></span>
       </div>
   </div>
   <div class="form-group">
       <div class="controls">
           <input type="text" name="input_acronym" class="form-control"  placeholder="Acronym">
           <span class="help-block"></span>
       </div>
   </div>
           
   <!-- <input type="submit " name="regisSubmit " class="btn-primary " value="Submit " /> -->
   <button type="submit" value="submit" onclick="save(this.form); return false" class="btn btn-primary">Save</button>
</form>

It worked. The field values are serialized and available in the $_POST array.

Maybe a stupid question but what values are you expecting?
Sir,

It's working when I try to console.log ... When I tried to input random text. I'm trying to figure it out why it doesn't insert in the database. I didn't change my controller & model.
Code:
agency_id=&agency_name=q&category=q&address=q&acronym=q
Reply
#20

Because your agency_id is empty
Reply




Theme © iAndrew 2016 - Forum software by © MyBB