Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi this is my coding

JavaScript
function jsfun(var1_name)
{
document.getElementById('txth1').value=var1_name;

$.ajax(
{
 type: "POST",
 data: "v="+var1_name,
 success: function(){
   var y="<?php pfun();?>";// call to PHP function 
 	alert(y);
 }
});
return false;
}


PHP
<?php
function pfun(){
	
	echo($a);
}
?>

What i need is i have to pass the variable var1_name to tha PHP function pfun.
How to do this? Pls help.
Posted
Updated 27-Jan-20 4:06am
Comments
Killzone DeathMan 23-Nov-12 4:39am    
Why you just dont do the POST to a php file normally and there you work you variables and make an output? You can't just calling a php function like you put there...

JavaScript
function jsfun(var1_name)
{
  document.getElementById('txth1').value=var1_name;
 
  $.ajax(
  {
    type: "POST",
    data: "v="+var1_name,
    // call to PHP function 
    //suppose PHP file path is localhost/code/application.php
    url : localhost/code/application.php?fn=pfun,
    success: function(y){   
    alert(y);
    }
  });
  return false;
}


//write pfun function in www/code/application.php file
C#
if(isset($_GET['fn']))
{
if($_GET['fn']=='pfun')
pfun();
else 
exit;
}

function pfun(){

    echo "here";exit;
}
?>
 
Share this answer
 
v2
If you drop that jscript, here's a template for ajax->php:

<script type="text/javascript">
		function ajax_Call() { // THIS IS AN AJAX CALL TEMPLATE
			var xmlhttp = new XMLHttpRequest();
			
			xmlhttp.onreadystatechange=function() {
			   if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				   document.getElementById("ajax_Target_ID").innerHTML=xmlhttp.responseText;
			   } // if (xmlhttp.readyState==4 && xmlhttp.status==200)
			} // xmlhttp.onreadystatechange=function()
			xmlhttp.open("POST", "ajax_Script_File.php", false);
			xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			sendStr = 'something=' + something
			        + '&else='     + else;
			xmlhttp.send(sendStr);
		} // function ajax_Call()
	</script>



 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900