Hello all,
I have same problem.
in javascript i called php fun using <?php fun1()?>. it run successfully. But i want to pass variable to that function which is a value of input text box.
i.e var tt=document.getElementById('cname');
and i want to pass that tt value to php function.

How to do this?
Please help...

Thanks,
Shubhada

Recommended Answers

All 7 Replies

How do you want the variable to be passed? Without reloading the page?

If u have an input form in a html body like this:

<form name="myForm" method="get" action="formreceiver.php">
<input type="text" name="myText" />
<input type="submit" value="Submit"/>
</form>

The the sending string will show:

formreceiver.php?myText=Hello

Then u can retrieve the value from the form input in php file called "formreceiver.php" and at the same time print it out like this:

<?php
if(isset($_GET['myText'])){echo "The text input is: " . $_GET['myText'];}
?>

It is tested and verified on a LAMP server.

U can also omit the static html form and make it on the fly with Javascript. Still, u need some html like this:

<input type="text" id="myText" />
<input type="button" value="Submit" onclick="javascript:setSubmit();"/>
<div id="myForm"></div>

And to run it, the Javascript can look like this:

<script type="text/javascript" language="javascript">
function setSubmit(){
  $value=document.getElementById("myText").value;
  $info='<form name="myForm" method="get" action="formreceiver.php">';
  $info+='<input type="hidden" name="myText" value="'+$value+'" >';
  $info+='</form>';
  document.getElementById("myForm").innerHTML=$info;
  document.myForm.submit();
}
</script>

The php file called "formreceiver.php" in previous reply can be used.
It is tested and verified on a LAMP server.

You can also use AJAX to pass the variable behind the scenes so to speak.

Using a php file only to change a value displayed from within a php variable to a html dom value and back to php for valuation, this example would be enough to satisfy the requirements:

<?php
$msg="?";
if(isset($_GET['myText'])){printSmiley($_GET['myText']);}
function printSmiley($myText){
  global $msg;
  switch($myText){
    case "happy" : 
      echo "The smiley is happy";
      $msg="sad";
      break;
    case "sad" : 
      echo "The smiley is sad";
      $msg="happy";
      break;
    default : break;
  }
}
echo '
<form name="myForm" method="get" action="myText.php">
<input type="text" name="myText" value="'.$msg.'"/>
<input type="submit" value="Submit"/>
';
?>

The php file is called "myText.php".
It is tested and verified on a LAMP server.

Here's an article on your subject "" can you help it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.