Hi, all

I want to embed some javascript code in php for user interactive
message. For example, when user entered string to the number field and hit the save button, I want to give user a message like "number is required in this field, please try again". I want to display this message in a popup box not in the the same page.

What should I code in php ?

Thanks

hongmei

    The easiest thing to do is write a link to an external Javascript file and then call the validation during for submission

    example...

    echo "<script language='JavaScript' src='forms.js' type='text/javascript'></script>"

    then place the external javascript file in the same directory.

    you could also do the same thing with the actual javascript, but the first way makes things a little cleaner.

      Thank you for your kindly response!

      But I still donot understand where should I insert this line of code in php and how to interact with .js file. Could you please provider me a simple example for .js content and php code to call .js.

      Thanks again!

      hongmei

        The php code does not call the .js file, basically when php builds the web page, you just need toput the same code in that you would for a normal html file using javascript. If you post the code that you want to add the javascript to and let me know exactly what you want to do with it, I will try to help.

          Thanks for your help!

          Here is my problem:

          I wrote a simple function called test()
          code:
          function test()
          {
          alert ("hello world!");
          return;

          }

          I saved this function as "forms.js".

          In my HTML section of php code.
          I inserted one line

          <script language="JavaScript" src="forms.js" type="text/JavaScript"></script>

          after <head></head>

          Then in html I have
          <input type="Submit" name="test" value="test" onClick="return test();">

          So, when I click test button, it should give me "hello world!" message alert. But it didn't work. I keep getting javascript error "test is not a function....."

          What should I do?

          Thanks again

          hongmei

            Hi

            First make sure the forms.js file is in the same directory as your php file. then move the script tag you have between the head tags (although I don't think it makes any difference) and see if it still fails. If that does not work, embed the actuall function inside the head tag and try that. if that does not work, post the whole page code here and I will take a look.

              Here is the whole page code. Thanks!

              <html>
              <?php
              $db = mysql_connect("anansi.nesdis.noaa.gov","hlu","flozz34");
              mysql_select_db("TES3devo",$db);
              if (isset($POST['save'])) {
              echo "save button has been clicked";
              $errors = array();
              $password1= $
              POST['password1'];
              echo "password filled in is".$password1;

              $username1= $_POST['username1'];
              echo "user name is ".$username1;
               //echo "logon has been submitted";
              
               if ($username1=="") {
              
                 	 $errors[] = 'Please enter user name';
              
              	}
               else{
              	//$db = mysql_connect("anansi.nesdis.noaa.gov","hlu","flozz34");
              	//mysql_select_db("TES3devo",$db);
              
              	$sql =" select user_name from TES_USERS where user_name='$username1'";
              
                 		$result = mysql_query($sql,$db);
              	$userdata = mysql_fetch_array ($result, MYSQL_ASSOC);
              
              	//echo "password in database is ".$userdata['password'];
              
              	if ($userdata['user_name']!="" ) 
              	{
              
              	 $errors[] ='This user already exists, please enter another user name!';
              
              	}
              
               }
               if ($password1=="") {
                 	 $errors[] = 'Please enter your password ';
              }
              
              
              if (count($errors) == 0) {
                 	 //assume using mysql database (that is connected and selected)
              
                  	if (mysql_query("INSERT INTO TES_USERS(user_name,password) 

              VALUES('$username1', '$password1')"))
              {
              echo 'Your information has been stored';
              display ==true;

                  	}
                  	else {

              display==false;
              echo 'Error: could not write to database';
              }

              	}
              else {

              display==false;
              echo 'Sorry, you have not filled the required fields<br>';
              foreach ($errors as $error) {
              echo $error . '<br>';
              }
              }
              }
              ?>
              <head>
              <title>Untitled Document</title>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

              </head>

              <body bgcolor="#6699CC" text="#000000">
              <script language='JavaScript' src='forms.js' type='text/javascript'>
              function test()
              {
              alert ("hello world!");
              return;

              }
              </script>
              <table border=0 cellspacing=0 cellpadding=0 align="center" width="337" >
              <form method="post" action="createUser.php" >

              <tr> 
                <td colspan="4"> 
                  <div align="center">
                    <p><b><font face="Arial, Helvetica, sans-serif">Create New TES 

              Users</font></b></p>
              <p>&nbsp;</p>
              </div>
              </td>
              </tr>

              <tr> 
                <td colspan="1" width="160" ><b>Enter User Name: </b></td>
                <td colspan="3" width="232"> 
                  <input type="text" name="username1" value="<?php

              if (isset($POST['username1'])) {
              echo htmlspecialchars($
              POST['username1']);
              }?>">
              </td>
              </tr>
              <tr>
              <td colspan="1" width="160"><b>Enter your Password:</b></td>
              <td colspan="1" width="232">
              <input type="password" name="password1" size="30" value="<?php
              if (isset($POST['password1'])) {
              echo htmlspecialchars($
              POST['password1']);
              }?>" >
              </td>
              </tr>

              <tr> 
                <td colspan="4">&nbsp; </td>
              </tr>
              
              
              <tr> 
                <td colspan="4">&nbsp; </td>
              </tr>
              
              <tr> 
                <td colspan="4"> 
                  <div align="center">
                    <input type="Submit" name="save" value="Save" >
                    <input type="Reset" name="reset" value="Reset">

              <input type="Submit" name="test" value="test" onClick="return test();">

                  </div>
                </td>
              </tr>

              </form>
              </table>
              </body>
              <?php
              ?>
              </html>

                Ok, first, it looks like 'test' is a reseved keyword in javascript, so change the function name to test1 (or anything besides test). Then use either the Javascript external .js file call or embed the javascript right in the page. examples are below.

                Use this:

                <script language='JavaScript' src='forms.js' type='text/javascript'>

                and put the function in the external .js file

                or use this:

                <script language='JavaScript' type="text/javascript">
                <!--

                function test1()
                {
                alert ("hello world!");
                return false;

                }
                // -->
                </script>

                and put that right in the page.

                the current syntax you have for the javascript is incorrect.

                I tried this and it works both ways.

                  is this what your looking for ??
                  page = test.php
                  <script language="JavaScript" type="text/javascript">
                  <!--
                  function validate(){
                  if(document.test_form.number.value==""'){
                  alert("you need to enter a number");
                  document.test_form.number.focus();
                  return;
                  }
                  document.test_form.submit();
                  }
                  //-->
                  </script>
                  <form name="test_form" action="test_action.php" method="post">
                  <input type="text" name="number">
                  <input type="button" name="submit" value="submit" onClick="javascript:validate();">
                  </form>

                  this does a test to see if number is blank, its just like how you would do it for a html page.

                  reg
                  kevin

                    How can I get the return value from dataValidate() and apply it to
                    if (isset($_POST['save'] {
                    //here I want to code

                    if passed validation, then go ahead to process data save. otherwise, do not go into this save process.

                    My problem is that even validation is not passed, it still go through the save process.
                    What should I do?

                    Thanks a lot! You are a big helper!
                    }

                    here is my dataValidate() function:

                    function dataValidate()
                    {
                    //check whether task name and task title has value before save

                    if (document.screenform.txtTaskname.value.length ==0 ){
                    alert ("You must enter taskname!");
                    document.screenform.txtTaskname.focus();
                    return;
                    }
                    if (document.screenform.txtCcrTitle.value.length ==0){
                    alert ("You must enter task title!");
                    document.screenform.txtCcrTitle.focus();
                    return;
                    }
                    document.screenform.submit();
                    }

                    here is my save button HTML:
                    <input type="Submit" name="save" value="Submit data" onclick="dataValidate()">

                    Here are my code for when save button submitted: They basically get the value from HTML form and insert into database and then tell user that data has been saved...

                    if (isset($REQUEST["save"]))
                    {
                    echo " save button has been submitted";
                    $db = mysql_connect("anansi.nesdis.noaa.gov","hlu","flozz34");
                    mysql_select_db("TES3devo",$db);
                    $id=$
                    POST['txtCcrId'];
                    $title =$POST['txtCcrTitle'];
                    $taskname =$
                    POST['txtTaskname'];
                    $product=$POST['selProduct'];
                    $initiator =$
                    POST['txtInitiator'];
                    $initPriority =$POST['selInitPriority'];
                    $Palpriority =$
                    POST['selPALPriority'];
                    $status=$POST['selStatus'];
                    $totalHours= $
                    POST['txtTotalHours'];
                    $remainHours =$_POST['txtRemainingHours'];
                    if ($HTTP_POST_VARS['chkProDesc'] =="1"){

                    $ProDesc =1;
                    //echo "PDD check box is". $ProDesc;
                    }
                    else{

                    $ProDesc =0;
                    //echo "PDD check box is". $ProDesc;
                    }
                    if ($HTTP_POST_VARS['chkInterControl'] =="1"){

                    $InterControl =1;
                    //echo "IC check box is". $InterControl;
                    }
                    else{
                    $InterControl =0;
                    //echo "IC check box is" .$InterControl;
                    }
                    if ($HTTP_POST_VARS['chkSysDesc'] =="1"){

                    $SysDesc =1;
                    //echo "check box is" .$SysDesc;
                    }
                    else{

                    $SysDesc =0;
                    //echo "check box is". $SysDesc;
                    }
                    if ($HTTP_POST_VARS['chkUserManu'] =="1"){

                    $UserManu =1;
                    //echo "check box is" .$UserManu;
                    }
                    else{

                    $UserManu =0;
                    //echo "check box is". $UserManu;
                    }
                    if ($HTTP_POST_VARS['chkMainManu'] =="1"){

                    $MainManu =1;
                    //echo "check box is" .$MainManu;
                    }
                    else{

                    $MainManu =0;
                    //echo "check box is" .$MainManu;
                    }
                    if ($HTTP_POST_VARS['chkOperManu'] =="1"){

                    $OperManu =1;
                    //echo "check box is" .$OperManu;
                    }
                    else{

                    $OperManu =0;
                    //echo "check box is" .$OperManu;
                    }
                    if ($HTTP_POST_VARS['chkOperEvent'] =="1"){

                    $OperEvent =1;
                    //echo "check box is". $OperEvent;
                    }
                    else{

                    $OperEvent =0;
                    //echo "check box is" .$OperEvent;
                    }

                    if ($HTTP_POST_VARS['chkOthers'] =="1"){

                    $Others =1;
                    //echo "check box is" .$Others;
                    }
                    else{

                    $Others =0;
                    //echo "check box is". $Others;
                    }

                    $descTask =$POST['descTask'];
                    $ReasonChange =$
                    POST['ReasonChange'];
                    $EffectChange=$POST['EffectChange'];
                    $alternatives =$
                    POST['alternatives'];
                    $ProblemRes = $POST['ProblemRes'];
                    $ImpleRequ =$
                    POST['ImpleRequ'];
                    $AssignPersonnel =$POST['AssignPersonnel'];
                    $PalDispo =$
                    POST['PalDispo'];
                    $FormDate =$POST['FormDate'];
                    $DispoDate =$
                    POST['DispoDate'];
                    $StartDate =$POST['StartDate'];
                    $closeDate =$
                    POST['closeDate'];
                    $sql = "INSERT INTO TES
                    values($id,'$title','$taskname','','$product','$initiator','$initPriority','$Palpriority','$

                    status',$totalHours,$remainHours,
                    $ProDesc,$InterControl,$SysDesc,$UserManu,$MainManu,$OperManu,$OperEvent,$Others,'$descTask'

                    ,'$ReasonChange',
                    '$EffectChange','$alternatives',
                    '$ProblemRes','$ImpleRequ','$AssignPersonnel',
                    '$PalDispo','$StartDate','$DispoDate',
                    '$FormDate','$closeDate')";
                    $result = mysql_query($sql,$db);

                    echo "Thank you! Information entered.\n";
                    //document.screenform.action="search_hlu.php";
                    }

                      1. are you submitting the data from your form to the same page or to another page ??
                      2. paste the code for the form page in full here.
                      3. Also next time do not printout your host,username,pwd like here
                        $db = mysql_connect("anansi.nesdis.noaa.gov","hlu","flozz34");

                      my suggestion is that you change your username and pwd asap.

                      reg
                      kevin

                        That is my testing server machine.

                        Everything is only for testing only, no real data.

                        I submit data from and to the same page.

                        Why do you need my whole HTML page ? Other parts are not related at all.

                          1. when u submit the form and the data is blank do you get a javascript alert and then the form still submits ??

                          2. why dont u try to check if the data input is blank ??

                          instead of
                          document.screenform.txtCcrTitle.value.length == 0
                          try
                          document.screenform.txtCcrTitle.value == " "

                          1. in javascript instead of return; try return false;

                          2. the reason i wanted to see the html is to see how you had defined the input type in the form for txtCcrTitle and the rest of the form

                          3. see if this works

                          function dataValidate()
                          {
                          //check whether task name and task title has value before save
                          var error = 0;
                          if (document.screenform.txtTaskname.value=="" ){
                          alert ("You must enter taskname!");
                          document.screenform.txtTaskname.focus();
                          error = 1;
                          return false;
                          }
                          if (document.screenform.txtCcrTitle.value==""){
                          alert ("You must enter task title!");
                          document.screenform.txtCcrTitle.focus();
                          error = 1
                          return false;
                          }
                          if(error == 0){
                          document.screenform.submit();
                          }
                          }
                          reg
                          kevin.

                            Write a Reply...