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

使用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个输入文本,分别是“type:number”和一个保存按钮。 i想要插入 使用ajax将这3个输入文本数据放入数据库。 i已编写如下代码: p>

 &lt; fieldset&gt;&lt; legend&gt;响应时间&lt; / legend&gt;&lt;  ; /字段集&GT;  
&lt; form class =“form”&gt; 
&lt; div class =“control-group”&gt; 
&lt; label class =“control-label”&gt;高优先级&lt; / label&gt; 
&lt;  div class =“controls”&gt; 
&lt; input type =“number”name =“high”id =“sval”/&gt; Days 
&lt; / div&gt; 
&lt; / div&gt; 
&lt;  div class =“control-group”&gt; 
&lt; label class =“control-label”&gt;低优先级&lt; / label&gt; 
&lt; div class =“controls”&gt; 
&lt; input type =“  number“name =”high“id =”sval“/&gt; Days&lt; / div&gt; 
&lt; / div&gt; 
&lt; div class =”control-group“&gt; 
&lt; label class =”control  -label“&gt;正常&lt; / label&gt; 
&lt; div class =”controls“&gt; 
&lt; input type =”number“name =”high“id =”sval“/&gt; Days&lt; / div&gt  ; 
&lt; / div&gt; 
&lt; button id =“insert”class =“btn btn-primary”&gt;保存&lt; / button&gt; 
&lt; / form&gt; 
&lt; script type =“text / javascript”  src =“&lt;?php echo $ BASE;?&gt; scripts / da  ta / projects_service.js“&gt;&lt; / script&gt; 
  code>  pre> 
 
 

我的查询 p>

 插入 app_settings(kunnr,skey,sval)值('0001000383','hp_days','1')
插入app_settings(kunnr,skey,sval)值('0001000383','lp_days','3')
插入 app_settings(kunnr,skey,sval)值('0001000383','np_days','2')
  code>  pre> 
 
 

它们是硬编码但我不想要硬编码数据 .my的问题是只有sval值显示在视图中但是这个skey值怎么样。我想要使用ajax插入它 我已经尝试过这个我写的ajax如下: p> \ n

  $(function(){
 $(“#insert”)。click(function(){
 var id = $(“#id”)。val(); 
 var sval  = $(“#sval”)。val(); 
 $ .post(root +“data / projects_service?json”,{pid:id,sval:sval},function(data){}); 
 \  n});  
}); 
  code>  pre> 
 
 

请在此建议我.. p> div>

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'])

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();

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.
                }
            });
    });
});