duangan6797 2017-04-20 07:46
浏览 9

解析$ _Post数据

I've been trying to parse the $_POST data to retrieve a range of prices and id's encoded within. Can someone point me in the right direction, please? Here is what I have:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
      $(function () {

        $('form').on('submit', function (e) {

          //e.preventDefault();

          var form = $('form')[0];
          var formData = new FormData(form);

          $.ajax({
            url: '/price-update.php',
            data: formData,
            type: 'POST',
            contentType: false,
            processData: false,
            success: function () {
              alert('form was submitted');
            }
          });

        });

      });
</script>

Example of the $_POST data sent:

?submit=Submit+Changes&price%5B%5D=11&id%5B%5D=6&price%5B%5D=22&id%5B%5D=5&price%5B%5D=33&id%5B%5D=3&price%5B%5D=44&id%5B%5D=2&price%5B%5D=55&id%5B%5D=8

<?php

//Part of my price-update.php file

    if($_POST) {

        $Values = array();
        parse_str($_POST, $Values);

        for($i = 0; $i < count($Values['price']); $i++) {
            $price_update = "UPDATE prices SET price=".$Values['price'][$i]." WHERE id=".$Values['id'][$i];
            $send_update = $instance->query($price_update);
        }

    }

?>

I get the success message popup saying that form was submitted. But no values are changed within the database.

  • 写回答

1条回答 默认 最新

  • dpjuppr1361 2017-04-20 08:16
    关注

    I've changed your code a bit and added part to php file which returns some values to the frontend so the example is complete.

    html file:

    <form id="form">
        <label for="bar">A bar</label>
        <input id="bar" name="bar" type="text" value="" />
    
        <input type="submit" value="Send" />
    </form>
    

    javascript file:

    $(function () {
    
        $('#form').on('submit', function (e) {   // get element with id="form"
    
            e.preventDefault();  // prevent default form submission
    
            var form = $('form')[0];
            var formData = new FormData(form);
    
            $.ajax({
                url: '/price-update.php',
                data: formData,
                type: 'POST',
                contentType: false,
                processData: false,
                done: function (res) {
                    console.log('res', res);
                    alert('form was submitted');
                }
            });
    
        });
    
    });
    

    php file:

    <?php
    
    if($_POST['bar']) {
    
        $values = array();
    
        foreach ($_POST as $key => $value) {
           $values[$key] = $value;  // do something with values (for example put them to array)
        }
    
        echo json_encode($values);  // return those values to the frontend
    }
    

    You can look at the more detailed description here.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教