1

I have a page called getvalues.php, I need to call a function which is written in a different php page(fileclass.php) in a class from this page(getvalues.php), but the issue is i need to pass a variable also which is $i, and the value of $i passed should be 1 if we are selecting option B, and $i=2 if option=c, and $i=3 if option=D given in dropdown. I had simply wiritten an onchange event but had not written any code in javascript. Please help Thanks. Here is the code for getvalues.php

 <html>
 <select id="s" onchange="callsome();">
    <option value='B' selected>B</option>
    <option value='c'>c</option>
    <option value='D'>D</option>
 </select></html>
 <?php include("fileclass.php")
      $obj=new file;
      echo $obj->func($i);?>
1
  • Please remember that all PHP code is executed before the HTML/Javascript, so you cannot use Javascript or HTML values as input to the PHP within the same script. Also, please update your post with a specific question so we can know where you are having trouble. Commented Jul 5, 2011 at 20:15

2 Answers 2

2

You could implement this using JQuery or Javascript (I use JQuery in the example because it is shorter and easier to make Ajax calls) :

<html>
   <select id="s" onchange="callsome();">
       <option value='1' selected="selected">B<option>
       <option value='2'>C</option>
       <option value='3'>D</option>
   </select>

   <script>
       function callsome() {
           var selected = $('select#s option:selected').val();
           $.ajax({
               type: "POST",
               url: "fileclass.php",
               data: ({selectedvalue : selected}),
               success: function(data) {
                  alert(data);
               }
           });
       }
   </script>
</html>

After that the callsome returns the output of the fileclass.php script and you can use that however you like in your code. From your explanation it was not really clear what is happening in fileclass.php and what you want to do with it, so I hope it helps you.

If you want the function in Javascript only:

<script type="text/javascript">
function callsome() {
     var e = document.getElementById("s");
     var strVal = e.options[e.selectedIndex].value;

     var xmlhttp;
     if (window.XMLHttpRequest) {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
     }
     else {
          // code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4 && xmlhttp.status==200) {
               var data = xmlhttp.responseText;
               //use the data as you wish
          }
     }
     xmlhttp.open("GET","fileclass.php?selectedvalue=strVal",true);
     xmlhttp.send();
}
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

is there a possibility to call the function func inside javascript
Basically this is all javascript. But if you want you can just paste the function callsome() { ... } into a Javascript file and include it. It will work too, yes. The reason I just put it between script tags in the body is to make it easy to read. :)
I added the code in Javascript only, in case you do not want to make use of JQuery.
2

This isn't the way PHP and HTML work.

PHP is rendered on the server. HTML is rendered on the client, after the PHP is completely done. To do what you want to do, you need to have the HTML (possibly Javascript) make a request to the PHP page at fileclass.php.

1 Comment

is there anyway to change the value of $i passed depending on option selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.