繁体   English   中英

将值从Javascript传递到脚本PHP,并生成JSON到其他页面

[英]Pass values from Javascript to script PHP, and generate JSON to other page

我在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页面( eclipse中的mapa.html ),该页面从php(wtf !?)获取json ...该怎么做? :S ..

另外,当我单击提交表单(在index.html中 )时,必须重定向到mapa.html,以获取json ....

我真的不知道,也没有任何想法来解决这个大问题...

有人可以帮帮我吗?...

对不起英语不好

您可以将json作为附加在url或隐藏字段上的url_encoded字符串传递。 如果需要在页面之间进行通信,请让生成页面将json回显到隐藏字段中。 您也可以对json执行ajax或curl请求

类似于(在index.html中)

<?php $data = url_encode(json_encode($array)) ?>
    <form action="mapa.html?data=<?php echo $data?>" method="get">
    ...
    </form>

HTML中的form元素没有action属性。 当用户单击“提交”时,浏览器将在此处发送请求。 因此,如果要将请求发送到PHP脚本,则需要在表单的action属性中进行设置。

假设Consulta.php与HTML页面位于同一目录中...

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

现在,提交此表单后,请求将定向到您的PHP脚本,该脚本将生成JSON。

至于要获取PHP中表单发送的值,您需要使用为这些SELECT元素定义的name属性。

在您的情况下,这是combustibleSelectdistritoSelect

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

请不要使用旧的ext / mysql API与数据库连接,因为它已被弃用,并且在以后的PHP版本中可能会删除。 考虑使用更新的PDO或MySQLi API来与PHP中的MySQL数据库连接。

至于将数据导入javascript,您想使用ajax向 PHP脚本发出XHR请求 这将允许您通过请求javascript出门并在后台向您的PHP脚本发出请求,然后随便将JSON交还给您,从而用javascript填充您想要在HTML的DOM中填充的所有内容。无需用户离开页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM