I need to able to click on list element and depending on which I am clicking, 2 variables will be displayed on the same page.
I have 2 variables which are defined in the html code ( this could be changed as I am hardcoding them variables into the html ):
<ul>
<li><a href="#" onclick="('6','67')">Text 1</a></li>
<li><a href="#" onclick="('22','240')">Text 2</a></li>
<li><a href="#" onclick="('34','56')">Text 3</a></li>
</ul>
Later I would like to pick up both variables with PHP $_POST like this:
<?php
echo $_POST['var1'];
echo $_POST['var2'];
?>
How can I achieve that?
You could do something like this:
<ul>
<li><a href="#" onclick="myFunction('6','67')">Text 1</a></li>
<li><a href="#" onclick="myFunction('22','240')">Text 2</a></li>
<li><a href="#" onclick="myFunction('34','56')">Text 3</a></li>
</ul>
<script>
function myFunction(a, b){
$.ajax(function(){
url: <yourURL>,
data: {'a': a, 'b': b }
success: function(){
//access the variables here.
}
});
}
</script>
use $.post()
html
<ul>
<li><a href="#" onclick="callFunction('6','67')">Text 1</a></li>
<li><a href="#" onclick="callFunction('22','240')">Text 2</a></li>
...
jquery
function callFunction(var1,var2){
$.post('path/to/your/php/page',{'data1':var1,'data2':var2},function(result){
alert('success');
})
}
and you can get the posted data in php by $_POST
php
$data1 = $_POST['data1'];
$data2 = $_POST['data2'];
echo $data1 " , " $data2;