Jump to content

PHP functions / Javascript variables


paul2463

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
Link to comment
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.
Link to comment
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 ;)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.