3

phonegap と jquery mobile を使用して Web アプリ (Android) を作成しています。

私はjsonとしてhtmlフォームから3つのフィールドをphpページに送信しようとしています.phpページはjson文字列/オブジェクトをデコードし(json、ajax、jqueryは初めてです)、3つのフィールドをmysqlクエリとして私のデータベースに追加しますローカルホスト。

私の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> 

質問は:

PHP ファイル (connection.php) に送信した文字列から 3 つのフィールド (ID、名前、アドレス) を抽出するにはどうすればよいですか? connection.php は私のローカル サーバーでホストされています。

データベースへの接続や、mysql へのクエリの追加にも精通しています。私は ajax と jquery と json に慣れていないので、3 つのフィールドを抽出するための助けだけが必要です。

今のところ、これは私が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);
?>

このファイルに関連するコードを追加して、私を助けてください。大変お世話になります。:)

4

4 に答える 4

3

やりたいことこれこれ

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

次に、あなたの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);

ただし、これらの入力を挿入する前に必ずサニタイズしてください。

于 2012-07-06T15:42:42.860 に答える
1

使用する:

$id = json_decode($_POST['id']);
$name = json_decode($_POST['name']);
$Address = json_decode($_POST['Address']);

$sql .= "VALUES ($id, '$name', '$Address')";

代わりに :

$sql .= "VALUES ($id, '$name', '$Address')";
于 2012-07-06T13:13:57.820 に答える
0

$ id_json_encoded = json_encode($ _ POST ['id']);を使用します。フォームの投稿をエンコードするには、次のようなjqueryスクリプトを使用します

<script type="text/javascript">

$(document).ready(function(){

        $("#audit").click(function(){
            $.post("/audit_report/"+$('#branch').val()+"/"+$('#ttype').val()+"/"+$('#from').val()+"/"+$('#to').val(),

                    {
                        targetDiv:"divswitcher"
                    }
                ,
                    function(data){

                     $("#imagediv").html('<img src="/public/images/spinner_white.gif"> loading...');  
                //alert(data);
                $("#bodydata").html(data);
                 $("#imagediv").html('');  
                    //  $("#divswitcher").html(data);
        });                 
        });     
        });     

于 2012-07-10T11:33:01.947 に答える
0
this is the complete code that encodes form data and send it to the server using ajax


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" language="javascript" src="jquery-ui-1.8.16.custom.min.js"></script> 





    <script type="text/javascript">
$(document).ready(function()
  { 


            $("#btn").click(function(){

            var encoded = json_encode("/audit_report/"+$('#id').val()+"/"+$('#name').val()+"/"+$('#Address').val());
                $.post(encoded,

                        {
                            targetDiv:"divswitcher"
                        }
                    ,
                        function(data){

                         $("#imagediv").html('<img src="spinner_white.gif"> loading...');  
                    //alert(data);
                    $("#bodydata").html(data);
                     $("#imagediv").html('');  
                        //  $("#divswitcher").html(data);
            });                 
            });     
            });     
</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" name="id" >
</label>
<label for="name">
<input type="text" id="name" name="name" >
</label>
<label for="Address">
<input type="text" id="Address" name="Address" >
</label>

<input type="button" id="btn" name="btn" value="Add record" />
</form>
</div>

</body>
</html>


<div id="bodydata" class="class">

</div>
于 2012-07-11T05:27:45.287 に答える