dongshan3806
dongshan3806
采纳率0%
2012-12-01 10:04

使用php中的ajax将数据插入数据库

已采纳

i do have 3 input text which are of "type:number" and a save button. i want to insert the these 3 input text data into the database using ajax. i have written the code as follows:

<fieldset><legend>Response Times</legend></fieldset> 
    <form class="form">
        <div class="control-group">
            <label class="control-label">High Priority</label>
            <div class="controls">
            <input type="number" name="high" id="sval"/>Days
            </div>
        </div>
        <div class="control-group">
            <label class="control-label ">Low Priority</label>
            <div class="controls">
            <input type="number" name="high" id="sval"/>Days</div>
        </div>
         <div class="control-group">
            <label class="control-label ">Normal</label>
            <div class="controls">
            <input type="number" name="high" id="sval"/>Days </div>
        </div>
        <button id="insert" class="btn btn-primary">Save</button>
</form>
<script type="text/javascript" src="<?php echo $BASE;?>scripts/data/projects_service.js"></script>

and my query is

Insert into app_settings(kunnr,skey,sval) Values ('0001000383','hp_days','1') 
Insert into app_settings(kunnr,skey,sval) Values ('0001000383','lp_days','3')
Insert into app_settings(kunnr,skey,sval) Values ('0001000383','np_days','2')

which are hard coded but i dont want hard coded data.my problem is that only sval values are showing in the view but what about this skey value.and i want o insert it using ajax and i have tried this by ajax that i have written as follows:

$(function () {
    $("#insert").click(function () {
        var id = $("#id").val();
        var sval = $("#sval").val();
        $.post(root + "data/projects_service?json", { pid: id, sval: sval }, function (data) { });

    });        
});

please suggest me on this..

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duandun3178 duandun3178 9年前

    In "your url" file you can access variables hp for example by $_GET['hp'].

    Can you tell me kunnr should be different parameter for different days? If they are not, then you can make the query in the following way:

    Insert into app_settings(kunnr,skey,sval) Values ('0001000383','hp_days',$_GET['hp']) 
    Insert into app_settings(kunnr,skey,sval) Values ('0001000383','lp_days', $_GET['lp'])
    Insert into app_settings(kunnr,skey,sval) Values ('0001000383','np_days',$_GET['n'])
    
    点赞 评论 复制链接分享
  • doupai6875 doupai6875 9年前

    html code

    <html>
    <body>
    
    <fieldset><legend>Response Times</legend></fieldset> 
        <form class="form">
            <div class="control-group">
                <label class="control-label">High Priority</label>
                <div class="controls">
                <input type="number" name="high" id="kunnr"/>Days
                </div>
            </div>
            <div class="control-group">
                <label class="control-label ">Low Priority</label>
                <div class="controls">
                <input type="number" name="high" id="skey"/>Days</div>
            </div>
             <div class="control-group">
                <label class="control-label ">Normal</label>
                <div class="controls">
                <input type="number" name="high" id="sval"/>Days </div>
            </div>
            <button id="insert" class="btn btn-primary">Save</button>
    </form>
    
    </body>
    </html>
    

    jquery code

    $(function()
    {   
        $("#insert").click(function() 
        {
            var kunnr = $("#kunnr").val;
            var sKey = $("#skey").val;
            var sVal = $("#sval").val;
            var dataString = "kunnr="+kunnr+"&sKey="+sKey+"&sVal="sVal;
            $.ajax(
            {
                type: "GET",
                    url: "your url", // in the page of url write the code to insert in db
                    data: dataString,
                    success: function(result) 
                    {               
                        alert(result); // you can just check whether the row is inserted or not.
                    }
                });
        });
    });
    
    点赞 评论 复制链接分享
  • dongpu8935 dongpu8935 9年前

    Look at this line:

    var id = $("#id").val();
    

    This line means that you take value from the element whose id's name is "id".

    Therefore Js can't find the value of this element as it doesn't exist.

    And why you use id="sval"? Id parameter should be always unique.Change their id to "hp","lp","n" respectively and get 3 values in jQ.)

    var hp = $("#hp").val();
    var lp = $("#lp").val();
    var n = $("#n").val();
    
    点赞 评论 复制链接分享

为你推荐