dongwai4434 2017-05-22 14:55 采纳率: 0%
浏览 98
已采纳

在Javascript上调用PHP函数Onchange

I want to call a function in php onchange in select of html,how can this be done?

html code

<div class="col-sm-4 form-group">
   <label for="c_city">City</label>
   <select class="form-control" id="c_city" onchange="<?php getArea(); ?>">
        <option value="">Select City Name</option>
        <!--populate value using php-->
        <?php
            $stmt ="SELECT * FROM Cities";
            foreach ($con->query($stmt) as $row) {
         ?>
         <option value="<?php echo $row['City_ID'];?>"><?php echo $row['City_Name'];?></option>
         <?php
          }
         ?>
   </select>
</div>

PHP Functions Page

function getArea() 
{
    if(isset($_GET['c_city'])) 
    {
        $c_city = $_GET['c_city'];
        echo $c_city;
    }
}
  • 写回答

2条回答 默认 最新

  • dousi8559 2017-05-22 15:06
    关注

    You can try to use ajax to call the php script and receive results back to javascript.

    For example in your html page:

    <div class="col-sm-4 form-group">
       <label for="c_city">City</label>
       <select class="form-control" id="c_city" onchange="getArea()">
            <option value="">Select City Name</option>
            <!--populate value using php-->
            <?php
                $stmt ="SELECT * FROM Cities";
                foreach ($con->query($stmt) as $row) {
             ?>
             <option value="<?php echo $row['City_ID'];?>"><?php echo $row['City_Name'];?></option>
             <?php
              }
             ?>
       </select>
    </div>
    
    <script>
     function getArea(){
      var c_city = $('#c_city').val();
       $.ajax({
               type: 'POST',
               url: "get_area.php",
               data: {c_city:c_city},
               success: function(result){
                   //do something here with return value like alert
                   alert(result)
               }
         })
    }
    </script>
    

    Then in your php script you can manipulate the value you receive from javascript

    if(isset($_POST['c_city'])) 
        {
            $c_city = $_POST['c_city'];
            echo $c_city;
        }
    

    Let me know if it helps or if you find a problem

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?