SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why Javascript not work in php

    Hi everyone,
    I have problems in php script,

    1: First javascript cannot show errror from common.php.
    2: I want to put Print button in the code where it display the result.
    3: I want to validate the both txt box for icnumber and confirm icnumber
    how it will work in php, i put the javascript code in the head of the script.


    My all coding is below for admin.php:
    PHP Code:
    <?php 
    include("db.php");
    include(
    "common.php");

    if (!isset(
    $_POST['submitok'])):
        
    // Display the user signup form
        
    ?>
        <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><script language="JavaScript"><!--
    function validateForm() 
    {
     var okSoFar=true
     with (document.admin)
     {
       
      if (icnumber.value=="" && okSoFar)
      {
        okSoFar=false
        alert("Please enter IC Number.")
        icnumber.focus()
      }
      if (cicnumber.value=="" && okSoFar)
      {
        okSoFar=false
        alert("Please confirm IC Number.")
        cicnumber.focus()
      }
      var e1 = icnumber.value
      var e2 = cicnumber.value
      if (!(e1==e2) && okSoFar)
      {
        okSoFar = false
        alert ("IC Numbers does not match.  Please re-enter.")
        icnumber.focus()
      }
       
      if (okSoFar==true)  submitok();
     }
    }
    // --></script>
      <title> Access Code Generator Form </title>
      <meta http-equiv="Content-Type"
        content="text/html; charset=iso-8859-1
    </head>
    <body>

    <h3> Access Code Generator Form </h3>
     <p><font  color="orangered" size="+1"><tt><b>*</b></tt></font>
    <u>indicates a required field</u></p>
    <form name="admin" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <table align="center" border="0" cellpadding="0" cellspacing="5">

        <tr>
            <td align="right">
                <p>IC Number :</p>
            </td>
            <td>
                <input name="icnumber" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font>
            </td>
        </tr>
        
        <tr>
            <td align="right">
                <p>Confirm IC Number :</p>
            </td>
            <td>
                <input name="cicnumber" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font>
            </td>
        </tr>
        
        
        
        <tr>
            <td align="right" colspan="2">
                <hr noshade="noshade" />
                <input type="reset" value="Reset Form" />
                <input type="submit" name="submitok" value="Generate Code"      ONCLICK="javascript:validateForm()" />
                
            </td>
        </tr>
    </table>
    </form>

    </body>
    </html>


     <?php 
       
    else:
        
    // Process  submission
        
    dbConnect('db');
        
      
            
        
    // Check for existing user with the ic number
        
    $sql "SELECT COUNT(*) FROM m_user WHERE icnumber = '$_POST[icnumber]'";
        
    $result mysql_query($sql);
        if (!
    $result) {    
            
    error('A database error occurred in processing your '.
                  
    'submission.\\nIf this error persists, please '.
                  
    'contact [email]you@example.com[/email].');
        }
        if (
    mysql_result($result,0,0)>0) {
            
    error('A user already exists with your chosen userid.\\n'.
                  
    'Please try another.');
        }
        
        
    $random rand(1000000,9999999990);  
        
        
        
    $sql "INSERT INTO m_user SET
                  icnumber = '
    $_POST[icnumber]',
                  accesscode = ('
    $random'),
                  Actdate = now();
                  "
    ;
        if (!
    mysql_query($sql))
            
    error('A database error occurred in processing your '.
                  
    'submission.\\nIf this error persists, please '.
                  
    'contact [email]admin@straight-a.com.my[/email] .\\n' mysql_error());
                
        
    ?>
        <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
          <title> Code Generation Complete </title>
          <meta http-equiv="Content-Type"
            content="text/html; charset=iso-8859-1" />
        </head>
        <body><center>
               <?php
    $con 
    mysql_connect("localhost","root","adil");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("db"$con);

    $result mysql_query("SELECT icnumber,accesscode,actdate FROM m_user where
     icnumber = '
    $_POST[icnumber]'
    "
    );

    echo 
    "<table align=Center CELLSPACING='0'BORDERCOLOR='#007FFF' BGCOLOR='cornsilk' border='1'>

    <tr><th colspan=12>User Activation details</th>
    <tr>
    <th>IC Number</th>
    <th>Access Code</th>
    <th>Current Date</th>

    </tr></tr>"
    ;while($row mysql_fetch_array($result))
      {
      echo 
    "<tr>";
      echo 
    "<td>" $row['icnumber'] . "</td>";
      echo 
    "<td>" $row['accesscode'] . "</td>";
      echo 
    "<td>" $row['actdate'] . "</td>";
      
      echo 
    "</tr>";
      }
    echo 
    "</table>";mysql_close($con);


    <
    html>
    <
    form>
    <
    input type="button" value="Print This Page" onClick="window.print()" />
    </
    form>

    </
    html>

    ?></strong></a>
           </p>
        </body>
        </html>
        <?php
    endif;

    ?>

    When i put the print button in the php code it display error,
    And if a user enter ic number which is in database it show the error message:

    <? =$msg ?> in a popup window.

    here is my commom.php code:
    PHP Code:
    <?php // common.php

    function error($msg) {
        
    ?>
        <html>
        <head>
        <script language="JavaScript">
        <!--
            alert("<? =$msg ?> ");
            history.back();
        -->
        </script>
        </head>

        </html>
        <?php
        
    exit;
    }
    ?>
    Plz any body if have some idea tell me whats wrong with this code.
    thanks....
    Last edited by mmarif4u; Dec 19, 2006 at 21:01.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php is a serverside language.
    javascript is a clientside language.

    i dont think you understand the difference.
    http://www.sitepoint.com/article/adv...esign-primer/1

    use bbcode tags when posting code.
    eg
    [php]
    code goes here
    [/php]

  3. #3
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks for reply

    Yes i know that php is server side lang and javascript is client side.
    But there is no way to put java code in php for validation or others.

    plz help me on this issue.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you asking how to duplicate your javascript validation with php code?

    edit your post with bbcode tags.

  5. #5
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I edit it

    I edit the post plz take a look on
    the code now...

    I want that php first check the txt box that the values match or not
    if ok than save to table,if not than error msg, and what wrong with
    the common.php bcoz it cannot display the correct error msg, and where
    to put the javascript code for print button.

  6. #6
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I Got It

    The Error Msg which is not displayed i got it,
    now its working well , But other problems still have.

  7. #7
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also correct the another problem now php code check two
    fields for confirmation.


    Only one problem remain that is where to put the print button code.

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '
    <form>
    <input type="button" value="Print This Page" onClick="window.print()" />
    </form> 
    '


  9. #9
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot clamcrusher ,

    Its working.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •