CodeIgniter Forums

Full Version: the input->post() is not loaded when form submitted with javascript
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Gyorgy Fekete[/eluser]
I have a problem. I'm submitting a form with javascript. I set the form's action and submit the form.

The problem is on the back-end side. The controller is loaded, but the $this->input->post is not loaded with the form elements.

I did a var_dump on $_POST and it was there.

I think it is a bug, but I'm not sure. Here's my html and javascript (jQuery)

HTML

Code:
<form action="#" method="post" enctype="multipart/form-data" name="frmSample" id="frmSample">

<input type="text" id="sample" name="sample" value="" />
<a href="#" id="form_link">Test submit link</a>

&lt;/form&gt;


[removed]

Code:
var url = "http://test.dev/form-test"

$('#form_link').click(function(){
    $('#frmSample').attr('action',url).submit();

});

I check for $this->input->post('sample') which is not loaded.
I check for $_POST['sample'] which works fine.

El Forum

[eluser]Derek Allard[/eluser]
Ah, I see this now. I saw your earlier bug report, but didn't see this post (generally bug report posts are in the bug report forum, so I'm moving this there now. The forum security has removed the actual javascript. Could you repost and change "javscript" to "javaript" (or some other intentional misspelling)?

I built a sample test, and I simply cannot recreate.
Code:
$this->load->helper('form');
        echo form_open('form_test');
        echo form_input('inputer');
        echo '&lt;input type="button" onclick="document.forms[0].submit();" /&gt;';
        echo form_close();
        echo "<hr>";
        echo $this->input->post('inputer');

El Forum

[eluser]Gyorgy Fekete[/eluser]
Here it is the [removed]
Quote:var url = "http://test.dev/form-test"

$('#form_link').click(function(){
$('#frmSample').attr('action',url).submit();

});

El Forum

[eluser]Derek Allard[/eluser]
OK, I see you're using jQuery here. Can you reduce this down to the most simple possible example with no external libraries. I'd like to be able to cut/paste and test. We need to rule out a javascript error here, and without all kinds of extra work I'm not able to easily do so.

El Forum

[eluser]Gyorgy Fekete[/eluser]
The problem is not with javascript here. But I think rather with the back-end controller. Simply because the $_POST contains the form elements and $this->input->post doesn't. (it's not even loaded)

El Forum

[eluser]Derek Allard[/eluser]
Since you've tested this lots, that easy for you to tell at this point - but I haven't been able to see it in action. Each of the tests I've run (I provided some of my code so you could recreate) seem to work. I'm not doubting that you've found something here, and I absolutely want to get it fixed up for you. In order for me to fix it, I need to be able to recreate it.

El Forum

[eluser]Gyorgy Fekete[/eluser]
HTML:

Code:
&lt;form action="#" method="post" enctype="multipart/form-data" id="frmSample" name="frmSample"&gt;
&lt;input type="text" name="txtSample" value="sample text value" /&gt;
&lt;a href="#" id="sample_link" onclick="submitForm();return false;"&gt;Submit link</a>
&lt;/form&gt;

//java cript

function submitForm()
{
    var form = document.getElementById('frmSample');
        form.action = 'http://sample.dev/test';
        form.submit();
}

Back-end PHP:

Code:
class Test extends Controller
{
function __construct()
{
    parent::Controller();
}

function index(){}

function test()
{
    var_dump($this->input->post());
    var_dump($_POST);
}

El Forum

[eluser]xwero[/eluser]
It looks odd to me using a link as a submit button and then triggering it with submit? There is no possibility to backup the functionality with server side code.

var_dump($this->input->post()); should return a false because no index is added.

if var_dump($this->input->post('txtSample')); doesn't return a a value something's wrong.

El Forum

[eluser]Derek Jones[/eluser]
xwero is correct regarding the use of $this->input->post(). It's worth mentioning just from a debugging POV that CodeIgniter, and any PHP application, doesn't care how information is submitted to the server. If the $_POST array has values, PHP, and CodeIgniter will see it.