doushan2311 2014-12-14 18:04
浏览 55
已采纳

使用ajax将加载的数据插入到页面源中以将它们插入到mysql中

I have 3 divs that when the user clicks on each of them, a particular content of a form will load. till this part there is no problem, but after that, I can't insert them into mysql.

html:

  <div id="adds">
  <div onclick="show_content('value1',this)" class="add selected">first div</div>
  <div onclick="show_content('value2',this)" class="add">second div</div>
  <div onclick="show_content('value3',this)" class="add">third div</div>
  </div>
  <div class="clear"></div>
  <div id="add_form"></div>
        <?php
          if(!empty($error)) {
        ?>
        <script type="text/javascript">
           $(".error").html(" <?php echo $error;?> ");
           $(".error").slideDown(function(){
              $(this).animate({top: '50%'});
           });
           $(".error_back").fadeIn();
           $("body").css("overflow" , "hidden");
           setTimeout(function(){
           $(".error").animate({top: '-74px'}, function(){
              $(this).slideUp();
           });
           $(".error_back").fadeOut();
           $("body").css("overflow" , "auto");
           },2000);
        </script>
        <?php
          }
        ?>

and jquery:

    <script type="text/javascript">
    function show_content(type_name,selected){
    datastring = "type_name=" + type_name;
    $.ajax({
          type: "POST",
          url: "motor.php",
          data: datastring,
          cache: false,
          success: function(html) {
              $("#add_form").hide().html(html).fadeIn();
          }
    });
          return false;
    }
    </script>

and php:

if(!empty($_POST['type_name'])) {
    if($_POST['type_name']=='value_1') {
        include('add_dore.php');
        if(isset($_POST['add_value_1_submit'])) {
            $value_1 = $_POST['value_1'];
            if(empty($value_1)) {
                $error = "inter value 1";
                }else{
                mysql_query("INSERT INTO `value_1`(
                     'id' ,
                     'value_1' ,
                )VALUES(
                     NULL ,
                     '$value_1'
                )");
            }
        }
    }elseif($_POST['type_name']=='value_2') {
        include('value_2.php');
    }elseif($_POST['type_name']=='value_3') {
        include('value_3.php');
    }
}

*for now, I just wrote the data for value_1:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <label for="value_1">value title:</label>
        <input type="text" name="value_1" <?php if(!empty($value_1)) echo "value=\"$value_1\""; ?> />
</form>
  • 写回答

3条回答 默认 最新

  • dongyu9667 2014-12-18 11:26
    关注

    I found the problem.

    I tell the ajax to go to the motor.php . and you know after that, we should do the insertions and etc in our first page. I wrote the insertion in motor page and it was my problem.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看