Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing a PHP command as a string

Status
Not open for further replies.

bradoirs

Technical User
Jul 8, 2009
35
0
0
GB
I want to call a PHP function based on information extracted from a form with javascript

hard-coded works fine:

if (x=="BSP"){
if (b){
var thecontent="<?php distincttype('BSP') ?>";
document.getElementById("BSP").innerHTML =thecontent;
}else{
document.getElementById("BSP").innerHTML ="";
}
}

What I am looking to do is change the php element depending on the content of a form element to replace the 'BSP' with the variable y
var y=document.forms["FindForm"]["myRef"].value; (assume value is "XYZ")

by creating a string variable thecontent
var thecontent="<?php distincttype('"+y+"') >?";

I can create the variable but it just doesnt work when the routine is substituted for the hard code.

Javascript remains a mystery to me so any help welcome and plkease be gentle with me :)


 
PHP runs BEFORE the javascript is even delivered to the browser, so you will have to use AJAX to call the PHP function.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
You can't. PHP and Javascript cannot directly interact. PHP is executed on the server. By the time JS runs, whatever PHP you have has already run, and delivered whatever it produced to the browser.

If you need to execute a PHP function based on a JS value, you'll need to look into Ajax.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top