0

I am trying to minimize my code by putting it into an array but nothing happens. I can't figure out what I am doing wrong. Here's the code

<html>
    <head>

    <title>test</title>

    <!-- JavaScript -->
    <script src="js/jquery-1.5.2.js" type="text/javascript"></script>   
    <script type="text/javascript">

        var phpfile = new Object();
        phpfile["testselect"] = "zoomchange.php";


        var elementID = new Object();
        elementID["testselect"] = "#testdiv";

        $(document).ready(function(){

            $("select").change(function() {
              $.post(
                phpfile[$(this).id()],
                $(this).serialize(),
                function(data) {
                  $(elementID[$(this).id()]).html(data)
                }
              );

            });

        });

    </script>   

    </head>

    <body>


    <select id="testselect">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>

    <div id="testdiv"></div>

    </body>
</html>

here is the zoomchange.php:

<?PHP echo $_REQUEST['testselect'] ; ?>
3
  • Any kind of errors from Firebug/Dev tools? Commented May 5, 2011 at 5:15
  • 1
    first you need to choose one doctype and delete comment and don't try to minimize your code, but make it clear and readable. Commented May 5, 2011 at 5:17
  • validate your java script code in jslint.com Commented May 5, 2011 at 5:19

3 Answers 3

2

Your initializers shouldn't look like this:

var phpfile = new Array();
phpfile["testselect"] = "zoomchange.php";

var elementID = new Array();
elementID["testselect"] = "#testdiv";

A JavaScript Array is indexed by numbers, not strings. You want simple object literals:

var phpfile   = { testselect: 'zoomchange.php' };
var elementED = { testselect: '#testdiv'       };

Then, your POST callback is confused:

function(data) {
    $(elementID[$(this).id()]).html(data)
}

this isn't what you think it is when that function is called. You want something more like this:

$("select").change(function() {
    var that = this;
    $.post(
        phpfile[that.id],
        $(this).serialize(),
        function(data) {
            $(elementID[that.id]).html(data);
        }
    );
});
Sign up to request clarification or add additional context in comments.

Comments

0

This

function(data)
{
  $(elementID[$(this).id()]).html(data);
}

instead of this

function(data)
{
  $(elementID[$(this).id()]).html(data)
}

Is this the error ?

2 Comments

JavaScript doesn't require semicolon statement terminators (even though you really should use them).
in javascript a semicolon before } is optional
0

You should do new Object() instead of new Array(). Edit: There are other mistakes, your js code should be this:

<script type="text/javascript">
        var phpfile = {};
        phpfile["testselect"] = "zoomchange.php";

        var elementID = {};
        elementID["testselect"] = "#testdiv";

        $(document).ready(function(){

            $("select").change(function() {
              var $select = $(this);
              $.post(
                phpfile[$select.attr("id")],
                $select.serialize(),
                function(data) {
                  $(elementID[$select.attr("id")]).html(data)
                }
              );

            });
       });
</script>

4 Comments

@ariel, It's a bad practice to store hashtables in javascript arrays, they must be in objects
cool.. didn't knew that. but that wasn't the error, probably was the reference to 'this' inside the ajax callback
ok the obove script updated the div with the random text i but on the bottom of the page... but the select is not getting submitted
with Edgar Villegas Alvarado script i checked firebug and there is no post data

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.