Jump to content

Archived

This topic is now archived and is closed to further replies.

paul2463

PHP functions / Javascript variables

Recommended Posts

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

[code]
//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
}
}

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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
If you look at some ajax examples like this one http://www.tizag.com/ajaxTutorial/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 ;)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.