dsjz1119 2017-05-11 21:32
浏览 28
已采纳

从javascript获取数据后无法从$ _POST获取数据

I am sending data to php from javascript in this format :

var str = $('#description').summernote('code');
    var formData =  $('#form').serialize();
    var formData2=formData+'&data='+encodeURIComponent(str);

           $.ajax({
                            type: 'POST',
                            url: 'test.php',
                            data: formData2,
                            cache: false,
                            success: function (data) {

                            }
                        });

But, I'm unable to get the data in php. I'm using :

$title=$_POST['title'];

to get title but it says that the index is not found and this it is an array and not a string . HOw can I get 'title' data along with many other values?

But ,

$myfile = fopen("test.txt", "a") or die("Unable to open file!");
fwrite($myfile, var_export($_POST, true));
fclose($myfile);

gives me this :

array (
  'title' => 'test title',
  'from_datetime' => '',
 'sno' => ''22,
)

raw format from javascript console:

title=test%20title&from_datetime=&description=br%3E%3C%2Fp%3E%3Cp%3E-----

form:

 <div id="form_div" >
            <form id="form"  method="post" action="javascript:submit_data();" >


                    <div class="col-xs-12 col-sm-12">
                        <input name="title" id="title" placeholder="title" type="text" class="form-control" >

                    </div>

  <div class="col-xs-12 col-sm-12">
                        <input name="desc" id="desc" placeholder="desc" type="text" class="form-control" >

                    </div>

                </div>
  • 写回答

2条回答 默认 最新

  • douou6807 2017-05-11 21:43
    关注

    It seems you do not send the appropriate data through ajax. Try to serialize all the data from your form

    var dataSerialized = $('#form').serialize();
    
    $.ajax({
         type: 'POST',
         url: 'test.php',
         data: dataSerialized,
         cache: false,
         success: function (data) {
             console.log('success');
         }
    });
    

    Then do a var_dump($_POST); in your test.php file to see if you receive the title field. More about serialize()

    The $_POST by definition passes an associative array of variables, so the following formatting is normal.

    array (
      'title' => 'test title',
      'from_datetime' => '',
      'sno' => ''22
    )
    

    What is not normal is that you do not have the same fields in the array as compared to the html code you show.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题