jQuery保存和加载功能

时间:2014-07-13 17:30:59

标签: jquery load

我有这个代码,当点击保存按钮时,它会保存包含vectorOutput和vectorName的数据。基于保存功能,我想编写一个函数,在单击按钮加载时加载这些数据。有人能帮助我吗?

    jQuery(document).ready(function(){           
             /*if the submit Button gets clicked this function is called*/
            jQuery("#submitButton").click(function() {

                /*the text of the html field output and vectorName are stored into javascript variables vectorOutput & VectorName*/
                var vectorOutput = jQuery("#output").val();
                var vectorName = jQuery("#vectorName").val();

                /*the jQuery.ajax gets openend*/
                jQuery.ajax({
                    /*typ POST gets defined*/
                    type: "POST",
                    /*the PHP file that receives the POST*/
                    url: "parser22.php",
                    /*the data that gets transfered*/
                    data: {
                        /*vector: contains the geometric information of the overlay, in kml format*/
                        vector: vectorOutput,

                        /*name: contains the chosen name of the geofence*/
                        name: vectorName
                    },
                    success: function(html){ //so, if data is retrieved, store it in html
                        /*if the save button gets clicked the field with the geofencename */
                        jQuery("#output").val('');
                        /*and kml koordinates get emptied*/
                        jQuery("#vectorName").val('');
                        /*to show the user that it worked alert Window pops up*/
                        alert("Geofence successfully saved!");
                    }
                }); //close jQuery.ajax(
            });
        });

1 个答案:

答案 0 :(得分:1)

我给你一个指南,所以你可以学习:)

因此,假设parser22.php文件收到POST个vars,然后对您的数据库执行INSERT INTO语句...

尝试执行相同操作,但在ajax调用中使用GET作为type:,并在数据库上执行SELECT语句。

使用firebug(firefox)或开发人员工具(chrome)查看实际发送和接收的内容。

ajax()来电中使用此功能进行日志记录:

success function(html): {console.log(htm)}, 
error function(e) {console.log(e)} 

在控制台中获取信息。 console.log是alert()

的更好选择

修改

/*the SELECT query gets created*/ 
$eintrag = "SELECT geofencename,geometry,fk_user_geofence FROM public.geofence"; 

/*the above mentioned query gets executed 
  --> this always executes a query, no matter what kind */ 
$eintragen = pg_query($eintrag);

另见W3schools有关SQL和查询数据库的简单介绍