0

Alright, so right now I have a page that has two select buttons. Once both of them are set, I want the page to automatically update. Then, whenever a new option is selected each time, the page will keep automatically updating. Here's my HTML:

<form action="" method="POST" id = "multiple">
    <select name="op" id="op"> 
        <?php
            require('C:\www\php\connect.php'); 
            require('C:\www\php\stuff1.php'); // populates first select bar
        ?>   
    </select>

    <br>

    <select name="op2" id="op2"> 
        <?
            php require('C:\www\php\stuff2.php'); //populates second select bar
        ?> 
    </select>
</form>

Here was my jQuery BEFORE. Right now with this, it will automatically submit the page once two items are selected:

$(document).ready(function () {
    $('#op,#op2').on('change', function () {
        if ($('#op').val() && $('#op2').val()) {
            $("#multiple").submit();
        }
    });
});

Here's my (not working) AJAX response that I want to try to implement to do what I mentioned at the start:

$(document).ready(function (e) {
    $('#op,#op2').on('change', function () {
        $("#multiple").submit();
        var url = "C:\www\php\stuff2.php"; 
        alert("show me signs of life"); // show response
        $.ajax({
               type: "POST",
               url: url,
               data: $("#multiple").serialize(),
               success: function(data)
               {
                   alert(data); // show me something
               }
             });

        e.preventDefault();
    }
});

Where exactly am I going wrong here? I'm pretty sure that my URL situation is a little bit messed up seeing as how I use multiple php files to populate the boxes, but I feel like that doesn't matter. If it helps, the file where the magic happens is in stuff2.php, so that was the only one I've included.

4
  • I don't think u can do ajax to a local file on the hard drive ie.var url = "C:\www\php\stuff2.php"; as browsers don't allow it as it's considered cross domain Commented Feb 4, 2016 at 16:19
  • So would var url = "/php/stuff2.php"; be better? Commented Feb 4, 2016 at 16:24
  • Yes it would be since it would be looking for it on the same domain. Browsers also don't allow access to local files on the hard drive due to security so by using that relative url it should work. I'm assuming since you're using php you have a php server running on your local host Commented Feb 4, 2016 at 16:55
  • You say no submit but you have $("#multiple").submit(); right there to DO a submit. - note you need then do the UPDATE in the callback (success or the .done deferred Commented Feb 4, 2016 at 17:25

1 Answer 1

2

The code:

$("#multiple").submit()

will perform an HTTP POST request to the current URL. You don't need it if you are making AJAX calls.

Simply having:

$.ajax({
     type: "POST",
     url: url,
     data: $("#multiple").serialize(),
     success: function(data)
     {
          alert(data); // show me something
     }
});

should work just fine.

Sign up to request clarification or add additional context in comments.

4 Comments

To be specific, I'm trying to update my c3js charts. I did what you did and I got the alert. The problem now is that the charts themselves aren't updating like they're supposed to
Are you updating the object related to the chart? Take a look here: Line Chart. You should be able to update the chart by feeding it data using the 'load' method of the respective object.
Yes. But how do I get my input from my updated form and then use the load method to update the chart without ajax?
I would need a little more context to be more helpful, but, to answer the question, you simply need to do something like this: chart.load({columns: ['op', $('#op').value()], ['op2', $('#op2').value()]});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.