简体   繁体   English

PHP和Javascript通讯

[英]PHP and Javascript Communication

<form action="../">
<select onchange="window.open(this.options[this.selectedIndex].value,'_top')">
    <option value="">Choose a zipcode </option>
    <option value="92507">92507</option>
    <option value="30078">30078</option>
    <option value="92606">92606</option>
    <option value="30004">30004</option>
    <option value="32034">32034</option>
    <option value="37160">37160</option>
</select>
</form>

I have created a drop down menu where the user could select a zip code. 我创建了一个下拉菜单,用户可以在其中选择邮政编码。 I want to send the zipcode that the user has selected to a php script. 我想将用户选择的邮政编码发送到php脚本。 How to go about this? 怎么办呢?

This is what I have done/understood so far. 到目前为止,这是我已经做/理解的。 I need to add this line of code $php_variable = $_GET['param1']; 我需要添加以下代码行$php_variable = $_GET['param1']; in my php file which would get the zip code as param1 which the javascript sends over the url. 在我的php文件中,该邮政编码将作为javascript通过URL发送的param1。 I can use an ajax function in my javascript and call a post to send over the data. 我可以在我的JavaScript中使用ajax函数并致电以发送数据。 Am I on the right track? 我在正确的轨道上吗? How do I actually save the user's selection of zip code into a variable. 我实际上如何将用户对邮政编码的选择保存到变量中。 Also shouldn't the ajax be in a script? 另外,ajax不应包含在脚本中吗? Can I also put the drop down menu in the script portion as well? 我还可以在脚本部分中放置下拉菜单吗?

Almost there. 快好了。 The current problem is that when I click on a particular zip code. 当前的问题是,当我单击特定的邮政编码时。 This is the url I get http://localhost:8888/92606 instead of http://localhost:8888/getuv.php?param1=92606 这是我得到的URL http://localhost:8888/92606而不是http://localhost:8888/getuv.php?param1=92606

<form action="page.php" method="post">
<selecton name="zip" onchange="window.open(this.options[this.selectedIndex].value,'_top')">
<option value="">Choose a zipcode </option>
<option value="92507">92507</option>
<option value="30078">30078</option>
<option value="92606">92606</option>
<option value="30004">30004</option>
<option value="32034">32034</option>
<option value="37160">37160</option>
</select>
</form>

on the page.php you should code like 在page.php上,您应该像

<?php
$code=$_POST['code'];
echo 'the zip code is'.$code;
?>

You should try jquery way for frontend. 您应该尝试使用jquery方式进行前端。

<select id="myList">
    <option value="">Choose a zipcode </option>
    <option value="92507">92507</option>
    <option value="30078">30078</option>
    <option value="92606">92606</option>
    <option value="30004">30004</option>
    <option value="32034">32034</option>
    <option value="37160">37160</option>
</select>



$("#myList").change(function () {
    var myValue = $("#myList option:selected").val();
    sendToServer(myValue);
});
var sendToServer = function(myValue){
 // Use some ajax
    jQuery.ajax(
        type: "GET",
            url: 'http://host.com/' +'param1='+ myValue,
            beforeSend: function(){ 
                // do something
            },
            success: function(data){
                // success 
            }
        });
}

You need to assign 'param1' to name in the select tag: 您需要在select标签中为“ param1”分配名称:

code:

 <form action="../"> <select name = 'param1' onchange="window.open(this.options[this.selectedIndex].value,'_top')"> <option value="">Choose a zipcode </option> <option value="92507">92507</option> <option value="30078">30078</option> <option value="92606">92606</option> <option value="30004">30004</option> <option value="32034">32034</option> <option value="37160">37160</option> </select> </form> 

You forgot the name attribute, and the onchange method you had was just opening a new window with the param1 as the url. 您忘记了name属性,而您使用的onchange方法只是打开一个以param1作为url的新窗口。

<form action="../">
    <select name="param1" onchange="this.form.submit()">
        <option value="">Choose a zipcode </option>
        <option value="92507">92507</option>
        <option value="30078">30078</option>
        <option value="92606">92606</option>
        <option value="30004">30004</option>
        <option value="32034">32034</option>
        <option value="37160">37160</option>
    </select>
</form>

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

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