View Full Version : php inside javascript and viceversa
Hello!
How to call to a function in javascript sending a numerical parameter? This parameter will be used in php code inside javascript:
call the javascript function:
<A href='javascript:void(0)' onClick='ShowFAQ(25)'>text</A>
<script type="text/javascript">
function ShowFAQ(faqid){
window.document.labeltitlefaq.innerText='<?php echo($arrayFAQtitle[faqid])?>' ;
}
</script>
The previous code does not work, and if I replace "faqid" to a number, it works fine.
Thanks for your support
Geuis
11-25-2005, 09:07 PM
I'm not sure if I understand the grammar of your question.
Are you trying to pass a value from PHP to your javascript function?
Forgive my English, I talk spanish from Argentine
I try to pass a value from html to javascript function and
inside of the javascript to pass a PHP and not work.
the $arrayFAQtitle is a golbal var php and work fine if to pass a var num
for example: this is OK
<script type="text/javascript">
function ShowFAQ(faqid){
window.document.labeltitlefaq.innerText='<?php echo($arrayFAQtitle[25])?>' ;
}
</script>
ralph l mayo
11-25-2005, 11:14 PM
Forgive my English, I talk spanish from Argentine
I try to pass a value from html to javascript function and
inside of the javascript to pass a PHP and not work.
the $arrayFAQtitle is a golbal var php and work fine if to pass a var num
for example: this is OK
<script type="text/javascript">
function ShowFAQ(faqid){
window.document.labeltitlefaq.innerText='<?php echo($arrayFAQtitle[25])?>' ;
}
</script>
You're misunderstanding the fundamentals of server side versus client side scripting. You can use PHP to generate javascript no problem, but javascript can't generate PHP because by the time it's called the page is out of the hands of the server. Web browsers don't interpret PHP, and the user will just see raw code.
edit: you CAN pass variables from javascript to PHP by refreshing the page with ?get=foo stuff, but if it gets to that point there's almost certainly a better way to accomplish whatever you're trying to do. The point is that if you want PHP interpretation after the page has been passed to the client you have to hit the server again.
LuiePL
09-13-2006, 09:25 AM
I'm having a similar issue. I'm trying to update the name and info fields after a different member number is selected from a drop-down list. I got it to work so when you select a different name, it says it on the page.
How would I go about sending info back to the server in a function after it's been created? I know it's done with other "AJAX" programs, such as logging in, so it has been done before.
I'm trying to start off simple with just retreiving the first and last names in the table. Below is my code.
echo "<SCRIPT language='JavaScript'>\n";
echo "<!--\n";
echo "function nameChange(Name){\n";
echo "var memberName = Name;";
$number = Name;
$dbh = mysql_connect ("localhost", "name", "password") or die ('Database Connection Error: ' . mysql_error());
mysql_select_db ("database");
$query = "SELECT * FROM Users WHERE Number='$number'";
$sql = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($sql);
echo "var strName = \"The name is: \"+memberName+\"<br />\";";
echo "strName = strName+\"First Name:<input type='text' name='fname' size='20' value='".$row['FirstName']."'>\";";
echo "strName = strName+\"<br>Last Name:<input type='text' name='lname' size='20' value='".$row['LastName']."'>\";";
echo "document.getElementById('divName').innerHTML = strName;";
echo "}\n";
echo "// -->\n";
echo "</SCRIPT>\n";
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.