doufen3134 2014-01-31 14:01
浏览 40

使用ajax,jquery和php将引导输入数据添加到mysql

Here I have a bootstrap modal window:

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Add new row</h4>
      </div>
      <div class="modal-body">
      ......
<div class="input-group">
  <span class="input-group-addon">Ime</span>
  <input type="text" id="Ime" class="form-control" placeholder="Upisi ime">
</div>
</br>
<div class="input-group">
  <span class="input-group-addon">Pol</span>
  <input type="text" id="pol" class="form-control" placeholder="Pol (male/female)">
</div>
</br>

<div class="input-group">
  <span class="input-group-addon">Godine</span>
  <input type="text" id="godine" class="form-control" placeholder="Godine starosti">
</div>
</br>

<div class="input-group">
  <span class="input-group-addon">Broj pojedenih krofni</span>
  <input type="text" id="krofne" class="form-control" placeholder="Pojedene krofne">
</div>
</br>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" id="newData" class="btn btn-primary">Add new data</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

I want to ADD this data from input fields to mysql database with column: name,gender,age,donuts eaten

So I write ajax jquery code:

<script>
  //add data to database using jquery ajax
$("#newData").click(function() {
    //in here we can do the ajax after validating the field isn't empty.
    if($("#message-content").val()!="") {
        $.ajax({
            url: "messages.php",
            type: "POST",
            async: true, 
            data: { Name:$("#ime").val(), Gender:$("#pol").val(), Age:$("#godine").val(), Donuts_eaten:$("#krofne").val()}, //your form data to post goes here as a json object
            dataType: "html",

            success: function(data) {
                $('#output').html(data);    
            },  
        });
    } else {
        //notify the user they need to enter data
    }
});
</script>

now I was try to write a php file but i'm beginer to this show I only know how to connect so I write:

<?php

$con = mysql_connect('localhost', 'gmaestro_agro', 'password') or die('Error connecting to server');

mysql_select_db('gmaestro_agro', $con); 

//WHAT I NEED TO WRITE HERE TO ADD DATA TO MYSQL

?>

I have a demo of all this: http://agrotime.roadvoyage.com/index1.html, so click on add new button to get modal window...

Please help me with that

sorry for my english its bad but i'm learning hard

  • 写回答

1条回答 默认 最新

  • duanqiao3608 2014-01-31 14:18
    关注

    As simple as you have to write a query:

    $con = mysql_connect(...);
    if($con != false) {
        mysql_select_db('gmaestro_agro', $con);
        $query = "INSERT INTO `table` (`name`, `gender`, `age`, `donuts_eaten`) VALUES (";
        $query .= mysql_real_escape_string($_POST['Name']) . ", ";
        $query .= mysql_real_escape_string($_POST['Gender']) . ", ";
        $query .= mysql_real_escape_string($_POST['Age']) . ", ";
        $query .= mysql_real_escape_string($_POST['Donuts_eaten']);
        $query .= ")";
        $result = mysql_query($query);
        if($result != false) {
            echo "success!";
        } else {
            echo "an error occured saving your data!";
        }
    }
    

    I assume by the way that you know the name of your 'table' and your 'columns' in your mysql db and can fix them on your own. All the data in your AJAX Request appear in PHP's POST array.

    评论

报告相同问题?

悬赏问题

  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看