4 Replies - 14134 Views - Last Post: 26 April 2007 - 04:28 PM

#1 kalou1675  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-April 07

Assigning a JavaScript function to a PHP variable

Posted 25 April 2007 - 03:00 PM

Hi

I am working on a php page which contains bits of javascript, html and php code.
One of the features of my programme offers an inputbox and a click button(which is of type "submit" )
The user enters a string (for example: "test") and clicks the button ,then a new php page opens displaying the output in a textarea.

Here is the code for the textarea:

<textarea name="Answerfield" id="ta" name="ta">
		  <?php
		  echo $Mark;
		 ?>
		 </textarea>


As you can see there is a php variable in the textarea that will just print whatever is defined by $Mark

I have written a javascript function called getMark which basically assignes a constant JS variable (an integer) to a specific a specific string of character.

This JS code works

<script language=Javascript>
			 var m10 = 10;
			 var m20 = 20;
			 var m30 = 30;
			
function getMark(input){

var inputMark = <?php echo $Mark; ?>";

var p ="";
			if (input == "test"){
			   p= m10 + " %";
		   		}

			
document.getElementById("ta").innerHTML = p;
document.f.ta.value = "p";
return p;

}

</script> 


What I am trying to achieve is to assign the getMark Javascript function to my $Mark PHP variable so that if I type "test" in the inputbox and it would print "10%" in the textarea.

I have created a JS variable which would link the getMark function to $Mark :

var inputMark = <?php echo $Mark; ?>";


Here is the line of code of the $Mark variable:

<?php 
		 $Mark = "?><script language=javascript>document.write('inputMark');
</script><?php";

?>


Obviously, it just prints "?><script language=javascript>document.write('inputMark');
</script><?php" in the textarea and ignores the inputMark variable which is linked to $Mark in the Javascript

Would you know what I need to do so that the GetMark javascript function could be assigned to the PHP variable $Mark?

<?php $Mark= ?>

Result , I have tried echo commands mixed with javascript doc.writes and I can manage to assign a php var to javascript var but not the other way around.I read something about having to recall the same form but I cant get the syntax correct.

I am new to php and I must admit I am stuck with this rather silly problem!

Thanks in advance for your precious advice,
Kalou

Is This A Good Question/Topic? 0
  • +

Replies To: Assigning a JavaScript function to a PHP variable

#2 Sifu_Socky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-February 07

Re: Assigning a JavaScript function to a PHP variable

Posted 25 April 2007 - 03:36 PM

I'm a bit dizzy, but couldn't you just take out the bits ?> and <?php either side of the
<script language=javascript>document.write('inputMark');
stuff?
Was This Post Helpful? 0
  • +
  • -

#3 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Assigning a JavaScript function to a PHP variable

Posted 25 April 2007 - 08:23 PM

PHP outputs data that is then parsed by the client (in this case a html browser). So everything PHP outputs whether it be (X)HTML or Javascript must be valid when it is outputted.

Using the $avariable = "?> SOMETHING <?php"; doesn't work because you are jumping out of the php engine. In other words, SOMETHING is just sent to the client without PHP even touching it.
Was This Post Helpful? 0
  • +
  • -

#4 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: Assigning a JavaScript function to a PHP variable

Posted 25 April 2007 - 09:23 PM

Not exactly true. $var = "?>test<?php"; works because it is just a string and is not being parsed by the php engine, so you're not jumping out of it.

Furthermore, you can't set php variables with javascript variables. It doesn't work that way. Doing $var = "<script language=javascript>document.write('test');</script>"; will output test to the browser because $var is not just "test," but that entire string of javascript and the browser is interpreting it. But if you tried to do anything based on that interpreted text on the assumption that $var is really the string "test", it'd be wrong.

Simply put, php cannot be assigned anything with javascript.

But you can change php values with forms. So if you resubmitted the form with the changed content or with a hidden variable, that would work, but not otherwise.
Was This Post Helpful? 0
  • +
  • -

#5 kalou1675  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-April 07

Re: Assigning a JavaScript function to a PHP variable

Posted 26 April 2007 - 04:28 PM

Thanks guys for your replies! I really appreciate your feedback even though I realise i won't be able to achieve what I want that way.

Styx, Thanks also for your explanation. That is correct, that does print out the entire string of Javascript at the top of my page.

I guess what I can do is create a php variable that does what my getMark javascript function does..basically I gonna have to do some study doh as php isn't really my cup of tea and I really don't have a clue how to do this now.

I will probably show you the php i'll generate and seek you expertise again, hopefully not ;)

Thanks a mil guys!

ATB
Kalou
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1