using Javascript inside PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

using Javascript inside PHP

Post by pritam79 »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi everyone, I have an input form "register.php" which the user uses to register. Its action field is " do_register.php". I have included javascript alert functions in " do_register.php" to check the user inputs. When I provide wrong inputs and click on 'submit' button, I get the correct error message in alert boxes but i am directed to a different page (do_register.php) whereas i want to stay on the "register.php" page so that the user can fill the form again before being directed to the "members.php" page. Please tell me how to do this.

This is the do_register.php page-

Code: Select all

 
<?php
include "header1.php";
?>  
<div id="content">
<?php  
  //include function files for this application
  require("PDMS_fns.php");
  
  // start session which may be needed later
  // start it now because it must go before headers
  session_start();
  
  // email address not valid
  if (!valid_email($email))
  {
    print("<script language = 'javascript'>alert('Not a valid email address');</script>");
    exit();
  }
  
  // check username length
   if(($username == "" ) || strlen($username)>16)
   {
    print("<script language = 'javascript'>alert('Username must be from 1 to 16 characters');</script>");
    exit();
  }
 
  // passwords not same
   if($passwd != $passwd2)
   {
    print("<script language = 'javascript'>alert('Passwords donot match');</script>");
    exit();
   }
   
// check password length is ok
// ok if username truncates, but passwords will get munged if they are too long.
 
if (strlen($passwd)<6 || strlen($passwd) >16)
   {
    print("<script language = 'javascript'>alert('Password must be between 6 to 16 characters');</script>");
    exit();
   }
    
// attempt to register
 $reg_result = register($username, $email, $passwd);
 if($reg_result == "true")
  {
   // register session variable
   $valid_user = $username;
   session_register("valid_user");
   
   // provide link to members page
   echo "Your registration was successful-go to members page and start";
   }
  else
  {
   // otherwise, provide link back,  tell them to try again
   echo $reg_result;
   exit();
  }
?>  
</div>  
<?php
include "footer.php";
?>
And this is the register.php page

Code: Select all

 
<?php
include "header1.php";
?>
<div id="content">
   <div id="left" style="left: 0px; top: 0px">
    <ul>
      <li>Email address :</li>
      <li>Preferred username :</li>
      <li>Password :</li>
      <li>Confirm password :</li>
    </ul>
   </div>
   <div id="right">
    <p style="left: -1px; top: 0px"><font size="4.5px">REGISTER</font></p>
    <form action="do_register.php">
    <input type="text" size="20" name="email"><br><br>
    <input type="text" size="20" name="username"> (max 16 chars)<br><br>
    <input type="password" size="20" name="passwd"> (6 to 16 chars)<br><br>
    <input type="password" size="20" name="passwd2"><br><br>
    <input type="submit" name="Submit" value="submit">
    <input type="reset" name="Reset" value="reset">
    </form>
    </div>
</div>
<?php
include "footer.php";
?>

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: using Javascript inside PHP

Post by novice4eva »

Hi there vani sri, I think you have more than one option to achieve this. The best option however i think for you will be to set the action to "register.php" itself and put all the validation and register function within register.php itself

Code: Select all

 
if($_SERVER['REQUEST_METHOD']=="POST"){
   $valid = true;
   if(!valid_email($email)){
       $valid=$valid & $false;
      alert('....');
   }
 
   if($valid){
     do the registration and show appropriate message or redirect to the desired page
   }
} 
 
Else you can use the same logic in "do_register.php". In this case you will have to check the $valid and then use javascript to redirect back to "register.php" in case of any error(JUST IN CASE: javascript for redirection: window.location.href="www.google.com";).

I hope it helps. :wink:
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: using Javascript inside PHP

Post by novice4eva »

Correction in line 5

Code: Select all

$valid=$valid && false;
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: using Javascript inside PHP

Post by pritam79 »

Could you be a little more specific as to which line of code from my script to replace with your suggested code?
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: using Javascript inside PHP

Post by devendra-m »

there is no javascript functions. could you post complete code of register.php
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: using Javascript inside PHP

Post by pritam79 »

devendra-m wrote:there is no javascript functions. could you post complete code of register.php
This is register_form.php

Code: Select all

<?php
include "header1.php";
?>  
<div id="content">
  
<table style="width: 768px"><form action="register_form.php" method="post">
    <tr>
        <td style="width: 394px; height: 53px;"></td>
        <td style="height: 53px"></td>
    </tr>
    <tr>
        <td style="width: 394px; text-align: right; height: 44px;">E-mail : </td>
        <td style="height: 44px"><input type="text" size="20" name="email"></td>
    </tr>
    <tr>
        <td style="text-align: right; width: 394px; height: 44px;">Preferred username : </td>
        <td style="height: 44px"><input type="text" size="20" name="username">(6 to 16 chars)</td>
    </tr>
    <tr>
        <td style="text-align: right; width: 394px; height: 44px;">Password :&nbsp; </td>
        <td style="height: 44px"><input type="password" size="20" name="password"> (6 to 16 chars)</td>
    </tr>
    <tr>
        <td style="text-align: right; width: 394px; height: 44px;">Re-enter password : </td>
        <td style="height: 44px"><input type="password" size="20" name="password2"></td>
    </tr>
    <tr>
        <td style="width: 394px; height: 68px;"></td>
        <td style="height: 68px"><input type="submit"  name="Submit" value="submit">
    <input type="reset" name="Reset" value="reset"></td>
    </tr></form>
</table>
  
</div>
<?php
include "footer.php";
?>
And this is do_register.php

Code: Select all

<?php
include "header1.php";
?>  
<div id="content">
<?php  
  //include function files for this application
  require("PDMS_fns.php");
  
  // start session which may be needed later
  // start it now because it must go before headers
  session_start();
 
  if (!valid_email($_POST['email']))
  {
    echo "<center>Not a valid email address</center>";
    exit;
  }
  
  // check username length
  
     if((!isset($_POST['username']) || strlen($_POST['username']) < 6 ) || (strlen($_POST['username']) > 16)) 
     {
      echo "<center>Username must be between 6 to 16 characters</center>";  
      exit;
      }
     if($_POST['password'] == $_POST['username'])
      {
       echo "<center>Username and password cannot be same</center>";
       exit;
      }
      
  // passwords not same
  
   if($_POST['password'] != $_POST['password2'])
   {
    echo "<center>Passwords donot match</center>";
    exit;
   }
   
// check password length is ok
 
if (strlen($_POST['password']) < 6 || strlen($_POST['password']) > 16)
   {
    echo "<center>Password must be between 6 to 16 characters</center>";
    exit;
   }
  
  // attempt to register after all validation is done
  
function register($username, $email, $password)
  
  // register new person with db
    // return true or error message
 
   {
   
    // connect to database
 
     $conn = db_connect();
     if(!$conn)
       {
         echo "Could not connect to database server- retry";
         exit;
        }
 
     // check if username is unique
     $result = mysql_query("select * from users where username='$username'");
       if(!$result)
        {
         echo "<center>Could not execute query</center>";
         exit();
        }
       if(mysql_num_rows($result)>0)
        {
         echo "<center>You are already registered</center>";
         exit();
        }
    // if ok put in db
     
     $result = mysql_query("INSERT into users VALUES('$username', '$password', '$email')");
     
       if(!$result)
        {
         echo "<center>Could not register-<a href='register_form.php'>try again</a></center>";
         exit;
        }
    return true;
    }  
 $reg_result = register($_POST['username'], $_POST['email'], $_POST['password']);
 if($reg_result == "true")
  {
   // register session variable
   $valid_user = $username;
   session_register("valid_user");
   
   // provide link to members page
   
   echo "<br><br><center>You have been registered as $valid_user -go to <a href='login.php'>login</a> page to start</center></br>";
 
   }
  else
  {
   // otherwise, provide link back,  tell them to try again
   echo "<br><br><center>Regiatration failed- <a href='register.php'>Retry</a></center>";
   exit;
  }
?>
</div>  
<?php
include "footer.php";
?>
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: using Javascript inside PHP

Post by devendra-m »

if you want to validate in the same page then you should keep all javascripts in register.php and validate when form submits
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: using Javascript inside PHP

Post by devendra-m »

you should call javascript function when form submits as following

<form onSubmit="return call_javascript_function()">
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: using Javascript inside PHP

Post by devendra-m »

you can validate fields in call_javascript_function() function
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: using Javascript inside PHP

Post by pritam79 »

I want all the validation to be done with PHP in register_form.php before posting and sending it to the server. I donot want to use javascript for this purpose. Is it possible to do so?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: using Javascript inside PHP

Post by califdon »

pritam79 wrote:I want all the validation to be done with PHP in register_form.php before posting and sending it to the server. I donot want to use javascript for this purpose. Is it possible to do so?
No. You must understand that PHP is a server language. Once the page is sent to the browser, there is no PHP remaining, so it cannot possibly respond to what a user does. That's what Javascript does, it is a client language, interpreted in the browser.
Post Reply