dqw70970 2014-05-21 07:57
浏览 57

将JQuery变量传递给PHP变量[Simple]

I've read lots of threads on passing Jquery variables to PHP, however almost all of the posts were on the "Expert" level, and i couldn't understand a single thing (PHP/Jquery beginner here). Hopefully I can get some help.

Once the user selects from the drop down list, the script function will run. I want to get the user's selection from jquery to a php variable. From then, I want to use the user's selection to retrieve data from the database and display values on the same page itself. I'm not getting any output.

EDITED: So this is what I did after looking at the ajax function

AFile.php

<script type="text/javascript"     src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">     
$(document).ready(function() {     
$('#company').change(function(){     

$.ajax({
    type: 'GET',
    url: '<?php echo url_mgt::getTest(); ?>',
    data: 'company_name=' + $('#company').val(),
    success: function(msg) {
      $('#other').html(msg);
}
});

});     
});     
</script>     
</head>     

<body>     

<select id="company" name="company">     
   <option value="Company_A">A</option>     
    <option value="Company_B">B</option>     
    <option value="Company_C">C</option>     
    <option value="Company_D">D</option>     
 </select>     

 <div id="other"></div>

</body>

companyArray.php

<?php 
   require('protect.php'); 
   require_once(dirname(__FILE__) . '\..\controller\company_controller.php');
   require_once(dirname(__FILE__) . '\..\controller\url.php');

   $companyCtrl = new company_controller();
   $compArray = $companyCtrl->retrieveAllCompany(); 

   if($_GET['company_name']) {
     $get_comp = $_GET['company_name'];
     $inSpace = str_replace("_"," ", $get_comp);
    foreach($compArray as $company) {
       $comp_name = $company->getCompanyName();
       if($get_comp == $company) {
        $comp_add = $company->getCompanyAddress();
         echo $comp_add;
        }//end if
     }//end foreach
   } //end if
  ?>

I inspected element, but when i click on the drop down list nothing happens, i doubt its going to the companyArray.php. I also don't think its the url_mgt::getTest() link because ive been using this url pattern throughout the project.

  • 写回答

2条回答 默认 最新

  • dongyuan6949 2014-05-21 08:02
    关注

    use Ajax request on change and then populate the received values from the server on success and you can do what you are asking for.

    note: you can't pass a variable from client side (js) to server side (php) without sending it as a request.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。