Passing variables from PHP to JavaScript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wafasn
Forum Newbie
Posts: 6
Joined: Tue Apr 06, 2004 11:10 am

Passing variables from PHP to JavaScript

Post by wafasn »

Hi guys,

Can anyone help me with this:

I have a php file called a.php with the following code:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 transitional//EN"-->
<?php require"../../config.php"?>
<html>
<head>
<script language="JavaScript">
   function formSubmit() 
	{
    
		var formName="<?echo($form_name);?>";
		alert(formName);
	}
 
</script>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#333366">
 <tr>
	<td width='100'>first name</td>
	<td width='100'>last name</td>
	<td width='100'>Options</td>
 </tr>

      <? displayUsers() ?>
</table>
</body>
</html>
<? function displayUsers()
{
	for($i=0;$i<2;$i++)
	{
     $form_name="form".$i;
	 $fname="A".$i;
	 $lname="B".$i;
      echo" <tr>
	  <td> $fname</td>
	   <td> $lname</td>
       <td>
		<form name=$form_name>
		<input type='button' value='Delete' onClick='formSubmit();'>
		</form>
		</td>
		</tr>";
	  
	}
  }
?>
[Edit: Added PHP tags for eyecandy. --JAM]

when I click on delete button, I dont get the name of the form in the alert box, I dont know what's wrong with my code.
thanks in advance.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

whats the source of the generated page look like? And I don't see and opening form tag
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

oh yea there it is! well, its after the closing body and html tags for one thing. Also, view the source and see what the javascript function looks like
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

wafasan said:
Hi, this is the source code for the generated pagr, it seems that javaScript doesn't get the form name:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 transitional//EN"-->
<html>
<head>
<script language="JavaScript">
var formName = ;
function formSubmit()
{
alert("This form has the following name: "+formName);
}

</script>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#333366">
<tr>
<td width='100'>first name</td>
<td width='100'>last name</td>
<td width='100'>Options</td>
</tr>

<tr>
<td> A0</td>
<td> B0</td>
<td>
<form name=form0>
<input type='button' value='Delete' onClick='formSubmit();'>
</form>
</td>
</tr> <tr>
<td> A1</td>
<td> B1</td>
<td>
<form name=form1>
<input type='button' value='Delete' onClick='formSubmit();'>
</form>
</td>
</tr>

</table>
</body>
</html>
keep it public in case someone else has a similar problem!

the above source was not generated by the way above code - where are you adding the "This form has the following name:" ? cause you're not concatenating right for starters...
wafasn
Forum Newbie
Posts: 6
Joined: Tue Apr 06, 2004 11:10 am

passing variables from PHP to javaScript

Post by wafasn »

Hi,

the HTML code genereated is the one above!
Thanks.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

i know it is, but what script generated it? And the form has no defined action - so how can you submit it? and you are trying to echo form_name in the javascript before you declare it in the bottom - you should move your php before the html
Post Reply