use javascript with php

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
aditi_19
Forum Newbie
Posts: 23
Joined: Sun Jun 21, 2009 3:09 pm

use javascript with php

Post by aditi_19 »

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 !

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&#058;submitPage('Add New','newuser.php')" />
<input type="submit" name="Update"  VALUE="Update" tabindex="2" onClick="javascript&#058;submitPage('Update','updateuser.php')" />
<input type="submit" name="Delete" VALUE="Delete" tabindex="3" onClick="javascript&#058;submitPage('Delete','deleteuser.php')"/>
</form>
 </body>
</html>
 
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: use javascript with php

Post by Eric! »

I'm admittedly stupid, so I can confess I don't understand your problem 100%.

PHP runs then javascript starts. To get them to talk again, javascript has to run another php script after some client action takes place.

However if your question is how to just pass data once from php to javascript, I wrote a short example here
viewtopic.php?f=1&t=101994&hilit=+javascript
aditi_19
Forum Newbie
Posts: 23
Joined: Sun Jun 21, 2009 3:09 pm

Re: use javascript with php

Post by aditi_19 »

Hey,

I got it working fine. Thanks a lot for your help !
Post Reply