0

我正在eclipse中做一个android应用程序(使用phonegap + json + php),我遇到了一些麻烦......我真的不知道如何......

  • 我有一个生成 json 的 php 文件(consulta.php),它位于我的服务器中(192.168.1.200/test/consulta.php

    header('content-type: application/json');
    
    mysql_connect("localhost","jhonatan","jsandoval");
    mysql_select_db("tesis");
    
    $array = array();
    
    $query = mysql_query("SELECT * FROM GRIFO");
    
    while($fila = mysql_fetch_object($query)){
        //echo $fila['id'] . " " . $fila['grifo'] . " " . $fila['distrito'] . "<br/>";
        $array[] = array('id'=>$fila->id,
                        'grifo'=>$fila->grifo,
                        'distrito'=>$fila->distrito,
                        'latitud'=>$fila->latitud,
                        'longitud'=>$fila->longitud);
    }
    
    
    echo json_encode($array);
    

因此,在我的 index.html(来自 android 应用程序,eclipse 中的 phonegap)中,有一个表单,它有 2 个选择(HTML)。

<form id="combustibleForm" method="get">
        <select id="combustibleSelect" name="combustibleSelect" data-theme="a">
        <option value="gnv" >Gas Natural Vehicular</option>
        <option value="glp" >Gas Licuado de Petróleo</option>
        </select>

        <br/>

        <select id="distritoSelect" name="distritoSelect" data-theme="a">
        <option value="SJL" >San Juan de Lurigancho</option>
        <option value="Miraflores" >Miraflores</option>
        <option value="Chorrillos" >Chorrillos</option>
        <option value="Surquillo" >Surquillo</option>
        </select>

        <br/>

        <input type="submit" data-role="button" id="continuarBtn" value="Continuar.." />
</form>

我必须将这些选择的值传递给上面的 php 文件并从那里生成一个 json:

// GET THE VALUE OF THE SELECTS ... I don't know if this is correct? :S
$tipo = $_GET['tipo'];
$distrito = $_GET['distrito'];
...
...
$query = mysql_query("SELECT * FROM GRIFO WHERE (tipo='$tipo' and distrito='$distrio')");
...
...
// Generate json
echo json_encode($array);

这就是我要做的:在那之后......我有另一个html页面(mapa.html,在eclipse中),它从php(wtf!?)中获取json......如何做到这一点?:S ..

Also, when i Click the submit form (in index.html), must redirect to mapa.html , to get the json....

I really don't know and don't have any ideas to do this big problem...

Can someone please help me?...

Sorry for bad english

4

2 回答 2

0

You could pass the json as an url_encoded string appended to the url or a hidden field. if you need to communicate beteween pages, have the generating page echo the json into a hidden field. You could also do ajax or curl request for the json

Something like (in index.html)

<?php $data = url_encode(json_encode($array)) ?>
    <form action="mapa.html?data=<?php echo $data?>" method="get">
    ...
    </form>
于 2012-12-09T22:16:13.400 回答
0

Your form element in the HTML has no action attribute. This is where the browser will send the request when the user clicks on submit. So, if you want to send the request to your PHP script you will need you need to set that in the action attribute of your form.

Assuming consulta.php is located in the same directory as that of your HTML page...

<form id="combustibleForm" method="get" action="consulta.php">
        <select id="combustibleSelect" name="combustibleSelect" data-theme="a">
        <option value="gnv" >Gas Natural Vehicular</option>
        <option value="glp" >Gas Licuado de Petróleo</option>
        </select>

        <br/>

        <select id="distritoSelect" name="distritoSelect" data-theme="a">
        <option value="SJL" >San Juan de Lurigancho</option>
        <option value="Miraflores" >Miraflores</option>
        <option value="Chorrillos" >Chorrillos</option>
        <option value="Surquillo" >Surquillo</option>
        </select>

        <br/>

        <input type="submit" data-role="button" id="continuarBtn" value="Continuar.." />
</form>

Now, when this form is submitted the request will be directed to your PHP script, which will generate the JSON.

As for getting the values sent by the form in your PHP you need to use the name attributes you defined for those SELECT elements.

In your case that is combustibleSelect and distritoSelect.

$_GET['combustibleSelect']; // This will be the value of the 1st SELECT box
$_GET['distritoSelect'];    // This will be the value of the 2nd SELECT box

Please do not use the old ext/mysql API to interface with your database as it has been deprecated and may be removed in future versions of PHP. Consider using the newer PDO or MySQLi APIs instead to interface with your MySQL databse in PHP.

As for getting the data into javascript, you want to use ajax to make an XHR request to your PHP script. This will allow you to populate whatever you want to populate in the DOM of your HTML with javascript, by asking javascript to go out and make a request to your PHP script in the background and then hand you back the JSON to do with as you please without the user ever having to leave the page.

于 2012-12-09T22:19:25.660 回答