0

Trying to create a nice dynamic selection process. There are two parts to the selection process: Choose category, then choose name. The form process works just fine. I then want to display an image based on the name chosen. I can't seem to figure it out, here's the code:

<form action="file.php" method="post">
    <select id="first-choice" name="cardset">
    <?php foreach ($data as $row): ?>
        <option><?=$row["name"]?></option>
    <?php endforeach ?>
    </select>
    <select id="second-choice" name="card">
        <option>Please choose from above</option>
    </select>
    <img src="" name="image-swap">

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script language=JavaScript >
        $(document).ready(function() {
            $("#first-choice").change(function() {
                $.get("getter.php", { choice: $(this).val() }, function(data) {
                    $("#second-choice").html(data);
                });
            });
        });

        $(document).ready(function() {
            $("#card").change(function() {
                var first = $("first-choice");
                var sec = $(this).val();

                $("#image-swap").html(src ? "<img src='/pics/" + first + sec + "'>" : "");
            });
        });
    </script>

I am trying to pull the image from pics/"first"/"sec".jpg

Michael Ramos
  • 5,523
  • 8
  • 36
  • 51

3 Answers3

0

Two things:

You only need 1 document ready wrap:

$(document).ready(function(){
    $("#first-choice").change(function() {

       $.get("getter.php", { choice: $(this).val() }, function(data) {
            $("#second-choice").html(data);
        });
    });
    $("#card").change(function() {
        var first = $("first-choice");
        var sec = $(this).val();

        $("#image-swap").html(src ? "<img src='/pics/" + first + sec + "'>" : "");
    });
});

The second, non of your options have values, which is why it isn't passing.

<select id="first-choice" name="cardset">
    <?php foreach ($data as $row): ?>
        <option value="<?=$row["name"]?>"><?=$row["name"]?></option>
    <?php endforeach ?>
</select>

Given if the is correct. I've never used = is php to echo, normally I would do <?php echo $row["name"]; ?>.

0

Hard to tell without seeing the HTML, but . . .

var first = $("first-choice");

. . . is missing the .val() at the end. If you change it to this, it should get you much closer to what you are looking for:

var first = $("first-choice").val();

At the moment, you are trying to append the jQuery reference to the HTML option to a string value (sec).

UPDATE

Was looking a little further and found some other issues . . .

1) You are using an HTML img element, but you've given it a name attribute value of "image-swap":

<img src="" name="image-swap">

. . . and are trying to reference it with a jQuery "id" selector:

$("#image-swap")

In order for that selector to work, you must change the name attribute to and id attribute, like this:

<img src="" id="image-swap">

2) You appear to be attempting to update the src attribute of the img tag, but you don't have that quite right in your code:

$("#image-swap").html(src ? "<img src='/pics/" + first + sec + "'>" : "");

There are a couple of issues here:

  • src does not exist as a variable, but you appear to be checking for its existence as one
  • you want to update the src attribute of the img, but you are using the .html() method, which sets/gets the HTML content of a tag, not its attributes

To do what you appear to be wanting to do, you probably need to use this code:

$("#image-swap").attr("src", (first !== "" &&  + sec !== "") ? "pics/" + first + "/" + sec + ".jpg" : "");

That will update the src attribute with the image location, if neither of the selections has an empty value, or with "", if one of them does.

talemyn
  • 7,822
  • 4
  • 31
  • 52
  • You understand best what I am trying to do. However I left out that I am trying to pull the image from pics/first/sec.jpg and Its not working out with what i have added... $("#image-swap").attr("src", (first !== "" && + sec !== "") ?"pics/" + first "/" sec".jpg" : ""); – Michael Ramos Jan 31 '14 at 18:04
  • You almost have it . . . you are missing some `String` concatenation operators (`+`) in the code . . . it should be: `$("#image-swap").attr("src", (first !== "" && + sec !== "") ? "pics/" + first + "/" + sec + ".jpg" : "");`. I've updated my code to match the final format of the `src` that you are looking for. – talemyn Jan 31 '14 at 18:21
  • Thanks.. I can't seem to get change to fire though. The selection process still works but no picture is appearing. I checked and made sure the file path was right and it is when I set the default src to an image. Here is how it should work: Select Option 1 --> Select option 2 --> Image appears based off selection. – Michael Ramos Jan 31 '14 at 19:04
  • What is the full path to the image and the full path to the HTML page that this code is on? There may be an issue with the relationship of the relative path. – talemyn Jan 31 '14 at 19:06
  • I had #card as changing parameter, but as you pointed thats not the id, second-choice was, so I changed it to, that, It fires, but image is not found, the first path is being defined as undefined (first isn't being defined... sec is though): http://localhost/~myname/mysite/pics/undefined/Image.jpg – Michael Ramos Jan 31 '14 at 19:43
  • Just had to add # to indicate back to first-choice. – Michael Ramos Jan 31 '14 at 19:46
  • Ahh . . . awesome. Glad you got it sorted out. :) – talemyn Jan 31 '14 at 19:47
  • Thank you, it works great. Another problem is that some of the folders and even image names have spaces, and they won't show. Should I post that as another question? – Michael Ramos Jan 31 '14 at 19:49
  • Try encoding the value before setting it . . . check out this post for more details: http://stackoverflow.com/questions/2856294/css-and-jquery-spaces-inside-image-name-break-code-of-url – talemyn Jan 31 '14 at 19:54
0

I figured out $("first-choice").val() and Try this,

$(document).ready(function(){
    $("#first-choice").change(function() {
    $.get("getter.php", { choice: $(this).val() }, function(data) {
        $("#second-choice").html(data);
    });
});


    $("#card").change(function() {
    var first = $("first-choice").val();
    var sec = $(this).val();

    $("#image-swap").html(src ? "<img src='/pics/" + first + sec + "'>" : "");
    });
});
dhana
  • 6,487
  • 4
  • 40
  • 63
  • You don't need to find the selected option to get it's value . . . (`$("first-choice").children(":selected").val();`) . . . `$("first-choice").val();` will give you the value of current value of the select box (i.e., the value of the currently selected option). – talemyn Jan 31 '14 at 16:57