开发者

To assign javascript value to a php variable in the same page

I have a page with php and javascript. In the php section i need to call a function with an argument $arg, Now $arg is the problem, $arg is actually value of a dropdown option in the same page . Here is what i am trying to accomplish.

 <?php include("aclass.php")?>
<script language="javascript">
function getoption(val){
}
</script>
<html>
 <select name="sel" onchange="getoption();">
   <option value="1">One</option>
   <option value="2">Two</option>   
</html>
<?php 
$object开发者_StackOverflow=new a;
$object->calculate($arg);
 ?>

// Here the issue is $arg should be the value i select in dropdown, $arg should be 1 if i select One and should be 2 if i select Two in dropdown.

" Is there a possibility to pass the javascript value 'val' to php variable $arg "


The thing you want to do is not possible without a form submission or without a contact to the server. PHP is a server side language whereas javascript is client side. So obviously you cannot do a server function in the client side.

So, either make this as a form and submit the form on change of the value in select or if you dont want reload of the page, try using ajax


You can use PHP variables in Javascript but the opposite can't be done. PHP runs server-side. Javascript is client-side. The only way to send something from Javascript to PHP is using AJAX. You might want to search in that direction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜