Is there any way to pass parameter by just clicking on a link, in condition of link does not handle the parameter for example
<a href='example.php?id=1'>M</a>
I don't want to use the ?id=1
is there a way to do this? I want the parameter to be passed to next page without form and link parameter when clicking on a link.
So I can handle the parameter on the next page, not on the same page.
Is there any way to pass parameter by just clicking on a link, in condition of link does not handle the parameter for example
<a href='example.php?id=1'>M</a>
I don't want to use the ?id=1
is there a way to do this? I want the parameter to be passed to next page without form and link parameter when clicking on a link.
So I can handle the parameter on the next page, not on the same page.
Share Improve this question edited Sep 13, 2017 at 8:24 always-a-learner 3,79410 gold badges45 silver badges88 bronze badges asked Sep 1, 2017 at 2:59 SilvarCrawSilvarCraw 791 gold badge2 silver badges10 bronze badges3 Answers
Reset to default 3Example 1
You can use a data-*
parameter and Ajax for it.
<a class="example_anchor" href='example.php?' data-id="1" >M</a>
<script>
$(".example_anchor").on("click", function (e) {
e.preventDefault();
var id = $(this).data('id');
$.ajax({
url: your_url,
type: "POST",
dataType: "json",
data: { id: id },
error: function (msg) {
// Error handling
},
success: function (msg) {
// Success handling
if (msg == true) {
alert("You will now be redirected.");
window.location = "//your_redirect_link/";
}
}
});
});
</script>
Example 2
You can use Function in onclick
of tag <a>
passing parameters
<a href='#' onclick="yourfunction(1)">M</a>
<script>
function yourfunction(id1)
{
alert(id1);
}
</script>
JS Fiddle Example
Easiest - you could create a form with a hidden input and send it on click on that link. Here is the code (using jQuery):
$('.submit-form').click(function(){
//add the value to be sent to the input in the form
$('#link-extra-info input').val( $(this).data('submit') );
//the href in the link bees the action of the form
$('#link-extra-info').attr('action', $(this).attr('href'));
//submit the form
$('#link-extra-info').submit();
//return false to cancel the normal action for the click event
return false;
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="link-extra-info">
<input type="hidden" name="p" value="0">
</form>
<a href="page.html" class="submit-form" data-submit="1">Link text 1</a>
<a href="page2.html" class="submit-form" data-submit="2">Link text 2</a>
Potentially creative solution: use a cookie. HTML would be:
<a href="link.html" onclick="passVariable(this)">Link</a>
With some JS
<script>
function passVariable(url) {
document.cookie = "id=1";
window.location.href = this;
}
</script>
Then on the page you're navigating to, use some JS to retrieve the cookie:
<script>
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
</script>
Could do an AJAX call to get associated data after parsing the cookie value.