0

I am using the following php code with a form which updates a table. However I want to add a javascript variable to the sql so that the variable will be added to a column in the database. The variable is in a different file to the php.

php :=

$name = $_POST['firstname'];
 $lastname = $_POST['lastname'];
 $userAddress = $_POST['address'];
 $userPostCode = $_POST['postcode'];
 $delivery = $_POST['deliverytype'];

 $sql = "INSERT INTO USERS (FIRSTNAME, SECONDNAME, ADDRESS, POST_CODE, DELIVERY_TYPE) VALUES ('$name', '$lastname', '$userAddress', '$userPostCode', '$delivery') ";
 $conn->exec($sql);

i then want to add to that a totalcost variable from the following javascript that will go in Total_Order_Cost and $totalCost

here is the js function that i wish to take the variable totalPrice from

function displayBasket(){

  basket = document.getElementById("basket"); 
  string = "";
  var basketStorage = localStorage.getItem("basket"); 
  jsonBasket = JSON.parse(basketStorage); 

    var totalPrice = 0;
    itemTotal = 0;
  for (var property in jsonBasket){ 

    var qPrice = jsonBasket[property ].quantity * jsonBasket[property ].cost;
    var total = jsonBasket[property ].quantity;
    string += "<section id='basketPageSection'>";
    if(jsonBasket.hasOwnProperty(property )){ 
      string += "<p>Item: " + jsonBasket[property ].name + "</p>"; 
      string += "<p>Price: £" + jsonBasket[property ].cost + "</p>"; 
      string += "<p>Quantity: " + jsonBasket[property ].quantity + "</p>"; 

    }

    totalPrice += qPrice;
    itemTotal += total;
    string += "</section>";

  }

  string += "<section id='basketSection'> <h3> Total Cost: £" + parseFloat(totalPrice).toFixed(2) + "</h3></section>"

  basket.innerHTML = string; 
  displayQuant();
}
1
  • Notice: Your code is vulnerable to SQL Injection. Have a look at Prepared Statements. Commented Apr 16, 2015 at 19:57

2 Answers 2

2

You could use a hidden input field change it value attribute and submit along with rest of the form.

<input type="hidden" name="calc" id="calc" value="">

Use Jquery to set its value

$('#calc').val(someval);

Update Javascript

document.getElementById("calc").value="Your Value";
Sign up to request clarification or add additional context in comments.

4 Comments

where do i add this to my code? To the form? I am new o JQuery
You could use plain javascript as well let me update my answer
okay, i see what you are doing, where do i put the form though? with my forms in the form file? @AAB
you mean the input field rite Yes it should be placed inside the form tag you wish to submit
0

You can use ajax.

It is the best solution for me :

Use a code like that in your javascript :

value1= ... ; // firstname
value2= ... ; // lastname    
$.ajax({
        type: 'post',
        url: 'post.php',
        data: 'firstname='+value1+'&lastname='+value2+'&address= ....,
        success: function () {
          alert('data sent');
        }
      });

Another Way

$.ajax({
            type: 'post',
            url: 'post.php',
            data: $('#formID').serialize()+"&extraval=1&extraval2=2...",
            success: function () {
              alert('data sent');
            }
          });

Comments

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.