-1

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.

  • Look into [jquery ajax](https://api.jquery.com/jquery.ajax/) if you want to pass data from the client side back to the server side. – Patsy Issa May 21 '14 at 07:59
  • but is it possible to use that function in the same page? – DoubleClickOnThis May 21 '14 at 08:00
  • this will give a perspective http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript/23740549#23740549 – user1978142 May 21 '14 at 08:10
  • @DoubleClickOnThis have you looked at the network tab in your browsers' development tools to ensure the ajax request is being sent? – Darren May 21 '14 at 08:36
  • you can't use php tags inside a script area! remove that bogus url! – CME64 May 21 '14 at 08:38
  • @CME64 What? Yes you can. – Ben Fortune May 21 '14 at 08:39
  • i noticed that it's sending over company_name=Company%20C maybe because of the space, but ive changed it to Company_C and performed a str_replace in companyArray.php. shall update my codes – DoubleClickOnThis May 21 '14 at 08:40
  • @BenFortune that is logical, the server returns the page contents as a string then it is processed by the browser,, you're right you can do that but usually you won't need it.. thanks for the info – CME64 May 21 '14 at 08:49

2 Answers2

1

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.

CME64
  • 1,673
  • 13
  • 24
  • so you're saying i cant have the script on the same page as the drop down list? – DoubleClickOnThis May 21 '14 at 08:04
  • @DoubleClickOnThis you can have php and js script in same page, but php has to check if there is _GET or _POST parameters to process data and and show modified HTML with php. – Justinas May 21 '14 at 08:06
0

You aren't concatenating your data.

'company_name=' $('#company').val(),

should be

'company_name=' + $('#company').val(),
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80