calling a php file onchange event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • creddyc
    New Member
    • Mar 2010
    • 2

    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....a nd creates an array....

    how can i access the values?
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    From what you are saying is hard to work out what is it that you are trying to do. Is there some code?

    Comment

    • creddyc
      New Member
      • Mar 2010
      • 2

      #3
      Code:
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <script type="text/javascript">
      
      function callPHPScript(url, val) {
          var w = document.refererForm.refererList.selectedIndex;
      	alert(w);
      	var selected_text = document.refererForm.refererList.options[w].text;
      	alert(selected_text);
      	window.location=url+"?"+val;
      }
      </script>
      </head>
      
      <table class=tableBorder border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
      <td>
      <table border="0" cellpadding="3" cellspacing="1" width="100%">
          <tr class=rowHeader>
              <td class="colHeaderLink" width="100%" colspan="3">XYZ <b><a href="xxxx.jspa?id=$project.getLong('id')">$project.getString('name')</a></b></td>
          </tr>
          <tr class=refererHeader width="100%">
      	<td class="colHeaderLink" colspan="1">Referrer:</td>
      	<td class="colHeaderLink" colspan="2">
      	<form name="refererForm">
      	<select name="refererList" onChange="callPHPScript('http://localhost:9999/xxxx.php', 'projectId=123&referer=abc')">
              <option value="1">All</option>
      		#if ($DbReferrers)
      		    #set ($row = 2)
      		    #foreach ($value in $DbReferrers)
                      <option value="$row">$value.get(0)</option>
      				$row = $row + 1
      			#end
      		#end
          </select>
      	</form>
      	</td>
      	</tr>
      </table>
      </td>
      </tr>
      </table>
      </html>
      -------------------
      The php code looks some this like this....

      Code:
      <?php
      
      function executeSQLQuery($sql) {
          $result = mysql_query($sql);
          if (!$result) {
              die("Query to show fields from table failed");
          }
          $value = mysql_fetch_row($result);
          mysql_free_result($result);
          return ($value[0]);
      }
      
      $db_host   = 'localhost';
      $db_user   = 'user';
      $db_pwd    = 'password';
      $database  = 'db';
      $projectId = $_GET['projectId'];
      $referer   = $_GET['referer'];
      
      if (!mysql_connect($db_host, $db_user, $db_pwd))
          die("Can't connect to database");
      
      if (!mysql_select_db($database))
          die("Can't select database");
      
      // Construct all the SQL queries requierd
      // that need to be executed for getting the
      // values from the database
      
      //constructs queries...based on passed params...
      //cals the execute funcion...
      $table = array();
      $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
      Last edited by Dormilich; Mar 5 '10, 06:25 AM. Reason: Please use [code] tags when posting code

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        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.

        Comment

        • rbuczynski
          New Member
          • Mar 2010
          • 10

          #5
          To elaborate on zorgi's answer, Ajax is probably what you're looking for.

          If you're unfamiliar, please read about it here:
          Sorry! We can't seem to find the resource you're looking for


          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.responseTex t property. The process might look something like this:



          In PHP, if you execute this code:
          Code:
          <?php
          print $array;
          ?>
          You get an output that looks like this:
          Code:
          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:
          Code:
          alert(req.responseText);
          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:
          Code:
          <?php
          $table = array( 'key' => 'value' , 'anotherKey' => 'anotherValue' );
          ?>
          Then you could output the data like this:
          Code:
          <?php
          foreach($table as $key=>$val) {
            echo "$key=$val\n";
          }
          ?>
          Which will produce the following output:
          Code:
          key=value
          anotherKey=anotherValue
          This data will then be returned to your page and is accessed through the req.responseTex t 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!

          Comment

          • philipwayne
            New Member
            • Mar 2010
            • 50

            #6
            If I understand correctly use json_encode on the array in PHP then use JavaScript's eval function

            Code:
            <?php
            
            $my_ary = array( "lol", "rtm", "dry" );
            
            ?>
            <script>
            var my_ary = eval( "<?php echo json_encode( $my_ary ); ?>" );
            </script>
            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_f ile' )";

            Hope that helps.

            Comment

            • vivekgs2007
              New Member
              • Mar 2010
              • 62

              #7
              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

              Comment

              • vivekgs2007
                New Member
                • Mar 2010
                • 62

                #8
                i was tryied in java script but it showing error

                Comment

                • vivekgs2007
                  New Member
                  • Mar 2010
                  • 62

                  #9
                  Code:
                  <html><head>
                  <SCRIPT language=Javascript>
                        
                        function isNumberKey(evt)
                        {
                           var charCode = (evt.which) ? evt.which : event.keyCode
                           if (charCode > 31 && (charCode < 48 || charCode > 57))
                              return false;
                  
                           return true;
                        }
                  	  function rld()
                  	  {
                  	  document.frm.action="stud5.php";
                  	  document.frm.submit();
                  	  }
                  	
                        </SCRIPT>
                     </head>
                  <body>
                  <FORM method=post name='frm'>
                  <h1><font color="blue">Student details</font><h1>
                  
                  <TABLE BORDER=10>
                  <TR>
                      <TD>Student Name</TD>
                  	<TD>
                  	<INPUT type=text name="StudentName">
                  	</TD>
                  </TR>
                  
                  <TR>
                  	<TD>Father Name</TD>
                  	<TD>
                  	<INPUT type=text name="Father Name">
                  	</TD>
                  </TR>
                  <TR>
                  	<TD>course</TD>
                  	<td>
                  	<?php
                  	$S1="";
                  	$S2="";
                  	$S3="";
                  	$S4="";
                  	if($course=='MBA')
                  		$S1="selected";
                  	elseif($course=='MCA')
                  		$S2="selected";
                  	elseif($course=='Bsc')
                  		$S3="selected";
                  	elseif($course=='Bcom')
                  		$S4="selected";
                  	?>
                  	
                  	<SELECT name="course" onchange="rld()">
                  	    <OPTION VALUE="">Select</OPTION>
                  		<OPTION VALUE="MBA"> MBA </OPTION>
                  	    <OPTION VALUE="MCA"> MCA </OPTION>
                  		<OPTION VALUE="Bsc">Bsc </OPTION>
                  		<OPTION VALUE="Bcom">Bcom </OPTION>
                  		</select>
                  	</TD>
                  	<TR>
                  	<TD>Gender</TD>
                  	<TD>
                  	Male: <INPUT type="radio" name="gender" value="M">
                  	Female: <INPUT type="radio" name="gender" value="F">
                  	</TD>
                  </TR>
                  <TR>
                      <TD>Address1</TD>
                  	<TD>
                  	<INPUT type="text" name="Address1" >
                  	</TD>
                  </TR>
                  <TR>
                      <TD>Address2</TD>
                  	<TD>
                  	<INPUT type="text" name="Address2">
                  	</TD>
                  </TR>
                  <TR>
                      <TD>city/town</TD>
                  	<TD>
                  	<INPUT type="text" name="city/town">
                  	</TD>
                  </TR><TR>
                      <TD>Distic</TD>
                  	<TD>
                  	<INPUT type="text" name="distic">
                  	</TD>
                  </TR>
                  <TR>
                  	<TD>State</TD>
                  	<TD>
                  	<SELECT name="STATE">
                  	    <OPTION VALUE="Select">Select</option>
                  		<OPTION VALUE="Karnataka">Karnataka</OPTION>
                  		<OPTION VALUE="Andra Pradesh">Andra Pradesh</OPTION>
                  		<OPTION VALUE="Madya Pradesh">Madya Pradesh</OPTION>
                  		<OPTION VALUE="Kerala">Kerala</OPTION>
                  		<OPTION VALUE="Tamilnadu">Tamil Nadu</OPTION>
                  		<OPTION VALUE="Uttara pradesh">Uttara Pradesh</OPTION>
                  	</SELECT>
                  	</TD>
                  
                  <TR>
                      <TD>Pincode</TD>
                  	<TD>
                  	<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar" MAXLENGTH="6">
                  	</TD>
                  </TR>
                  	<tr><td>
                  	<FONT SIZE=4 align='center'>Certificate Factor</FONT></br>
                  <input type="checkbox" name="option1" value="10th/SSLC markscard"><FONT SIZE=2 align='center'>10th/SSLC Markscard</br>
                  <input type="checkbox" name="option2" value="12th/PUC Markscard">12th/PUC Marks card</br>
                  <input type="checkbox" name="option3" value="TC">TC<br>
                  <input type="checkbox" name="option4" value="Study Certificate">Study Certificate</br></font>
                  </td></tr>
                  <TR>
                  	<TD COLSPAN=2>
                  	<INPUT type="submit" value="submit" onClick="return validate()">
                  	</TD>
                  </TR>
                  </Table>
                  </body></html>
                  this is the code
                  Last edited by Dormilich; Sep 23 '10, 11:28 AM. Reason: please use [code] [/code] tags when posting code

                  Comment

                  Working...