0

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?

5
  • GET / POST variables?
    – user26607
    Feb 26, 2013 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. Feb 26, 2013 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.
    – user26607
    Feb 26, 2013 at 13:44
  • okay, I'm new to ajax. What would be a basic setup? Feb 26, 2013 at 13:50
  • Now I understand. I'm going to write an example for you!
    – user26607
    Feb 26, 2013 at 14:11

1 Answer 1

0

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.

2
  • how should I build/format this url-to-script? Can you give a basic example. Thank you so far! Feb 26, 2013 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.
    – user26607
    Feb 26, 2013 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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