0

I want to access a javascript variable inside a php query

function myFunction2(){
    Total=parseInt(point2)
    <?php
    $con->query("UPDATE eventlist SET P2 = $this.Total WHERE Eid=$PID");           
    ?>
  }

I want the query to set p2=value of total

I understand that php is a serverside script and I cant do this like this. What is an alternative to this.?

EDIT

ok i got this on the JS side

function myFunction2(){
       var Total = parseInt(point1)+parseInt(point2);
         $.ajax({ url: 'ajax.php',
         data: {'total' : Total},
         type: 'post',
         dataType:'json',
         success: function(output) {
                      alert(output);
                  },
          error: function(request, status, error){
            alert("Error");
  }

and if i put

echo $_POST['total']

in ajax.php i get an alert with the value passed. So i think the value is being passed properly. But what i need to do is a Mysql Query.

$con->query("UPDATE eventlist SET P2 = $_POST['total']; WHERE Eid=1");  

Something like this. How do i do this

Arshak Anjum
  • 142
  • 1
  • 10
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – aynber May 23 '17 at 19:26
  • You can do it via ajax. – aynber May 23 '17 at 19:26
  • you can do your update in new page, call it via ajax on this page. – tech2017 May 23 '17 at 19:27
  • I think you need AJAX, this is a good link https://stackoverflow.com/questions/2338942/access-a-javascript-variable-from-php – Neetigya May 23 '17 at 19:29

2 Answers2

0

Try send javascript value to another php page which contain your query

function myFunction () {
    var param = "value";
    $.post('query.php', { postvalue: param}, function(data) {
        //do what you want with returned data
        //postvalue should be the name of post parameter in your query page
    })
}
Louay Hamada
  • 781
  • 1
  • 14
  • 22
0

Change your PHP in this way:

$total = $_POST['total'];
$con->query("UPDATE eventlist SET P2 = $total WHERE Eid=1"); 
quirimmo
  • 9,800
  • 3
  • 30
  • 45