SVG数据未通过表单提交

时间:2019-02-18 08:56:57

标签: php jquery codeigniter svg

我想将SVG数据保存在数据库中。我将尝试使用简单的提交以及AJAX。我还设置了content-type="image/svg-xml"

当我尝试将db svg值存储为null时,我将使用此处显示的print_r($_POST); SVG内容检查数据。

<?php echo form_open('admin/shape/add',array('class' => 'form-horizontal', 'name' => 'catfrm', 'id' => 'catfrm')); ?>
  <div class="box-body">
    <div class="col-md-8">
      <div class="form-group">
        <label class="col-sm-2 control-label">Title&nbsp;<span class="error">*</span></label>
        <div class="col-sm-10">
          <input type="text" name="title" value="" id="title" class="form-control" />
        </div>
      </div>
      <!-- form-group -->
    </div>
    <div class="col-md-8">
      <div class="form-group">
        <label class="col-sm-2 control-label">Content&nbsp;<span class="error">*</span></label>
        <div class="col-sm-4" id="shape_priview" height="30px">
          <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0,0,100,100"><polygon points="50 0, 0 100, 100 100"></polygon></svg>
        </div>
        <div class="col-sm-6">
          <textarea name="content" id="shape_content" class="form-control"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0,0,100,100"><polygon points="50 0, 0 100, 100 100"></polygon></svg></textarea>
        </div>
      </div>
      <!-- form-group -->
    </div>
  </div>
  <div class="box-footer" align="center">
    <button type="submit" class="btn btn-success">Add</button>
    <button type="reset" class="btn btn-dark" onclick="window.location.href = '<?php echo site_url('admin/shape'); ?>'">Cancel</button>
  </div>
<?php echo form_close();?>
$(document).ready(function() {
  $("#shape_content").on("change keyup paste", function() {
    $("#shape_priview").text('');
    var shape_content = $("#shape_content").val();
    $("#shape_priview").html(shape_content);
  });
  $("#catfrm").validate({
    rules: {
      title: {
        required: true,
      }
    },
    messages: {
      title: {
        required: "Title is required",
      }
    },
    submitHandler: function(form) {
      $("#catfrm").submit(function(event) {
        event.preventDefault();
        var content = $("#shape_content").val();
        var title = $("#title").val();
        alert(content);
        $.ajax({
          type: "post",
          url: "<?php echo site_url('admin/shape/add'); ?>",
          data: {
            "title": title,
            "content": content
          },
          contentType: "application/json",
          success: function(responseData, textStatus, jqXHR) {
            alert(responseData);
            alert("data saved")
          },
          error: function(jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
          }
        })
      });
    }
  });
});

0 个答案:

没有答案