Javascript function post and call php script
Solution 1:
EDIT 2018
Yeah, I am still alive. You can use the fetch
API instead of jQuery
. It is widely supported except (guess who?...) IE 11 and below but there is a polyfill for that. Enjoy modern coding.
Support for fetch API
OLD ANSWER
You will have to use AJAX.
Javascript alone cannot reach a php script. You will have to make a request, pass the variable to PHP, evaluate it and return a result. If you'are using jQuery sending an ajax request is fairly simple:
$.ajax({
data: 'orderid=' + your_order_id,
url: 'url_where_php_is_located.php',
method: 'POST', // or GET
success: function(msg) {
alert(msg);
}
});
and your php script should get the order id like:
echo $_POST['orderid'];
The output will return as a string to the success function.
EDIT
You can also use the shorthand functions:
$.get('target_url', { key: 'value1', key2: 'value2' }).done(function(data) {
alert(data);
});
// or eventually $.post instead of $.get
Solution 2:
Assuming you don't want to use AJAX, you can do something like this in your clickedbutton
function:
window.location.replace('path/to/page.php?orderid=' + orderid);
and then in your page.php
"...where OrderID = '" . $_GET('orderid') . "'";
(note the dots to join strings)
Solution 3:
By using Ajax.
function clickedbutton(buttonid,orderid){
$.post("page.php", { buttonid: buttonid })
.done(function(data) {
alert("Data Loaded: " + data);
});
}
In php you get it with $_POST.
//[..] previous php code
$strSQL = "update orders set OrderStatus = 'In Progress' where OrderID = '" + $_POST['buttonid'] + "'";
//[..] rest of php code
Watch out for SQL injection. Don't take this advice as written.