Search code examples
phpjquerycart

put selected values from textbox and dropdown to a basket like amazon


I have a PHP survey form in which there is a question "Which are your favourite movies?". for this question, I put a dropdown which enable users to select movies by title or by actor. If user select "by title", a textbox (auto-complete) will be shown where he can insert a movie name. If user select "by actor", a new window will be opened containing a textbox where user should insert an actor name, then a dynamic dropdown is populated showing list of movies by that actor.

Question:

How can I get the selected movies (from textbox and also dropdown in new window) and put them in a basket like amazon shopping cart? I searched a lot, but I really could not find the solution.. I can put the selected values in a new dropdown, but my professor asked me to use the same method like amazon and put them in a basket!!

UPDATE:

Here is what I have tried:

<html>
<head>
 <link type="text/css" href="res/jquery-ui.css" rel="stylesheet" />
 <script type="text/javascript" src="res/jquery.min.js"></script>
 <script type="text/javascript" src="res/jquery-ui.min.js"></script>
</head>

<body>
<div class="movienames">
<select id="selectType" name="source" style="size=5px; width:100px; height:30px;">
  <option value="">MoviesBy</option>
  <option value="byTitle">byTitle</option>
  <option value="byActor">byActor</option>
  <option value="byDirector">byDirector</option>
</select>

<div id="m_scents">
<p>
 <label style="margin-bottom:10px;" for="m_scnts"></label>
<input class="autofill4" type="textbox" name= "q27[]" id="q" style="display:none;" placeholder="Enter movie titles here" />
<!--<a href="#" id="addScnt4">Add more movies</a>-->
<input type="button" value=">> Add to selected list >>" id="btnMove" style="display:none;"/>
<input name="s" value="all" type="hidden"/>
<label style="margin-bottom:10px;" for="m_scnts"></label>
</p>
</div>
<select id="selectedItems" name="selectedItems[]" multiple="multiple" style="width:200px; size:10px;">
</select>

<script type="text/javascript">
$(document).ready(function () {
  $("#selectType").change(function () {

               if ($(this).val() == "byTitle") {
                    $("#q").show();
                    $("#btnMove").show();
                    $("#q").focus();
                    $("#q").autocomplete({
                        minLength: 0,
                        delay:5,
                        source: "mona.php",
                        focus: function( event, ui ){
                             event.preventDefault(); //This prevent the inserted text to be changed while moving in suggest list 
                        return false;
                        },
                select: function( event, ui ) {
                             $(this).val( ui.item.movieName );
                              return false;
                        }
                    }).data("uiAutocomplete")._renderItem = function( ul, item ) {
                      return $("<li></li>")
                          .data( "item.autocomplete", item )
                          .append( "<a>" + (item.posterLink?"<img class='imdbImage' src='imdbImage.php?url=" + item.posterLink + "' />":"") + "<span class='imdbTitle'>" + item.movieName + "</span>" + "<div class='clear'></div></a>" )
                          .appendTo( ul );
                        };
                 } else 
                   if ($(this).val() == "byActor"){     
                            window.open("target.html","_blank","height=400,width=400, status=yes,toolbar=no,menubar=no,location=no");
                    }
            });
   });

$('#btnMove').on('click', function (d) {
       var selected = $("#q").val();
       if (selected.length == 0) {
            alert("Nothing to move.");
            d.preventDefault();
       }
       $('#selectedItems').append(new Option(selected));
       var title = new Option(selected);
       $("#q").val("");
       d.preventDefault();
   });      

</script>
</body>
</html>

and this is target.html:

<html>
<head>
  <link type="text/css" href="res/jquery-ui.css" rel="stylesheet" />
  <script type="text/javascript" src="res/jquery.min.js"></script>
  <script type="text/javascript" src="res/jquery-ui.min.js"></script>
  <script src="http://thecodeplayer.com/uploads/js/prefixfree-1.0.7.js"type="text/javascript"type="text/javascript"></script>
</head>

<body>
<form>
 <p>
  <input type="textbox" name= "tag" id="tags" placeholder="Enter an actor/actress name here" />
 </p>
 <p>
  <select id="movieName" name="movieName[]" multiple="multiple" width="200px" size="10px" style="display:none;">
  </select>
 </p>
  <script type="text/javascript">
  $(document).ready(function () {
    $("#tags").autocomplete({
     source: "actorsauto.php",
     minLength: 2,
     focus: function( event, ui ){
            event.preventDefault(); //This prevent the inserted text to be changed while moving in suggest list 
            return false;
            },
     select: function (event, ui){      
                    var selectedVal = ui.item.value;
                    $.post("actions.php", {q: selectedVal}, function (response){
                         console.log(response);                                       
                         $("#movieName").html(response).show();
                     });
             }
    }); 
  });
 </script>

</form>
</body>
</html> 

Could someone kindly inform me if there is any tutorial or sample that I can use for this purpose?

All ideas are highly appreciated,

Thanks


Solution

  • ok for the first part I would do the following:

    HTML:

    <html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
        <style>
            #basket{
                padding: 10px;
                border:1px solid #ccc;
            }
    
            #basket h3{
                padding: 0;
                margin: 0;
            }
    
        </style>
    </head>
    
    <body>
        <div class="movienames">
            <select id="selectType" name="source" style="font-size:15px; width:100px; height:30px;">
                <option value="">MoviesBy</option>
                <option value="byTitle">byTitle</option>
                <option value="byActor">byActor</option>
                <option value="byDirector">byDirector</option>
            </select>
    
            <div id="m_scents">
                <p>
                    <label style="margin-bottom:10px;" for="m_scnts"></label>
                    <input class="autofill4" type="textbox" name= "q27[]" id="q" style="display:none;" placeholder="Enter movie titles here" />
                    <!--<a href="#" id="addScnt4">Add more movies</a>-->
                    <input type="button" value=">> Add to selected list >>" id="btnMove" style="display:none;"/>
                    <input name="s" value="all" type="hidden"/>
                    <label style="margin-bottom:10px;" for="m_scnts"></label>
                </p>
            </div>
            <div id="basket">
                <h3>Basket</h3>
                <div id="basket_content">
                </div>
            </div>
    

    JS:

    var master_basket = new Array();
    
    $(document).ready(function() {
        $("#selectType").change(function() {
            if ($(this).val() == "byTitle") {
                $("#q").show();
                $("#btnMove").show();
                $("#q").focus();
                $("#q").autocomplete({
                    minLength: 0,
                    delay: 5,
                    source: "mona.php",
                    focus: function(event, ui) {
                        event.preventDefault(); //This prevent the inserted text to be changed while moving in suggest list 
                        return false;
                    },
                    select: function(event, ui) {
                        $(this).val(ui.item.movieName);
                        return false;
                    }
                }).data("uiAutocomplete")._renderItem = function(ul, item) {
                    return $("<li></li>")
                            .data("item.autocomplete", item)
                            .append("<a>" + (item.posterLink ? "<img class='imdbImage' src='imdbImage.php?url=" + item.posterLink + "' />" : "") + "<span class='imdbTitle'>" + item.movieName + "</span>" + "<div class='clear'></div></a>")
                            .appendTo(ul);
                };
            } else
            if ($(this).val() == "byActor") {
                window.open("target.html", "_blank", "height=400,width=400, status=yes,toolbar=no,menubar=no,location=no");
            }
        });
    });
    
    $('#btnMove').on('click', function(d) {
        d.preventDefault();
        var selected = $("#q").val();
        if (selected.length == 0) {
            alert("Nothing to move.");
            d.preventDefault();
        } else {
            addToBasket(selected);
        }
        $("#q").val("");
    });
    
    function addToBasket(item) {
        master_basket.push(item);
        showBasketObjects();
    }
    
    function showBasketObjects() {
        $("#basket_content").empty();
        $.each(master_basket, function(k, v) {
            $("#basket_content").append("<div>" + v + "</div>");
        });
    }