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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog