Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  100] [ 6]  / answers: 1 / hits: 51231  / 10 Years ago, tue, september 16, 2014, 12:00:00

I have a JavaScript variable which I wan to pass along with an HTML link.



<script>
var demo=10;
</script>


I get the value of demo on executing one javascript function & few if-else & for loops. Since that code doesn't make any sense to this question, I haven't included that. Assume after all those operations, the final result that I get is stored in the demo variable. Now I wanna pass this demo variable along with an link.



<a href=to_fake_page.php>On click pass demo</a>


I am trying to pass the demo variable as a parameter to the href link. Is this ever possible???



I know



window.location.href = to_fake_page.php?demostore=demo;


would do the same.



UPDATE



This is my Javascript function



This function has one parameter & it is passed onto a_php_page.php where some database operations & condition checks are performed & the corresponding result is passed back from the PHP page to the AJAX function as JSON. I parse the JSON & obtain demo variable. And a HTML modal is included if I get response from the PHP page. Inside the HTML modal, I have a link pointing to_fake_page.php to where I have to pass the demo variable on clicking a link On click pass demo.



 function onefunction(parameter)
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange=function() //callback fn
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
// these values are obtained from a php page using AJAX.
// An array has been passed from the php page after json encoding it.

var jsonStr = JSON.parse(xmlhttp.responseText);
var demo=jsonStr.value1;
document.getElementById('myLink').href += '?demo=' + encodeURIComponent(demo);

$('<div class=modal fade>' +
'<div class=modal-dialog>' +
'<div class=modal-content>' +
demo+
'</div>' +
'<div class=modal-footer>' +
'<a href=to_fake_page.php id=myLink>On click pass demo</a>' +
'</div>' +
'</div>' +
'</div>' +
'</div>').modal();

}
}
xmlhttp.open(GET,a_php_page.php?from=+parameter,true);
xmlhttp.send();
}

More From » html

 Answers
92

To get the result I modified my JavaScript code like this:



<script>
var demo=10;
var urll='to_fake_page.php?demostore='+demo;
</script>


and changed the line



'<a href=to_fake_page.php id=myLink>On click pass demo</a>' + 


in AJAX function to



'<a href='+url+'' +
'id=myLink>On click pass demo</a>' +


It was simple as that. Added the variable urll to the DOM.


[#69448] Friday, September 12, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
suzanne

Total Points: 425
Total Questions: 87
Total Answers: 109

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
;