Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type "pakket", in which I've put all kinds of data in the taxonomy terms. This custom post type has about 50 items, each with their own values for taxonomy terms. For example:

Pakket

Hakhoutsingel

(waardedaling)=0.6

(opbrengstderving)=YES

(aanlegkosten)='string'

Bossingel

(waardedaling)=6

and so on..

I have the same 50 corresponding values (hakhoutsingel, bossingel etc.) in a dropdown box on a page with a form. http://areal-agro.nl/tool/?page_id=6

I want to get the 50 values from the database (with php), and check if they match the values from the dropdown box (jQuery). I know how to do both.

Problem is, I cannot use a jQuery variable in php, because php is server-sided scripting. How can I tackle this problem?

share|improve this question
    
GET / POST variables? –  Camil Staps Feb 26 '13 at 13:35
    
I know how to post the whole form. What I want to do, however, is the following. When the user has selected a value from the dropdown box, some calculations are made real-time. –  Lars van Peij Feb 26 '13 at 13:41
    
You can send a few variables with Ajax, so you wouldn't need a page refresh. I'm not sure what your problem is. –  Camil Staps Feb 26 '13 at 13:44
    
okay, I'm new to ajax. What would be a basic setup? –  Lars van Peij Feb 26 '13 at 13:50
    
Now I understand. I'm going to write an example for you! –  Camil Staps Feb 26 '13 at 14:11
add comment

1 Answer

up vote 0 down vote accepted

At first, you'll have to make a dropdown filled with your values from the database:

<select id="pakket">
  <?php
    foreach ($allPakkets as $pakket) {
      echo '<option value="'.$pakket['id'].'">'.$pakket['name'].'</option>';
    }
  ?>
</select>

And a div to put results in:

<div id="results"></div>

Now, we add a piece of javascript:

jQuery('#pakket').change(function(){ // when the dropdown changes...
  var newvalue = jQuery('#pakket').val(); // get the new value
  jQuery.ajax({
    method: 'GET',
    url: 'url-to-script.php',
    data: {
      newvalue: newvalue
    }
  }).done(function(response){ // after the request has been loaded...
    jQuery('#results').html(response); // put the response in the div
  });
});

Now the only thing you have to do is make a url-to-script.php which loads the properties of the value sent in $_GET['newvalue']. The output of this PHP script will be placed in the div#results.

share|improve this answer
    
how should I build/format this url-to-script? Can you give a basic example. Thank you so far! –  Lars van Peij Feb 26 '13 at 14:56
    
I cannot give you a basic example since it depends on your database very much. What you have to do is search your database for $_GET['newvalue'] and output the other information. –  Camil Staps Feb 26 '13 at 15:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.