0

I'm creating a timetable page for a community-run bus route planner/timetable site, and I'm currently coding a page where the user can select their desired bus from a drop-down list, and then PHP will output the full bus timetable from the MySQL database.

To do this I am POST'ing (via Javascript) the value of the dropdown when the user selects their bus route, which will result in the bus timetable being echo'd via PHP. As Wordpress uses no *.php in its pages, I cannot easily POST the data.

I am using the following Javascript code:

jQuery(document).ready(function($) {

$("#selectid").change(function() {
  var selection = $("#selectid option:selected").val();
  var file = "index.php";

  $.post(file, {selection : selection} , function(hdreturn){ 
  });
});

});

If I use var file = "bustimes.org.gg/timetables/", Chrome's Developer Console gives me a 503 Service Temporarily Unavailable error, the same thing also happens if I try using var file = "bustimes.org.gg/timetables/index.php".

How can I reliably POST to a Wordpress page, whilst not having to pipe to an external PHP file?

Edit: I tried using the code in the link that @Milo supplied and put into the Javascript function:

jQuery(document).ready(function($) {

$("#selectid").change(function() {
  var selection = $("#selectid option:selected").val();
  var data = {
        'action': 'selectid',
        'whatever': ajax_object.we_value      // We pass php values differently!
    };
    // We can also pass the url value separately from ajaxurl for front end AJAX implementations
    jQuery.post(ajax_object.ajax_url, data, function(response) {
        alert('Got this from the server: ' + response);
    });

  $.post(file, {selection : selection} , function(hdreturn){ 
  });
});

});

But however I'm getting this error:

enter image description here

  • are you trying to reload the whole page or get data via an ajax request? if you're trying to reload the page, you should just submit the form that contains the select element. jQuery's post is for ajax. if you want to fetch data via ajax, start here. – Milo Oct 14 '14 at 20:02
  • @Milo It's being fetched via ajax. – AStopher Oct 14 '14 at 20:03
  • @Milo I updated the question. – AStopher Oct 14 '14 at 20:08
  • Are you using the WordPress ajax or your own Ajax, because the WordPress ajax is probably most advised in this scenario. View Codex – Howdy_McGee Oct 14 '14 at 20:11
  • @Howdy_McGee I'm using the standard Wordpress Ajax. – AStopher Oct 14 '14 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.