weixin_33721344 2018-01-25 12:20 采纳率: 0%
浏览 25

数组JS到php codeigniter [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/18866571/receive-json-post-with-php" dir="ltr">Receive JSON POST with PHP</a>
                            <span class="question-originals-answer-count">
                                (7 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-01-25 15:22:27Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

I need to send an array of java script to php, this is the idea that I have made but it does not work, if you can help solve the problem or find another way to do it I would appreciate it. The idea is to reload the entire page again.
I have problems sending a JSON to a controller via AJAX, the JS alert works and the JSON shows me, but the controller is not receiving the data.
creation of 'actualizar':

var actualizar = new Object();
function myButton_onclick(x,id) {
   if(x.style.backgroundColor=='rgb(255, 114, 144)')
{
    x.style.backgroundColor='rgb(90,237,247)';
     actualizar[id]=1;

}else{
    x.style.backgroundColor='rgb(255, 114, 144)';
     actualizar[id]=0;

}
console.log(actualizar)
return false;
}

view:

$id= array('id' => 'x' );
echo form_open("cursos/guardarmalla",$id);?>
<div align="center"><?php echo form_submit('','Guardar Malla',"class=\"btn btn-success btn-lg\"");?></div>
<?php echo form_close()?>
<script type="text/javascript">
   $(document).ready(function(){
    $('#x').submit(function(){
      var aux = JSON.stringify(actualizar);
      $.ajax({
        type  :'POST',
        url: $(this).attr("action"),
        dataType: "json",
        data   : aux
    });
  alert(aux);

  })
})
</script>

controller:

function guardarmalla(){

    $datos= $this->input->post('aux');
    $deco=json_decode($datos,true);
    echo "Los datos recibidos son".$deco;

}

I regret having the variables in Spanish. I'm from Chile and in stackoverflow in Spanish I had no answer

EDIT

I think the problem is that ajax does not cause the driver function to run

EDIT EDIT

Investigating arrives at the conclusion that AJAX is not necessary, since I want to reload the whole page again, but I do not know how to send an Array or JSON they are the "submit" button to PHP

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33735077 2018-01-25 12:29
    关注

    In your ajax in js you need to correct code:

        $.ajax({
            type  :'POST',
            url: $(this).attr("action"),
            dataType: "json",
            data   : {'aux' : aux}
        });
    

    It corrected structure of json variable

    Also instead alert(aux); add return false;. It is prepent of html form submit.

    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加