473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

calling a php file onchange event

I am trying to call a php file...on an onChange event of the drop down box....

The php file takes a few parameters....and creates an array....

how can i access the values?
Mar 4 '10 #1
8 11230
zorgi
431 Expert 256MB
From what you are saying is hard to work out what is it that you are trying to do. Is there some code?
Mar 4 '10 #2
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <script type="text/javascript">
  5.  
  6. function callPHPScript(url, val) {
  7.     var w = document.refererForm.refererList.selectedIndex;
  8.     alert(w);
  9.     var selected_text = document.refererForm.refererList.options[w].text;
  10.     alert(selected_text);
  11.     window.location=url+"?"+val;
  12. }
  13. </script>
  14. </head>
  15.  
  16. <table class=tableBorder border="0" cellpadding="0" cellspacing="0" width="100%">
  17. <tr>
  18. <td>
  19. <table border="0" cellpadding="3" cellspacing="1" width="100%">
  20.     <tr class=rowHeader>
  21.         <td class="colHeaderLink" width="100%" colspan="3">XYZ <b><a href="xxxx.jspa?id=$project.getLong('id')">$project.getString('name')</a></b></td>
  22.     </tr>
  23.     <tr class=refererHeader width="100%">
  24.     <td class="colHeaderLink" colspan="1">Referrer:</td>
  25.     <td class="colHeaderLink" colspan="2">
  26.     <form name="refererForm">
  27.     <select name="refererList" onChange="callPHPScript('http://localhost:9999/xxxx.php', 'projectId=123&referer=abc')">
  28.         <option value="1">All</option>
  29.         #if ($DbReferrers)
  30.             #set ($row = 2)
  31.             #foreach ($value in $DbReferrers)
  32.                 <option value="$row">$value.get(0)</option>
  33.                 $row = $row + 1
  34.             #end
  35.         #end
  36.     </select>
  37.     </form>
  38.     </td>
  39.     </tr>
  40. </table>
  41. </td>
  42. </tr>
  43. </table>
  44. </html>
-------------------
The php code looks some this like this....

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. function executeSQLQuery($sql) {
  4.     $result = mysql_query($sql);
  5.     if (!$result) {
  6.         die("Query to show fields from table failed");
  7.     }
  8.     $value = mysql_fetch_row($result);
  9.     mysql_free_result($result);
  10.     return ($value[0]);
  11. }
  12.  
  13. $db_host   = 'localhost';
  14. $db_user   = 'user';
  15. $db_pwd    = 'password';
  16. $database  = 'db';
  17. $projectId = $_GET['projectId'];
  18. $referer   = $_GET['referer'];
  19.  
  20. if (!mysql_connect($db_host, $db_user, $db_pwd))
  21.     die("Can't connect to database");
  22.  
  23. if (!mysql_select_db($database))
  24.     die("Can't select database");
  25.  
  26. // Construct all the SQL queries requierd
  27. // that need to be executed for getting the
  28. // values from the database
  29.  
  30. //constructs queries...based on passed params...
  31. //cals the execute funcion...
  32. $table = array();
  33. $table["key"] = $value;

I want to use this array in my javascript....


If you see in the javascript i have used the window.location which will cause to redirect the browser...but i dont want to get redirected...i want to execute...the php code...get the array in to javascript and then display using the javascript...

Can some one please help me if possible with a few small examples...pls
Mar 5 '10 #3
zorgi
431 Expert 256MB
Hm not that clear at all ... You want javascript to execute php code? Ajax would be good way to do that. Redirection is ok too.
Mar 5 '10 #4
To elaborate on zorgi's answer, Ajax is probably what you're looking for.

If you're unfamiliar, please read about it here:
http://www.w3schools.com/Ajax/ajax_intro.asp

Ajax is actually built on JavaScript and it gives one the ability to retrieve data through a GET or POST request without refreshing a page.

Your current configuration reloads the page while passing some GET data. This is repeated each time the target drop-down menu item is changed and this event is triggered through an event handler. The specified PHP script parses the GET data and retrieves information accordingly. From there, the script must redirect the user to another page for displaying output or dump results from the script itself.

Your method is on the brink of simulating an Ajax request.

Your question, "how can I access the values," is a little unclear. If you wish to access them, you can use PHP to output HTML along with your values by looping through the $table array or by specifying any of the $table elements individually. I assume that you already know that, so your question might be, then, "how can I access the values in JavaScript?"

Well, after you've read the article referenced above about Ajax, you will have learned that your PHP script's output data is returned to your page and stored in the req.responseText property. The process might look something like this:



In PHP, if you execute this code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. print $array;
  3. ?>
You get an output that looks like this:
Expand|Select|Wrap|Line Numbers
  1. Array
So, the data returned to your page from the Ajax request is actually a string value. If you were to execute this code in your javascript:
Expand|Select|Wrap|Line Numbers
  1. alert(req.responseText);
  2.  
Then you would get a dialog box that says, "Array." This is obviously not what we want. What we must do, then, is not simply use the PRINT command in PHP, but rather dump the data in a format that can be parsed by JavaScript.

For example, if your $table array looked like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $table = array( 'key' => 'value' , 'anotherKey' => 'anotherValue' );
  3. ?>
Then you could output the data like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. foreach($table as $key=>$val) {
  3.   echo "$key=$val\n";
  4. }
  5. ?>
Which will produce the following output:
Expand|Select|Wrap|Line Numbers
  1. key=value
  2. anotherKey=anotherValue
This data will then be returned to your page and is accessed through the req.responseText property. You will have to write some JavaScript code to parse this data.

From here you're on your own. I hope this helps, but please ask if you have more questions!
Mar 10 '10 #5
If I understand correctly use json_encode on the array in PHP then use JavaScript's eval function

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $my_ary = array( "lol", "rtm", "dry" );
  4.  
  5. ?>
  6. <script>
  7. var my_ary = eval( "<?php echo json_encode( $my_ary ); ?>" );
  8. </script>
  9.  
As for the JavaScript executing PHP code try Sajax PHP or write your own. Pretty simple really just check for the a post variable to be set if it is handle the request correctly and send the data as json back to JavaScript and there you have interaction between PHP and JavaScript. Also if you do it your self make sure you have some sort of security such as a ACL to prevent the user from calling your PHP functions "javascript: callPHP( 'deleteFile', 'my_important_file' )";

Hope that helps.
Mar 11 '10 #6
I was trying to create student details in php,i want dropdown like if i select MBA course means only $ sem must be diplay in same page,same as if i select Bsc means it must show ^ sems .default it must show on the bases of courses
Sep 22 '10 #7
i was tryied in java script but it showing error
Sep 22 '10 #8
Expand|Select|Wrap|Line Numbers
  1. <html><head>
  2. <SCRIPT language=Javascript>
  3.  
  4.       function isNumberKey(evt)
  5.       {
  6.          var charCode = (evt.which) ? evt.which : event.keyCode
  7.          if (charCode > 31 && (charCode < 48 || charCode > 57))
  8.             return false;
  9.  
  10.          return true;
  11.       }
  12.       function rld()
  13.       {
  14.       document.frm.action="stud5.php";
  15.       document.frm.submit();
  16.       }
  17.  
  18.       </SCRIPT>
  19.    </head>
  20. <body>
  21. <FORM method=post name='frm'>
  22. <h1><font color="blue">Student details</font><h1>
  23.  
  24. <TABLE BORDER=10>
  25. <TR>
  26.     <TD>Student Name</TD>
  27.     <TD>
  28.     <INPUT type=text name="StudentName">
  29.     </TD>
  30. </TR>
  31.  
  32. <TR>
  33.     <TD>Father Name</TD>
  34.     <TD>
  35.     <INPUT type=text name="Father Name">
  36.     </TD>
  37. </TR>
  38. <TR>
  39.     <TD>course</TD>
  40.     <td>
  41.     <?php
  42.     $S1="";
  43.     $S2="";
  44.     $S3="";
  45.     $S4="";
  46.     if($course=='MBA')
  47.         $S1="selected";
  48.     elseif($course=='MCA')
  49.         $S2="selected";
  50.     elseif($course=='Bsc')
  51.         $S3="selected";
  52.     elseif($course=='Bcom')
  53.         $S4="selected";
  54.     ?>
  55.  
  56.     <SELECT name="course" onchange="rld()">
  57.         <OPTION VALUE="">Select</OPTION>
  58.         <OPTION VALUE="MBA"> MBA </OPTION>
  59.         <OPTION VALUE="MCA"> MCA </OPTION>
  60.         <OPTION VALUE="Bsc">Bsc </OPTION>
  61.         <OPTION VALUE="Bcom">Bcom </OPTION>
  62.         </select>
  63.     </TD>
  64.     <TR>
  65.     <TD>Gender</TD>
  66.     <TD>
  67.     Male: <INPUT type="radio" name="gender" value="M">
  68.     Female: <INPUT type="radio" name="gender" value="F">
  69.     </TD>
  70. </TR>
  71. <TR>
  72.     <TD>Address1</TD>
  73.     <TD>
  74.     <INPUT type="text" name="Address1" >
  75.     </TD>
  76. </TR>
  77. <TR>
  78.     <TD>Address2</TD>
  79.     <TD>
  80.     <INPUT type="text" name="Address2">
  81.     </TD>
  82. </TR>
  83. <TR>
  84.     <TD>city/town</TD>
  85.     <TD>
  86.     <INPUT type="text" name="city/town">
  87.     </TD>
  88. </TR><TR>
  89.     <TD>Distic</TD>
  90.     <TD>
  91.     <INPUT type="text" name="distic">
  92.     </TD>
  93. </TR>
  94. <TR>
  95.     <TD>State</TD>
  96.     <TD>
  97.     <SELECT name="STATE">
  98.         <OPTION VALUE="Select">Select</option>
  99.         <OPTION VALUE="Karnataka">Karnataka</OPTION>
  100.         <OPTION VALUE="Andra Pradesh">Andra Pradesh</OPTION>
  101.         <OPTION VALUE="Madya Pradesh">Madya Pradesh</OPTION>
  102.         <OPTION VALUE="Kerala">Kerala</OPTION>
  103.         <OPTION VALUE="Tamilnadu">Tamil Nadu</OPTION>
  104.         <OPTION VALUE="Uttara pradesh">Uttara Pradesh</OPTION>
  105.     </SELECT>
  106.     </TD>
  107.  
  108. <TR>
  109.     <TD>Pincode</TD>
  110.     <TD>
  111.     <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar" MAXLENGTH="6">
  112.     </TD>
  113. </TR>
  114.     <tr><td>
  115.     <FONT SIZE=4 align='center'>Certificate Factor</FONT></br>
  116. <input type="checkbox" name="option1" value="10th/SSLC markscard"><FONT SIZE=2 align='center'>10th/SSLC Markscard</br>
  117. <input type="checkbox" name="option2" value="12th/PUC Markscard">12th/PUC Marks card</br>
  118. <input type="checkbox" name="option3" value="TC">TC<br>
  119. <input type="checkbox" name="option4" value="Study Certificate">Study Certificate</br></font>
  120. </td></tr>
  121. <TR>
  122.     <TD COLSPAN=2>
  123.     <INPUT type="submit" value="submit" onClick="return validate()">
  124.     </TD>
  125. </TR>
  126. </Table>
  127. </body></html>
this is the code
Sep 22 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: R.G. Vervoort | last post by:
I am using a javafunction (onclick in select) in which i am calling a function in php (thats why i send this to both php and javascript newsgroups). in the onclick i call the function...
2
by: calfdog | last post by:
Hello, Does anyone know a workaround for calling fireEvent. With the latest from Microsoft OS XP2 and Hot fixes to IE it now gives an "access denied" error in Python when called. Here is what...
2
by: Asit | last post by:
In JavaScripts checks for an onChange event against the value of the textbox at the time of the last onChange event. Since an onChange Event never fired after you changed the text first time ,...
4
by: Zeebra3 | last post by:
Here goes: I have a web form with several asp:dropdownlists, with which, when selection is changed I want to fire an event defined in some clientside js. The content of the clientside code is...
3
by: b_naick | last post by:
I realize that the onChange event for a drop down can be trapped as follows: <select name="myDropDown" onChange="somefunc"> Is it possible to trap the onChange event outside of the select...
3
by: zzzbla | last post by:
Hi, I need to attach a javascript function I wrote to the onChange event of a <select> tag. However, I'm using a 3rd party tool that creates the html files - it only lets me add bits of html to...
4
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript"...
2
by: shankwheat | last post by:
<select name="ddlProfiles" onchange="location.href=frmProfiles.ddlProfiles.options.value;addOption_list()"> Is it possible to call 2 different functions using the onChange event from a...
7
by: Tim Slattery | last post by:
I'm trying to handle the onChange event in an <input type="file"> element. In IE, there's no problem: the event fires when a file in the "open" box is doubleclicked, or the "Open" button in the box...
22
by: DL | last post by:
Hi, What I wanted to do is to call a function from a newly created element. But it stumbled me. Here's the line that references the newly created element and I used the alert function for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.