如何创建一个按钮来学习javascript函数,但页面无法刷新

如何创建一个按钮来学习javascript函数,但页面无法刷新,javascript,php,html,google-maps-api-3,Javascript,Php,Html,Google Maps Api 3,函数showFeature(geojson,样式){ currentFeature_或_Features=新的GeoJSON(GeoJSON,样式| | null); if(currentFeature\u或\u Features.type&¤tFeature\u或\u Features.type==“错误”){ document.getElementById(“在此处放置字符串”).value=currentFeature\u或\u Features.message; 返回; }

函数showFeature(geojson,样式){
currentFeature_或_Features=新的GeoJSON(GeoJSON,样式| | null);
if(currentFeature\u或\u Features.type&¤tFeature\u或\u Features.type==“错误”){
document.getElementById(“在此处放置字符串”).value=currentFeature\u或\u Features.message;
返回;
}
if(当前特征或特征长度){
对于(var i=0;i}
showFeature()
函数编写为:

function showFeature(event,geojson_parce,adressStyle){
    event.preventDefault();
    //...do rest of function
}
电话将会是

onclick=“showFeature(事件、地理位置、地址样式)”

事件
对象上调用
preventDefault()
,将阻止在单击提交时刷新页面


要实现您想要做的事情(从数据库获取详细信息,在地图上显示标记,而无需重新加载页面),您需要重新构造代码并使用ajax

目前,表单的提交将设置变量
$term
,然后在sql查询中使用该变量,但显然这会产生重新加载页面的负面影响

实现所需操作的最佳方法是通过Ajax向脚本发送POST请求,该请求将json响应发送回javascript函数,而不是重新加载页面,因为页面是异步的

下面的代码片段没有经过测试,但它们应该让您了解我的意思

/*
    At the top of your php page, this php code will get the data
    from the database when it receives a POST request:
*/

<?php
    $host = "localhost"; 
    $user = "postgres"; 
    $password = "20152016"; 
    $db = "Projet"; 

    $con = pg_connect("host=$host dbname=$db user=$user password=$password") or die ("Could not connect to server\n"); 


    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        /* get the POSTed variables from your ajax call and query the database */

        @ob_clean();

        $term = isset( $_POST['term'] ) && !empty( $_POST['term'] ) ? $_POST['term'] : false;
        if( !$term ) exit( json_encode( array('error'=>'Search term was empty') ) );

        $sql = "select row_to_json(fc)
                from ( select 'featurecollection' as type, array_to_json(array_agg(f)) as features
                from ( select 'feature' as type, st_asgeojson(lg.geometry)::json as geometry, row_to_json(lp) as properties
                from poi as lg inner join ( select id, description from poi where nom like '%".$term."%') as lp 
                on lg.id = lp.id  ) as f )  as fc;";

                /* Query db */
                $result = pg_query($con, $sql);

                /* Process the recordset and construct your data response */

                /* 
                   send json data: it is this data that YOUR showFeature
                   function uses. It equates to "http.responseText" 
                   in the ajax function
                */
                header("Content-Type: application/json; charset=UTF-8",true);
                echo $data;
        exit();
    }

    /* Any other code below ... */
?>
还有html表单

<form>
    <input type="text" id='term' name="term" /><br />  
    <input type="button" onclick="getdata();" value='Submit query' />    
</form>



因此,用户单击按钮(现在不再提交传统意义上的表单),该按钮向同一页面发送AJAX POST请求-请求由上面的PHP代码处理(仅当请求是POST请求时才会执行,尽管您可能希望添加其他条件),PHP代码查询数据库,处理记录集并回显用于向地图添加标记的json数据。您原来的javascript函数
showFeature
现在充当ajax请求的
回调
,但是,由于您的问题中没有显示它,我不知道它是如何工作的,或者它到底做了什么。

由于for中的提交按钮,页面正在刷新。要防止页面加载,您需要在表单中取消绑定submit。您可以通过以下方式实现这一点:

  • 搜索

  • 在这个函数的末尾,只需添加showFeature

    return false;
    
  • 或者像下面一样

    搜索


  • 希望这对您有所帮助。

    您面临的问题是什么,您的问题是什么?将按钮类型从
    submit
    更改为
    button
    如果我搜索点(var geojson\u parce),页面将显示结果(showFeature(geojson\u parce,addressstyle),但不会刷新(tourned)@RamRaider我这么做了,但他不能说明问题?!好吧-你也需要做一些其他的事情-我会解释一个问题answer@SteveCroHunter你能帮我解释一下吗?你能不能终止函数js?如果你能更改所有代码,因为我不能显示它,,,,,:/
    return false;