0

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..

4

3 に答える 3

2

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();
于 2012-12-01T10:18:13.537 に答える
2

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.
                }
            });
    });
});
于 2012-12-01T10:39:59.790 に答える
0

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'])
于 2012-12-05T22:05:22.837 に答える