I am making a web app (android) with phonegap and jquery mobile.

我正在制作一个带有phonegap和jquery mobile的网络应用程序(android)。

I am trying to send three fields from an html form as json, to a php page which will decode the json string/object (im new to json, ajax, jquery) and add the three fields as a mysql query to a database on my localhost.

我试图将一个html表单中的三个字段作为json发送到一个php页面,该页面将解码json字符串/对象(即时为json,ajax,jquery)并将三个字段作为mysql查询添加到我的数据库中本地主机。

My html page looks like this:

我的html页面如下所示:

<script type="text/javascript">
$(document).ready(function(){  
$('#btn').bind('click', addvalues);
});
  function addvalues(){
  $.ajax({  
  url: "connection.php",
  type: "POST",  
  data: "id=" + $("#id").val()+"&name=" + $("#name").val() + "&Address=" + $("#Address").val(),
   datatype:"json",
   success: function(status)
  {  
    if(status.success == false) 
    {  
        //alert a failure message
    } 
    else { 
          //alert a success message
        }  
   }  
 });  
} 
</script> 
</head> 
<body> 

<div data-role="header">
<h1>My header text</h1>
</div><!-- /header -->
<div data-role="content">
<form id="target" method="post">
<label for="id">
<input type="text" id="id" placeholder="ID">
</label>
<label for="name">
<input type="text" id="name" placeholder="Name">
</label>
<label for="Address">
<input type="text" id="Address" placeholder="Address">
</label>
<input type="submit" id "btn" value="Add record" data-icon="star" data-theme="e">
</form>
</div>
</body> 

The Question is:

How exactly do i extract the three fields (ID, name, Address) from the string that i have sent to my php file (connection.php)? connection.php is hosted by my local server.

我究竟如何从我发送到我的php文件(connection.php)的字符串中提取三个字段(ID,名称,地址)? connection.php由我的本地服务器托管。

I am familiar with making connections to database, as also with adding queries to mysql. I only need help with extracting the three fields, as i am new to ajax and jquery and json.

我熟悉与数据库的连接,以及向mysql添加查询。我只需要提取三个字段的帮助,因为我是ajax和jquery和json的新手。

As of now, this is ALL i have done in connection.php:

截至目前,这是我在connection.php中做的所有事情:

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "jqueryex";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);

//I do not know how to use the json_decode function here


//And this is how, i suppose, we will add the values to my table 'sample'
$sql = "INSERT INTO sample (id, name, Address) ";
$sql .= "VALUES ($id, '$name', '$Address')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
} else {
echo "Comment added";
}
mysql_close($con);
?>

Please add the relevant code in this file and help me out. I will be highly obliged. :)

请在此文件中添加相关代码并帮助我。我将非常感激。 :)

4 个解决方案

#1


3

what you want to do this this

你想做什么呢

           $(document).ready(function () {
        $('#btn').on('click', function () {
            $.ajax({
                url: "connection.php",
                type: "POST",
                data: {
                    id: $('#id').val(),
                    name: $('#name').val(),
                    Address: $('#Address').val()
                },
                datatype: "json",
                success: function (status) {
                    if (status.success == false) {
                        //alert a failure message
                    } else {
                        //alert a success message
                    }
                }
            });
        });
    });

then in your php do this

然后在你的PHP做到这一点

   //set variables from json data
    $data = json_decode($_POST); // Or extract(json_decode($_POST) then use $id without having to set it below.
    $id = $data['id'];
    $name = $data['name'];
    $Address = $data['Address'];

    //And this is how, i suppose, we will add the values to my table 'sample'
    $sql = "INSERT INTO sample (id, name, Address) ";
    $sql .= "VALUES ($id, '$name', '$Address')";
    if (!mysql_query($sql, $con)) {
        die('Error: ' . mysql_error());
    } else {
        echo "Comment added";
    }
    mysql_close($con);

be sure you sanitize those inputs before you insert them though.

确保在插入之前清理这些输入。

更多相关文章

  1. Mysql 批量修改字段存储过程
  2. mysql中select列表可以有group列表中没有的字段,大家不觉得奇怪吗
  3. mySQL库编码,PHP页面编码和mysql_query("set names 'GBK'")编码问
  4. 如何将空值传递给外键字段?
  5. MYSQL SET类型字段的SQL查询某个字段保函某个值的查询
  6. mysql 排序两个字段/列表先根据时间升序排序,时间相同再根据商家I
  7. 转!mysql 查询 distinct多个字段 注意!!
  8. 基础概念---mysql 列转行,合并字段
  9. mysql 怎么给一个表一次增加多个字段, mysql 添加 多个 字段

随机推荐

  1. 如何将值发送到Ionic中具有条件的其他页
  2. 将常量文本放在EditText中,这应该是不可编
  3. 使用mediaplayer + surfaceview来播放视
  4. android try catch并不影响性能
  5. Lance老师UI系列教程第三课->QQ登录注册
  6. getCacheDir()和getFilesDir()方法区别
  7. 关于AndroidStudio中提示cannot resolve
  8. eclipse转用android studio——常用快捷
  9. Android多窗口分屏(原生方法)
  10. JS唤醒Android APP(包括在外部浏览器和We