Baran Topal

Baran Topal


May 2024
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


passing js result to php

baranbaran

Long ago again, I needed to pass js result to php.

I hate hidden fields as they are not easy to spot if the code file is gigantic. Yet, i must say one way is to use an hidden field in the form to put the result:

<script type="text/javascript">
function sayHello(str, n) { // replace by your own function
 return str+n;
}

window.onload = function() {
 document.getElementsByTagName("form")[0].onsubmit = function() {
 document.getElementById("parameterFromPrevPage").value = sayHello('jazz', 12);
 }
}
</script>
<form method="POST" action="catch.php">
<input type="hidden" name="parameterFromPrevPage" />

Another way is:

<form method = "Post" action = "catch.php" onsubmit="this.action=this.action+sayHello('jazz', 12);"