DigiMartKe Posted November 4, 2015 Share Posted November 4, 2015 i have this on my drop box; (Part) <select name="Symptom" id="Symptomid" onchange="LSC()"> Then i have(java script); <script> function LSCJ(){ <?php LSC();?> } </script> Then i have (PHP function) <?php function LSC() { echo "Hello world Changed!"; } ?> when i change my choice in dropbox the code LSC should run How can i achieve this? what am i doing wrong? Please not that i have run a very complex php code . This is just a sample. Any help given will be highly appreciated . Regards Azhar Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/ Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2015 Share Posted November 4, 2015 You cannot call PHP functions from javascript. PHP and javascript are two completely different languages, they both run at different times. PHP runs on the server when the .php file is requested. Javascripts runs in browser when it receives the response from the request. The only way you can do this is via ajax, whereby you send a http request to your php script. In the PHP script you have PHP code which then will call the LCS PHP function. Example JavaScript code, using JQuery ajax method <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> function LSC() { // using jquery ajax method for calling the php script $.ajax({ // set this to the url of your php script for calling the LSC function url: 'lsc_script.php', // if the result of the ajax request is ok then this function is called success: function(response) { // the variable 'response' will contain the output from your php script // as an example we'll use a javascript alert to show the output of the response alert(response); } }); } </script> Example code for your PHP script <?php function lsc() { echo "Hello world has changed"; } // call lsc function lsc(); Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525431 Share on other sites More sharing options...
DigiMartKe Posted November 4, 2015 Author Share Posted November 4, 2015 (edited) i have tried running this code. I saved the first part in ajax.html and the second to lsc_script.php But when i run ajax.html, Nothing happens.Blank Page loads. What am i doing wrong? Edited November 4, 2015 by DigiMartKe Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525435 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2015 Share Posted November 4, 2015 But when i run ajax.html, Nothing happens.Blank Page loads. In ajax.html you do have your <select></select> drop down menu right? Also open your browser developers console (should open when pressing F12 and clicking the console tab). It will mention any javascript errors. Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525445 Share on other sites More sharing options...
DigiMartKe Posted November 4, 2015 Author Share Posted November 4, 2015 Understood , but now i have another two issues; 1.The echo is not displayed on the form rather as a popup. I have pasted the screen shot. See Error1.pdf 2. My header page had three pictures , one showing after the other , Now all showing at once , some java script is conflicting. Can it be so? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525483 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2015 Share Posted November 4, 2015 1.The echo is not displayed on the form rather as a popup. I have pasted the screen shot. See Error1.pdf Its displaying as popup because of the use of alert(response); Where in the form do you want the response to show? You need to show your form code. 2. My header page had three pictures , one showing after the other , Now all showing at once , some java script is conflicting. Can it be so? No idea. My code has nothing to do with your header. Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525491 Share on other sites More sharing options...
DigiMartKe Posted November 4, 2015 Author Share Posted November 4, 2015 (edited) This is my full code <?php require_once('Connections/Connshifa.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_Connshifa, $Connshifa); $query_RsSymptom = "SELECT * FROM symptoms ORDER BY symptoms.name"; $RsSymptom = mysql_query($query_RsSymptom, $Connshifa) or die(mysql_error()); $row_RsSymptom = mysql_fetch_assoc($RsSymptom); $totalRows_RsSymptom = mysql_num_rows($RsSymptom); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Cure by Symptom</title> <?php // Start the session session_start(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/style.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" type="text/css" href="css/coin-slider.css" /> <style type="text/css"> .main .content .content_resize .mainbar .article label { font-weight: bold; } </style> <script type="text/javascript" src="js/cufon-yui.js"></script> <script type="text/javascript" src="js/cufon-aller.js"></script> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="js/script.js"></script> <script type="text/javascript" src="js/coin-slider.min.js"></script> </head> <body> <div class="main"> <div class="header"> <div class="header_resize"> <div class="slider"> <div id="coin-slider"></div> <div class="clr"></div> </div> <div class="clr"></div> </div> </div> <div class="content"> <div class="content_resize"> <div class="mainbar"> <div class="article"> <h2>Find Your Cure</h2> <p class="infopost"> </p> <div class="clr"></div> <div class="clr"></div> </div> <div class="article"> <h2>Every Disease has a Cure</h2> <?php echo "Welcome " . $_SESSION["username"] . " !" . ".<br>"; ?> <form id="form1" name="form1" method="post" action=""> <label for="Uname">User Name/Email </label> <input name="Uname" type="text" id="Uname" value="<?php echo $_SESSION["username"] ?>" readonly="readonly" /> <form method="post"> </form> <label for="Symptom"><br /> Select your Symptom <br /> <br /> Symptom</label> <select name="Symptom" id="Symptomid" onchange="LSC()"> <?php do { ?> <option value="<?php echo $row_RsSymptom['name']?>"><?php echo $row_RsSymptom['name']?></option> <?php } while ($row_RsSymptom = mysql_fetch_assoc($RsSymptom)); $rows = mysql_num_rows($RsSymptom); if($rows > 0) { mysql_data_seek($RsSymptom, 0); $row_RsSymptom = mysql_fetch_assoc($RsSymptom); } ?> <br /> <br /> <br /> <br /> <br /> <p id="demo"></p> <script> function Symselect() { var x = document.getElementById("Symptomid").value; document.getElementById("demo").innerHTML = "You selected: " + x; } </script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> function LSC() { // using jquery ajax method for calling the php script $.ajax({ // set this to the url of your php script for calling the LSC function url: 'LoadSymptomDetails.php', // if the result of the ajax request is ok then this function is called success: function(response) { // the variable 'response' will contain the output from your php script // as an example we'll use a javascript alert to show the output of the response alert(response); } }); } </script> <?php //require("classes\LoadSymptomDetails.php"); ?> <br /> System</label> </form> <select name="System" id="System"> </select> <p class="infopost"> </p> <p class="infopost"> </p> </div> </div> </div> </div> <div class="footer"> <div class="footer_resize"> <div style="clear:both;"></div> </div> </div> </div> <div align=center>This template downloaded form <a href='http://all-free-download.com/free-website-templates/'>free website templates</a></div></body> </html> <?php mysql_free_result($RsSymptom); ?> and what i to call when a symptom is chosen is(as i want the system other columns to be displayed). I need to do this as when the columns are displayed in a table and a system chosen , the cures also shown in a table. So this code is to be used in two places; Although the code i have below is based on a post , i need it to run based on the combobox change. <?php $data = array(); if(isset($_POST['search'])){ $symptoms_name = $_POST['Symptom']; $sql1 = mysql_query("SELECT name,description , comments FROM symptoms WHERE symptoms.name ='$symptoms_name'") or die(mysql_error()); while($row1 = mysql_fetch_assoc($sql1)){ $data[] = $row1; } } ?> <?php if($data){ ?> <table border="1"> <tr> <td>name</td> <td>description</td> <td>comments</td> </tr> <?php if($data){ foreach($data as $sy){ ?> <tr> <td><?php echo $sy['name']; ?> </td> <td><?php echo $sy['description']; ?> </td> <td><?php echo $sy['comments']; ?> </td> </tr> <?php } } ?> </table> Edited November 4, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525515 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2015 Share Posted November 4, 2015 (edited) Oh so you also need to have the selected symptom submitted too? First change onchange="LSC()" to be onchange="LSC(this)". Now change your LSC function to be <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> function LSC(option) { // using jquery ajax method for calling the php script $.ajax({ // set this to the url of your php script for calling the LSC function url: 'LoadSymptomDetails.php', // send POST request method: 'POST', // set the Symptom name value to be submitted data: {Symptom: option.value}, // if the result of the ajax request is ok then this function is called success: function(response) { // the variable 'response' will contain the output from your php script // as an example we'll use a javascript alert to show the output of the response alert(response); } }); } </script> And I assume you want the response of the ajax request to be added to the <p id="demo"></p> element? In that case replace alert(response); with $('p#demo').html(response); Edited November 4, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525521 Share on other sites More sharing options...
DigiMartKe Posted November 6, 2015 Author Share Posted November 6, 2015 (edited) I have tried to follow your instructions , but have not been able to achieve. Nothing happens on change of dropbox value. I have made the following changes;(maybe based on this you can tell me what i have missed.) dropbox <select name="Symptom" id="Symptomid" onchange="LSC(this)> HTML have added the line <p id="demo"></p> Java script <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> function LSC(option) { // using jquery ajax method for calling the php script $.ajax({ // set this to the url of your php script for calling the LSC function url: 'LoadSymptomDetailsfridaytest.php', // send POST request method: 'POST', // set the Symptom name value to be submitted data: {Symptom: option.value}, // if the result of the ajax request is ok then this function is called success: function(response) { // the variable 'response' will contain the output from your php script // as an example we'll use a javascript alert to show the output of the response $('p#demo').html(response); } }); } </script> and the php LoadSymptomDetailsfridaytest.php (a bit simplified) <?php require_once('Connections/Connshifa.php'); ?> <?php $data = array(); { mysql_select_db("shifa",$Connshifa ); $sql1 = mysql_query("SELECT name,description , comments FROM symptoms ") or die(mysql_error()); while($row1 = mysql_fetch_assoc($sql1)){ $data[] = $row1; } } ?> <?php if($data){ ?> <table border="1"> <tr> <td>name</td> <td>description</td> <td>comments</td> </tr> <?php if($data){ foreach($data as $sy){ ?> <tr> <td><?php echo $sy['name']; ?> </td> <td><?php echo $sy['description']; ?> </td> <td><?php echo $sy['comments']; ?> </td> </tr> <?php } } ?> <?php } ?> </table> Kindly assist. Edited November 6, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525777 Share on other sites More sharing options...
Solution DigiMartKe Posted November 6, 2015 Author Solution Share Posted November 6, 2015 Thanks , sorted <select name="Symptom" id="Symptomid" onchange="LSC(this)> should have been <select name="Symptom" id="Symptomid" onchange="LSC(this.value)"> Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525779 Share on other sites More sharing options...
DigiMartKe Posted November 6, 2015 Author Share Posted November 6, 2015 see also http://www.w3schools.com/php/php_ajax_database.asp Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525780 Share on other sites More sharing options...
DigiMartKe Posted November 6, 2015 Author Share Posted November 6, 2015 one question , the code <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> if the site is down ? How can this be handled? Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525782 Share on other sites More sharing options...
Ch0cu3r Posted November 6, 2015 Share Posted November 6, 2015 You can just download jquery and link to it locally rather the linking to a remote source if you want to. Also note when posting code to wrap it within tags or click <> button in the editor. Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525803 Share on other sites More sharing options...
DigiMartKe Posted November 6, 2015 Author Share Posted November 6, 2015 (edited) I still have a problem further down, I have now changed the code for LoadSymptomDetails.php and it works. <?php require_once('Connections/Connshifa.php'); ?> <?php //$q = 1 ?> <?php $data = array(); { mysql_select_db("shifa",$Connshifa ); //This works $sql1 = mysql_query("SELECT name,description , comments FROM symptoms" ) or die(mysql_error()); while($row1 = mysql_fetch_assoc($sql1)){ $data[] = $row1; } when i change the php code to <?php require_once('Connections/Connshifa.php'); ?> <?php $data = array(); { mysql_select_db("shifa",$Connshifa ); $symptoms_name = $_POST['Symptom']; $sql1 = mysql_query("SELECT name,description , comments FROM symptoms WHERE symptoms.name ='$symptoms_name'") or die(mysql_error()); while($row1 = mysql_fetch_assoc($sql1)){ $data[] = $row1; } } ?> <?php if($data){ ?> <table border="1"> <tr> <td>name</td> <td>description</td> <td>comments</td> </tr> <?php if($data){ foreach($data as $sy){ ?> <tr> <td><?php echo $sy['name']; ?> </td> <td><?php echo $sy['description']; ?> </td> <td><?php echo $sy['comments']; ?> </td> </tr> <?php } } ?> <?php } ?> </table> When i make these changes , it stops working. I get an error Notice: Undefined index: Symptom in C:\xampp\htdocs\sh\server\LoadSymptomDetails.php on line 6 So how can i pass the selected value in the dropbox to the where part of the select statement. Kindly assist Edited November 6, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525831 Share on other sites More sharing options...
Ch0cu3r Posted November 6, 2015 Share Posted November 6, 2015 So how can i pass the selected value in the dropbox to the where part of the select statement. The code I gave earlier should be submitting the POST Symptom value via ajax when you make a change in the dropdown menu. I have tested it and it words ok for me. To debug. in my code, add beforeSend: function(xhr, obj) { alert(obj.data); }, After data: {Symptom: option.value}, When you make a selection in the drop down it should display an alert with text like "Symption=<the value you have selected>" This the last time I'm going to say this Also note when posting code to wrap it within tags or click <> button in the editor. Next time will issue warning. Quote Link to comment https://forums.phpfreaks.com/topic/299110-calling-a-php-function-from-javascript/#findComment-1525836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.