0

My problem is my product's only one size is inserting. But i am wanting which size i will select it should insert. I am new ajax. I think the problem is in ajax so Please guys help me out. enter image description here

      <ul id="MOTForm" class="size" >
                <h3>Length</h3>
                <input class="single-checkbox"  type="checkbox" id="sizeid[]" name="sizeid[]" value="M"> <label> M </label> 
                <input class="single-checkbox"  type="checkbox" id="sizeid[]" name="sizeid[]" value="S">  <label> S </label>
                <input class="single-checkbox"  type="checkbox" id="sizeid[]" name="sizeid[]" value="L"> <label> L  </label> 
                <input class="single-checkbox"  type="checkbox" id="sizeid[]" name="sizeid[]" value="XS"> <label> XS </label>
                <input class="single-checkbox"  type="checkbox" id="sizeid[]" name="sizeid[]" value="XL"> <label> XL </label>
                <input class="single-checkbox"  type="checkbox" id="sizeid[]" name="sizeid[]" value="XXL"> <label> XXL </label>
                <input class="single-checkbox"  type="checkbox" id="sizeid[]" name="sizeid[]" value="XXXL"> <label> XXXL </label>

                </ul>




                /*ADD TO TEMP CARD*/
                function add_temp_card(pro_id, pro_price, pro_name)
                {       



                    var quantity = document.getElementById("quantity").value;
                    var sizeid = document.getElementById("sizeid[]").value;

                    if(quantity == "") { alert("Please Enter Quantity. ");                  quantity.focus();       return false; }
                    if(quantity < 1) { alert("Please Enter Quantity Minimum 1 yrd. ");      quantity.focus();       return false; }

                    var xmlRequest = GetXmlHttpObject();
                    if (xmlRequest == null)
                    return;         

                        var url = "add_temp_card.php?quantity="+quantity+"&pro_id="+pro_id+"&pro_price="+pro_price+"&sizeid="+sizeid;
                        var browser=navigator.appName;
                        if (browser=="Microsoft Internet Explorer")
                        {
                            xmlRequest.open("POST",url, true);
                        }
                        else
                        {
                            xmlRequest.open("GET",url, true);
                        }

                        xmlRequest.setRequestHeader("Content-Type", "application/x-www-formurlencoded");
                        xmlRequest.onreadystatechange =function()
                        {
                            if(xmlRequest.readyState==4)
                            {
                                HandleAjaxResponse_add_temp_card(xmlRequest, pro_name);
                            }
                        };
                            xmlRequest.send(null);
                            return false; 
                } 
                function HandleAjaxResponse_add_temp_card(xmlRequest, pro_name)
                {
                    var xmlT=xmlRequest.responseText;
                    var alertmessage = pro_name + " Added to cart.";
                    //alert(alertmessage);
                    location.replace("check_out.php");  
                    document.getElementById("add_temp_card").innerHTML=xmlT;
                    return false;
                }


                            <?php
                            session_start();
                            require_once("webcontrol/connect_db.php");

                            $quantity       = $_REQUEST['quantity'];
                            $pro_id         = $_REQUEST['pro_id'];
                            $pro_price      = $_REQUEST['pro_price'];
                             $size          = $_REQUEST['sizeid'];


                            $date_time      = date("F j, Y, g:i a");

                            $uniq_id        = $_SESSION['uniq_id']; 
                            if(!$uniq_id)
                            {
                            $uniq_id             = time();
                            $_SESSION['uniq_id'] = $uniq_id;

                            $q1 = mysql_query("insert into temp_order values('', '', '$uniq_id', '$date_time')");
                            }

                            $q2 = mysql_query("select product_qty from temp_details where temp_id = '$uniq_id' and product_id = '$pro_id'");
                            $r2 = mysql_fetch_array($q2);
                            $product_qty = $r2[0];

                            if(!$product_qty)
                            {   
                            $q3 = mysql_query("insert into temp_details values('$uniq_id', '', '$size', '$pro_id', '$quantity', '$pro_price')");    
                            } else {
                            $new_product_qty = $product_qty + $quantity;
                            $q3 = mysql_query("update temp_details set product_qty = '$new_product_qty' where temp_id = '$uniq_id' and product_id = '$pro_id' ");
                            }
                            ?>

I will be thanked full for help.

Sumonto
  • 11
  • 1
  • 7
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky May 30 '17 at 14:58
  • Don't use the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky May 30 '17 at 14:58
  • OKay i will do @alex – Sumonto May 30 '17 at 15:02

2 Answers2

0

document.getElementById("sizeid[]") Give one element back, futhermore the id's should be unique in your document.

You can use getElementByTagName or getElementByClassName insteat, but than You should loop over the elements.

You can also switch to radiobutton, or use an select Tag for your size

Thomas
  • 1,058
  • 8
  • 15
0
<ul id="MOTForm" class="size" >
            <h3>Length</h3>
            <input class="single-checkbox"  type="checkbox" id="sizeid_0" name="sizeid[]" value="M"> <label> M </label> 
            <input class="single-checkbox"  type="checkbox" id="sizeid_1" name="sizeid[]" value="S">  <label> S </label>
            <input class="single-checkbox"  type="checkbox" id="sizeid_2" name="sizeid[]" value="L"> <label> L  </label> 
            <input class="single-checkbox"  type="checkbox" id="sizeid_3" name="sizeid[]" value="XS"> <label> XS </label>
            <input class="single-checkbox"  type="checkbox" id="sizeid_4" name="sizeid[]" value="XL"> <label> XL </label>
            <input class="single-checkbox"  type="checkbox" id="sizeid_5" name="sizeid[]" value="XXL"> <label> XXL </label>
            <input class="single-checkbox"  type="checkbox" id="sizeid_6" name="sizeid[]" value="XXXL"> <label> XXXL </label>

            </ul>




            /*ADD TO TEMP CARD*/
            function add_temp_card(pro_id, pro_price, pro_name)
            {       



                var quantity = document.getElementById("quantity").value;
                var sizeid = "";
                var elements=document.getElementsByClassName("single-checkbox");

        for( var i=0; i<elements.length; i++ )
        {
            var element=elements[i];
            if(element.checked)
            {
                sizeid=element.value;
                break;
            }
        }

                if(quantity == "") { alert("Please Enter Quantity. ");                  quantity.focus();       return false; }
                if(quantity < 1) { alert("Please Enter Quantity Minimum 1 yrd. ");      quantity.focus();       return false; }

                var xmlRequest = GetXmlHttpObject();
                if (xmlRequest == null)
                return;         

                    var url = "add_temp_card.php?quantity="+quantity+"&pro_id="+pro_id+"&pro_price="+pro_price+"&sizeid="+sizeid;
                    var browser=navigator.appName;
                    if (browser=="Microsoft Internet Explorer")
                    {
                        xmlRequest.open("POST",url, true);
                    }
                    else
                    {
                        xmlRequest.open("GET",url, true);
                    }

                    xmlRequest.setRequestHeader("Content-Type", "application/x-www-formurlencoded");
                    xmlRequest.onreadystatechange =function()
                    {
                        if(xmlRequest.readyState==4)
                        {
                            HandleAjaxResponse_add_temp_card(xmlRequest, pro_name);
                        }
                    };
                        xmlRequest.send(null);
                        return false; 
            } 
            function HandleAjaxResponse_add_temp_card(xmlRequest, pro_name)
            {
                var xmlT=xmlRequest.responseText;
                var alertmessage = pro_name + " Added to cart.";
                //alert(alertmessage);
                location.replace("check_out.php");  
                document.getElementById("add_temp_card").innerHTML=xmlT;
                return false;
            }


                        <?php
                        session_start();
                        require_once("webcontrol/connect_db.php");

                        $quantity       = $_REQUEST['quantity'];
                        $pro_id         = $_REQUEST['pro_id'];
                        $pro_price      = $_REQUEST['pro_price'];
                         $size          = $_REQUEST['sizeid'];


                        $date_time      = date("F j, Y, g:i a");

                        $uniq_id        = $_SESSION['uniq_id']; 
                        if(!$uniq_id)
                        {
                        $uniq_id             = time();
                        $_SESSION['uniq_id'] = $uniq_id;

                        $q1 = mysql_query("insert into temp_order values('', '', '$uniq_id', '$date_time')");
                        }

                        $q2 = mysql_query("select product_qty from temp_details where temp_id = '$uniq_id' and product_id = '$pro_id'");
                        $r2 = mysql_fetch_array($q2);
                        $product_qty = $r2[0];

                        if(!$product_qty)
                        {   
                        $q3 = mysql_query("insert into temp_details values('$uniq_id', '', '$size', '$pro_id', '$quantity', '$pro_price')");    
                        } else {
                        $new_product_qty = $product_qty + $quantity;
                        $q3 = mysql_query("update temp_details set product_qty = '$new_product_qty' where temp_id = '$uniq_id' and product_id = '$pro_id' ");
                        }
                        ?>
Thomas
  • 1,058
  • 8
  • 15