use javascript with php
Posted: Mon Jul 06, 2009 10:55 am
I have a class called viewusers.php to view existing users with 3 buttons to add, delete or update. In order to direct the current page to either of these 3 pages, javascript is used. I have a radio button attached with each username displayed in the table to select a user before clicking either delete/update button.
I want the user to select a radio button(attached with each user detail) before directing the page to either update/delete,else display an error message to select a username. For this i have to check whether any radio button($id) is set or not.
I am unable to convert php variable $id to javascript variable and check whether it is set or not.
How can i do a check whether any radio button is set before directing it to the button click page, and if not set, display an error message on the same page?
Please help me !
I want the user to select a radio button(attached with each user detail) before directing the page to either update/delete,else display an error message to select a username. For this i have to check whether any radio button($id) is set or not.
I am unable to convert php variable $id to javascript variable and check whether it is set or not.
How can i do a check whether any radio button is set before directing it to the button click page, and if not set, display an error message on the same page?
Please help me !
Code: Select all
<?php
session_start();
<html>
<head>
<script type="text/javascript">
function submitPage(buttonName, nextPage)
{
var page = nextPage;
var button = buttonName;
if (button == "Add New")
{
document.userform.action = page;
document.userform.submit();
}
else if (button == "Update")
{
document.userform.action = page;
document.userform.submit();
}
else if (button == "Delete")
{
document.userform.action = page;
document.userform.submit();
}
}
</script>
</head>
<body>
<form name="userform" method="post">
<table width="800" height="500" border="1" align="center">
<thead>
<tr>
<?php
$user = "root";
$host = "localhost";
$pswd = "project";
$dbname = "project" ;
mysql_connect($host,$user,$pswd);
mysql_select_db($dbname);
$query = "SELECT UserID, FirstName, MiddleInitial, LastName, Classification,SecurityGroup from tbluserlist;";
$result= mysql_query($query);
for ($i=0 ;$i<mysql_num_fields($result); $i++)
{
echo("<TH><h6>".mysql_field_name($result,$i) . "</h6></TH>");
}
echo("<TH><h6>"."Selector" . "</h6></TH>");
?>
</tr>
<tr></tr>
</thead>
<tbody>
<?php
for ($i=0 ;$i<mysql_num_rows($result); $i++)
{
$k=0;
echo("<tr>");
$row_array = mysql_fetch_row($result);
for($j=0 ;$j<mysql_num_fields($result); $j++)
{
echo("<td> <center>".$row_array[$j] . "</center></td>");
}
echo("<td><center>". "<INPUT TYPE=RADIO NAME=id VALUE= $row_array[$k] >" . "</center></td>");
}
?>
</tbody>
</table>
<input type="submit" name="Add New" VALUE="Add" tabindex="1" onClick="javascript:submitPage('Add New','newuser.php')" />
<input type="submit" name="Update" VALUE="Update" tabindex="2" onClick="javascript:submitPage('Update','updateuser.php')" />
<input type="submit" name="Delete" VALUE="Delete" tabindex="3" onClick="javascript:submitPage('Delete','deleteuser.php')"/>
</form>
</body>
</html>