Jump to content


Photo

PHP functions / Javascript variables


  • Please log in to reply
3 replies to this topic

#1 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 24 October 2006 - 12:00 PM

I realise the server / client side argument of the two languages I was just wondering if there was a way around my little problem

I have a page that allows a person to add a type of material a piece of equipment is made of, I have a javascript function that initiates a window.prompt() dialog box in fact code first questions later

//javascript function 
function addMaterialType() {
var $answer = window.prompt("Enter a new material");
if ($answer != "") {
<?=InsertMaterial($answer)?>; // a php function to add the material to the database
}
}

now...... if I place a value in the php function call i.e.
<?=InsertMaterial("Wood")?>;

the  function works beautifully, adding Wood , but only Wood everytime of course
but as the origonal function stands it adds nothing to the database because the php function does not recognise the javascript variable even though its starts with a $, is there any way I can make this function work as it should or do I have to start thinking in a new direction all together?

many thanks in advance

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 24 October 2006 - 12:38 PM

There is no way to get your javascript to populate a PHP variable in that way. Think of it like this: by the time  your page loads, your PHP code is not there any longer. It has already been parsed out by the server. You only have HTML and Javascript code available to the browser. So, when your Javascript function runs, there actually is no PHP variable to be populated. If you are wanting to allow a Javascript function to update a database or other database content on your page, you'll need to make an HTTP request and send the value of your variable to another script to run your update for you.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 24 October 2006 - 12:45 PM

Thanks for that Obs

much appreciated
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#4 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 25 October 2006 - 08:46 AM

If you look at some ajax examples like this one http://www.tizag.com...orial/index.php you will see how you can use events to triger a call to your php page, also parse variables to your php and get the php to send strings to the javascript to work with.  Database action is a bit more tricky, but with a little google action I'm sure you can work it out ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users