Announcement

Collapse
No announcement yet.

How send Javascript client var value to php server global variable

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How send Javascript client var value to php server global variable

    Hi all,
    I did a grid with an input field using the suggestions of Hiram (ref.How add an input field to a grid...)
    now I'm stucked becouse I'm not able to use the global variable available on client as JS variable (quantity) into a blank application that uses that same variable [glo_QuantityRow] to insert a record into mysql db.

    Grid has under onHeader

    echo '<script>
    function myfunction(id, name, supplier, category, company, price, quantity){

    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    console.log(xmlhttp.responseText);
    }
    }
    xmlhttp.open("POST",location.href,true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("id="+id+"&name="+name+"&supplier="+s upplier+"&category="+category+"&company="+company+ "&price="+price+"&quantity="+quantity.toString ());

    </script>';


    and under OnRecord

    if (isset($_POST['id'])){
    [glo_ProductID] = $_POST['id'];
    }
    if (isset($_POST['name'])){
    [glo_ProductName] = $_POST['name'];
    }
    if (isset($_POST['supplier'])){
    [glo_SupplierID] = $_POST['supplier'];
    }
    if (isset($_POST['category'])){
    [glo_CategoryID] = $_POST['category'];
    }
    if (isset($_POST['company'])){
    [glo_CompanyID] = $_POST['company'];
    }
    if (isset($_POST['price'])){
    [glo_UnitPrice] = $_POST['price'];
    }
    if (isset($_POST['quantity'])){
    [glo_QuantityRow] = $_POST['quantity'];
    }

    {col}= "<input type='text' size='6' onchange='myfunction(\"{ProductID}\",\"{ProductNam e}\",\"{SupplierID}\",\"{CategoryID}\",\"{CompanyI D}\",\"{UnitPrice}\", this.value)'>";

    ===============
    When I use the variable [glo_QuantityRow] within blank application it's always empty

    INSERT products_selected
    (ProductID, CustomerID, SalesmanID, ProductName, SupplierID, CategoryID, CompanyID, SelectedDate, Quantity, UnitPrice , ProductID_altern)
    VALUES
    ('[glo_ProductID]', '[glo_CustomerID]', '[glo_SalesmanID]' '[glo_ProductName]', '[glo_SupplierID]' ,
    '[glo_CategoryID]', '[glo_CompanyID]', '[order_date]' , [glo_QuantityRow], [glo_UnitPrice], '')
    ON DUPLICATE KEY UPDATE Quantity = Quantity + [glo_QuantityRow] ";

    and the echo is ....

    INSERT products_selected (ProductID, CustomerID, SalesmanID, ProductName, SupplierID, CategoryID, CompanyID, SelectedDate, Quantity, UnitPrice , ProductID_altern) VALUES ('013-3881-4', 'ALFKI', 'AGE01' 'CA - 560', 'F012' , 'SICLSYVA', 'Eos', '2015-01-17' , , 0.0000, '') ON DUPLICATE KEY UPDATE Quantity = Quantity +


    After '2015-01-17' you can see the Quantity value empty and also on very last Quantity = Quantity + is missing [glo_QuantityRow] value.

    Googling I founded this suggestion but I don't know how use it ... here;-(((


    <script type="text/javascript">
    function passvariable(var){
    window.location.href= 'filename.php?var='+var;
    }
    </script>

    <input type="button" value="submit" onclick="passvariable('<?php echo $phpvariable;?>');"/>

  • #2
    I'm not a javascript guru, but if you have created a custom field within the current form then this variable will be sent to your php application as all the form variables afaik. Thus I think that you can use $_GET or $_POST to retrieve the variable.
    Albert Drent
    aducom software netherlands
    scriptcase partner, reseller, support and (turn-key) development
    www.scriptcase.eu / www.scriptcase.nl

    Comment


    • #3
      Hi Albert, thanks
      the problem is that I need to add the input field for quantity into a "grid" not a form. That's a workaround using JS to accept a value in input.
      As you said grid not allowed input field as default and this is only way .

      $GET ... I have to use it into the blank application that makes the sql insert of selected row values?

      Another problem that for me is not so easy is about the price field on the grid. It's a select field that makes the search of that productID into the Price_List. If I use a select field seems that Javascript do not read it. Is it becouse it's a select field ??

      Comment


      • #4
        If you pass this info to a blank app, you don't need this globals. You can :

        PHP Code:
        $command $_GET['command'];

        switch(
        $command)
        {
            case 
        "insertProd":
                
        insertProd($_GET['id'], $_GET['name'], $_GET['supplier'], $_GET['quantity']); // fill all fields you need
                
        break;
        }

        function 
        insertProd($id$name$supplier$quantity){ //fill all fields you need
            
        $query "INSERT products_selected (ProductID,ProductName, SupplierID, quantity) VALUES ('.$id.', '.$name.', '.$supplier.', '.$quantity.') ON DUPLICATE KEY UPDATE Quantity = Quantity + "$quantity" ;";
                
        sc_exec_sql($query);

        On client side:

        Code:
        //Javascript
        function sendInsert(command, id, name, supplier, quantity){
        	$.get("../blankapp/blankapp.php",{command:"insertProd", id:id , name:name, supplier:supplier},function ()
        	{	});
        }
        Your {col}

        PHP Code:
        {col}= "<input type='text' size='6' onchange='sendInsert("inserProd", \"{ProductID}\",\"{ProductName}\",\"{SupplierID}\", this.value)'>"
        On my code are GET not POST just to not change my code. The difference is how params travel.

        Originally posted by Wikipedia
        In contrast, the HTTP GET request method is designed to retrieve information from the server. As part of a GET request, some data can be passed within the URI's query string, specifying for example search terms, date ranges, or other information that defines the query. As part of a POST request, an arbitrary amount of data of any type can be sent to the server in a request message body. A header field in the POST request usually indicates the message body's Internet media type.
        /Giuseppe

        Professional Scriptcase Services
        Some Customers opinions

        Comment


        • #5
          Hi Giu, first of all....thanks so much

          I try to tell what I'm doing:
          "hhh_grid_products_for_order" is the grid application with quantity({col}) field. Within grid I got a field Htlm image icon with a link to blank applic. that is "hhh_insert_selected"
          Blank application do only the sql insert.
          Now after your suggestions:

          GRID - hhh_grid_products_for_order

          OnHeader

          echo '<script>

          function sendInsert(command, id, name, supplier, category, company, price, quantity){
          $.get("../hhh_insert_selected/hhh_insert_selected.php",{command:"insertProd", id:id , name:name, supplier:supplier category:category, company:company, pricerice, quantity:quantity},function ()
          { });
          }
          </script>';

          OnRecord

          switch($command)
          {
          case "insertProd":
          insertProd($_GET['id'], $_GET['name'], $_GET['supplier'], $_GET['category'], $_GET['company'] , $_GET['price'], $_GET['quantity']);
          break;
          }


          {col}= "<input type='text' size='6' onchange='sendInsert("inserProd", \"{ProductID}\",\"{ProductName}\",\"{SupplierID}\" , \"{CategoryID}\",\"{CompanyID}\",\"{UnitPrice}\ ", this.value)'>";

          NOW,
          I'm bit confused here becouse you have inserted also the lines of blank code here. Have I to use it here ? What the blank application does if we make the insert here ? Sorry for stupid question
          but for me is really complex to understand ... all these steps


          function insertProd($id, $name, $supplier, $category, $company, $price, $quantity){
          $query = "INSERT products_selected (ProductID,ProductName, SupplierID, quantity) VALUES ('.$id.', '.$name.', '.$supplier.', '.$category.', '.$company.', '.$price.', '.$quantity.') ON DUPLICATE KEY UPDATE Quantity = Quantity + ". $quantity. " ;";
          sc_exec_sql($query);
          }

          Comment


          • #6
            PHP code I wrote, is for blank app onExecute, not on grid.

            This goes on blank app, not on grid. On grid you just need to build the JS link to call this blank app.

            PHP Code:
            $command $_GET['command'];

            switch(
            $command)
            {
                case 
            "insertProd":
                    
            insertProd($_GET['id'], $_GET['name'], $_GET['supplier'], $_GET['quantity']); // fill all fields you need
                    
            break;
            }

            function 
            insertProd($id$name$supplier$quantity){ //fill all fields you need
                
            $query "INSERT products_selected (ProductID,ProductName, SupplierID, quantity) VALUES ('.$id.', '.$name.', '.$supplier.', '.$quantity.') ON DUPLICATE KEY UPDATE Quantity = Quantity + "$quantity" ;";
                    
            sc_exec_sql($query);

            onRecord you just need to build the JS command:
            Code:
            {col}= "<input type='text' size='6' onchange='sendInsert("inserProd", \"{ProductID}\",\"{ProductName}\",\"{SupplierID}\" , \"{CategoryID}\",\"{CompanyID}\",\"{UnitPrice}\ ", this.value)'>";
            and onHeader you provided to the app the JS function itself
            Code:
            echo '<script>
            
            function sendInsert(command, id, name, supplier, category, company, price, quantity){
            $.get("../hhh_insert_selected/hhh_insert_selected.php",{command:"insertProd", id:id , name:name, supplier:supplier category:category, company:company, pricerice, quantity:quantity},function ()
            { });
            }
            </script>';
            The idea behind this is, you call the blank app with params needed to create the record you need, for this reason on Grid you just need the JS part.

            WARNING I didn't revised the code. Could be typos or something, I copy/pasted from my code and changed it to fits your columns.
            /Giuseppe

            Professional Scriptcase Services
            Some Customers opinions

            Comment


            • #7
              Thanks again to all...
              Some good news.. ;-))

              This is something working...

              OnHeader
              echo '<script>
              function myfunction(id, name, supplier, category, company, price, quantity, row){

              if (row.value==''){
              alert('Please insert a quantity!');
              return;
              }

              if (isNaN(row.value)){
              alert('Please insert a number');
              row.value= '';
              return;
              }

              if (price == 0){
              alert('Attention you do not have a price for this product. Please make a RFQ');
              return;
              }




              var xmlhttp;
              if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
              else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
              xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
              console.log(xmlhttp.responseText);
              }
              }
              xmlhttp.open("POST",location.href,true);
              xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
              xmlhttp.send("id="+id+"&name="+name+"&supplier="+s upplier+"&category="+category+"&company="+company+ "&price="+price+"&quantity="+quantity.toString ());

              }
              </script>';


              OnRecord
              if (isset($_POST['id'])){

              session_start();
              $_SESSION['product'] = $_POST;
              }

              {col}= "<input type='text' size='6' onchange='myfunction(\"{ProductID}\",\"{ProductNam e}\",\"{SupplierID}\",\"{CategoryID}\",\"{CompanyI D}\",\"{UnitPrice}\", this.value , this)'>";


              $sql_product_price="SELECT UnitPrice FROM price_list
              WHERE CustomerID = '[glo_CustomerID]' AND ProductID = '{ProductID}'
              ORDER BY UnitPrice";

              sc_lookup(rs0, $sql_product_price);

              if (isset({rs0[0][0]})) // Row found
              {
              {UnitPrice} = {rs0[0][0]};
              }
              else // No row found
              {
              {UnitPrice} = 0 ;
              }


              Blank
              [order_date] = date('Y-m-d');

              //print_r($_SESSION['product']);

              $product = $_SESSION['product'];
              $id = $product['id'];
              $name = $product['name'];
              $supplier = $product['supplier'];
              $category = $product['category'];
              $company = $product['company'];
              $price = $product['price'];
              $quantity = $product['quantity'];
              insertProd($id, $name, $supplier, $category, $company, $price, $quantity);

              function insertProd($id, $name, $supplier, $category, $company, $price, $quantity){
              $query = "INSERT products_selected (ProductID, CustomerID, SalesmanID, ProductName, SupplierID, CategoryID, CompanyID, SelectedDate, Quantity, UnitPrice , ProductID_altern) VALUES ('".$id."', '[glo_CustomerID]', '[glo_SalesmanID]', '".$name."', '".$supplier."', '".$category."' , '".$company."' , '[order_date]', '".$quantity."','".$price."' , '') ON DUPLICATE KEY UPDATE Quantity = Quantity + ". $quantity. " ;";

              echo $query;
              // sc_exec_sql($query);
              }
              Last edited by giovannino; 01-18-2015, 03:47 PM.

              Comment


              • #8
                I got, I hope... last problem to solve.
                Everything works nice now but when I navigate throught the pages using next page or page number it appears an Output window very like to that one that echo does ..
                If I delete all the code within OnHeader it disappears so I think the problem is within this event.
                The code is here down and for me.... it's like ... arab !!! I got problems with php yet ...;-) with JS it's a nightmare !!!
                Is there something that can generate this strange output ??? Only when page is changed. Thanks

                echo '<script>
                function myfunction(id, name, supplier, category, company, price, quantity, row){


                var xmlhttp;
                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
                }
                else
                {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                console.log(xmlhttp.responseText);
                }
                }
                xmlhttp.open("POST",location.href,true);
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xmlhttp.send("id="+id+"&name="+name+"&supplier="+s upplier+"&category="+category+"&company="+company+ "&price="+price+"&quantity="+quantity.toString ());


                }

                </script>';
                Attached Files
                Last edited by giovannino; 01-24-2015, 05:57 AM.

                Comment

                Working...
                X