1

ページの結果をフィルタリングするために使用したいHTMLフォーム(2つのリストボックスとチェックボックス)があります。

Submitボタン( )を押す.onclick(function(){})と、ローカルサーバー上のPHPスクリプトへのjQuery AJAX呼び出しが実行されるようにするにはどうすればよいですか。最初に、リストボックスで何かが選択されているかどうか、チェックボックスがチェックしてから、それに基づいて SQL ステートメントを作成し、データベースにクエリを実行して、結果を JSON として取得します。

理論的には、どのようにしますか。テーブルからすべてを単純に取得して JSON として保存するための PHP スクリプトが既にあります。

<?php
// databse connection info
require("dbinfo.inc.php");

// try to open a connection to a MySQL server
$connection=mysql_connect($host, $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

// select the active MySQL database to work with
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

// grab data about every campus and which instituion they belong to
$query = 'select statement to grab all the data from a table';
$result = mysql_query($query);

// check if $results has anything
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

// time to start generating our huge XML
while ($row = @mysql_fetch_assoc($result)){
    $finalarray[] = $row;
    $main_arr['products'] = $finalarray;
}
//  close connection to the database
mysql_close($connection);

//  echo, save and write the file
// print json_encode($main_arr);

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($main_arr));
fclose($fp);


?>
4

2 に答える 2

1

最初に ajax を使用してフォームからデータを送信します。

$('form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url : 'myPHPfile.php',
        data: {form : $('form').serialize()}
    }).done(function(data) {
         var myJSONresult = data;
    });
});

PHPファイルでは、次のフォームを取得します

$form = $_POST['form'];

ファイルに保存する必要はありません。エコーして送り返すだけです

echo json_encode($main_arr);
于 2012-05-23T16:19:57.007 に答える
1

phpファイルに投稿してajaxでデータを取得する例です。結果はコンソールに記録されます。json が取得されたら、js で json を処理する方法を知っていると思いますか?

//the php
<?php

    //bla bla connect to database etc

    //if form has been submitted with ajax
    if(isset($_POST["ajax_request"])){

        //build query
        $id    = $_POST["id"];
        $fruit = $_POST["fruit"];
        $query = "SELECT * FROM fruits WHERE id=$id AND fruit='$fruit'";

        //fetch from db
        $result = mysql_query($query);

        //send out json
        die(json_encode($result));

    }

?>

//the jquery
<script type="text/javascript">

$(document).ready(function(){

    //on submit
    $("#ajaxForm").submit(function(e){

        //stop default page refresh
        e.preventDefault();

        //define the form
        var form = $(this);

        //get the form data
        var data = form.serialize();

        //send it via ajax
        $.ajax({
           type      : "post",
           dataType  : "json",
           url       : "url to your php here",
           data      : data+"&ajax_request=1",
           success   : function(json){
               console.log(json);
           },
           error     : function(a,b,c){
               console.log(a,b,c);
           }
        });

    });

});

</script>

//the html
<form id="ajaxForm" method="post" action="">
    <input type="text" name="id" required="required" placeholder="enter id"/>
    <input type="text" name="fruit" required="required" placeholder="enter fruit"/>
    <input type="submit" name="submit"/>
</form>
于 2012-05-23T16:50:20.040 に答える