Page 1 of 1

Passing variables from PHP to JavaScript

Posted: Mon Apr 19, 2004 9:59 am
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.

Posted: Mon Apr 19, 2004 10:16 am
by magicrobotmonkey
whats the source of the generated page look like? And I don't see and opening form tag

Posted: Mon Apr 19, 2004 10:59 am
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

Posted: Mon Apr 19, 2004 11:14 am
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...

passing variables from PHP to javaScript

Posted: Mon Apr 19, 2004 11:34 am
by wafasn
Hi,

the HTML code genereated is the one above!
Thanks.

Posted: Mon Apr 19, 2004 11:42 am
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