Jump to content


PHP and Javascript


3 replies to this topic

#1 jakevfr

    Young Padawan

  • Members
  • Pip
  • 40 posts

Posted 23 September 2006 - 10:14 AM

Is there a way to pass javascript variables in to PHP on the spot? (without going to another file)

Here is what I'm attempting to do, but if you know a better way I would appreciate you sharing it :)
function create(name,age)
{
<? $name = name; echo $name; ?> = new character(name,age);
}
function character(name,age)
{
this.name = name;
this.age = age;
}
Any and all help would be appreciated :D

Edited by jakevfr, 23 September 2006 - 10:15 AM.


#2 NGPixel

    Senior Programmer

  • P2L Staff
  • PipPipPipPip
  • 1,410 posts
  • Gender:Male
  • Location:Montreal, Canada
  • Interests:Web Design : Coding : Animation

Posted 23 September 2006 - 10:25 AM

PHP is parsed BEFORE javascript so what you're trying to do is not possible. You need to send the vars the another PHP file then return the data. You could also use some AJAX if you don't want to load another page.

#3 jakevfr

    Young Padawan

  • Members
  • Pip
  • 40 posts

Posted 23 September 2006 - 11:10 AM

So ajax is the only way... alright thanks.

Is there a way to do what I'm doing with nothing but javascript (making it so the name entered is the name of the object)

Ex. If the name entered is "Jerry" then the code would become
Jerry = new character("Jerry",24)
How would I do something like that?

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 September 2006 - 03:04 PM

eval('Jerry = new character("Jerry",24);');
That would be one way. But honestly, without knowing what you're doing, i find this completely ridiculous. Typically when program, you want to use a variable that is generalized, so it's value or function can change and you won't have to go through the rest of the code and change the variable's name.

Edited by rc69, 24 September 2006 - 03:04 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users