POST数据未设置?Codeigniter解决方案



我想我在某个地方有一个简单的bug,但我看不到!在我看来,我有以下javascript来创建表单:

  $.ajax({
    url:"<?php echo site_url('mycontroller/methodX/'.$ip.'/'.$hardwaremodel);?>",
    type:'POST',
    dataType:'json',
    success: function(returnDataFromController) {
    var htmlstring;
    var submitFormHTML;
    htmlstring = "<br><br><B>To reassign the port to a new vlan, click on a VlanId below and then click on the OK button</B><br><table class='table table-bordered table-striped'>";
    htmlstring = htmlstring + "<th>VlanId</th><th>Name</th>";
    for(i = 0; i < returnDataFromController.length; i++) {
    }
    submitFormHTML = "<form method='post' accept-charset='utf-8' action='/myapp/index.php/controllerABC/methodABC/"+ $('#ip').val() +"/" + $('#hardwaremodel').val() +"/" + $('#port').val() + "'><input type='text' id='newVlanID' style='width:5em;height:1.5em'/>&nbsp;&nbsp;<button type='submit' class='btn' id='saveVlan' style='width:10em;height:2em'>Reassign Vlan</button></form>";
    //alert(submitFormHTML);
    $('#clientajaxcontainer').html(htmlstring);
    $('#newvlanform').html(submitFormHTML);

它是构建表单的"submitFormHTML"字符串。

在我的控制器中,我有以下逻辑来检查输入:公共函数方法ABC(){

     if($_POST){
         echo 'I am here';
         $form = $this->input->post();
         var_dump($form);
         exit();
     }
     else {
         echo "false";
     }

它总是打印"false"。我也尝试过使用:

print_r($this->input->post());

    echo $this->input->post('newID');

但我似乎无法将视图中的数据输入控制器。你能看出我哪里错了吗?谢谢你的帮助。

编辑:

页面呈现时,会为表单创建以下HTML:

<form method="post" action="/myapp/index.php/switches/changeportvlan/11.11.11.11 /">
<input type='text' id='newVlanID' style='width:5em;height:1.5em'/>&nbsp;&nbsp;
<button type="submit" class='btn' id='saveVlan' style='width:10em;height:2em'>Reassign Vlan</button>
</form>"

问题是文本框缺少"name"属性。"id"是不够的!

您需要

if ($this->input->post(Null, False)) {
   echo "I am here";
   $form = $this->input->post(Null, True); ## True for XSS-cleaning, which you probably want.
   exit();
}
else {
   echo "False";
}

您必须给出$this->input->post()参数。此外,切勿在CodeIgniter中使用$_POST

祝好运

相关内容

最新更新