Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Pass PHP Variable to Javascript function

Codes here !

Moderators: macek, egami, gesf

Pass PHP Variable to Javascript function

Postby Bassem » Tue Jul 16, 2013 11:14 pm

hi
i am new in PHP
i have a question please
can i run javascript functions which depend on variable that may change in server side ?
for more clarification kindly check the below code

it is a code of form

Code: Select all


<form method="post" action="insert.php" onSubmit="return insertmsg('<?php echo $confirmation ?>')">

    <table dir="rtl" align="center" >
   
    <tr>
    <td>Full name</td>
    <td> <input type="text" name="name" /> </td>
    </tr>
   
    <tr>
    <td>address</td>
    <td> <input type="text" name="address" /> </td>
    </tr>
   
    <tr>
    <td>birthdate</td>
    <td> <input type="date" name="bdate" /> </td>
    </tr>
   
    <tr>
    <td>tele</td>
    <td> <input type="text" name="tele" /> </td>
    </tr>
   
    <tr>
    <td>mobile</td>
    <td> <input type="text" name="mobile" /> </td>
    </tr>
   
    <tr>
    <td>image</td>
    <td> <input type="text" name="img" /> </td>
    </tr>
   
    <tr>
    <td colspan="2" style="text-align: center;"> <input type="submit" name="Add" /></td>
    </tr>
   
   
    </table>
   
     </form>



and here code of php inside the same page [ validation before insertion ]


Code: Select all


<?php

include 'Functions.php';

$name=$_POST['name'];
$add=$_POST['address'];
$bdate=$_POST['bdate'];
$tele=$_POST['tele'];
$mobile=$_POST['mobile'];
$img=$_POST['img'];
$submit=$_POST['Add'];

if($submit)
{
   connection();
   $length=strlen($tele);
   $error=array();
   
   if(!$name)
   {
    $error[]="Please Enter your name ";
   }
   elseif((is_numeric($name))||(preg_match('#[0-9]#',$name)))
   {
      $error[]= "No numbers allowed";   
   }
   
   
   if(!$tele)
   {
       $error[]="please enter your telephone number";
   }
   elseif  ($length!=10)
   {
            $error[]="telephone number must be 10 digit ! ";      
   }

   $confirmation=0;
   $len=sizeof($error);
   
   if($len>0)
   {
      foreach ($error as $items)
      {
         echo $items;
         echo "</br>";
      }
      $confirmation=0;
   }
   else
   {
      $confirmation=1;
//Function of insert
      insertson($name,$add,$bdate,$tele,$mobile,$img)   ;
   }
   
}

?>




and finally a Javascript function

Code: Select all

<script type='text/javascript'>
function insertmsg(var z)
{
   if(z==1)
   {
      var x=confsert these data ? ");
      if(x)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
   else
   {
      return false;
   }
}
</script>


All i want is :
customer will insert new data
senario 1 ) ..if he enter correct data , i need to show a confirm box , are u sure ? and then insert the data
senario 2 ) ..if he enter wrong data , validation error appeared and no need to run the java script function

in the above code i think to make a variable [ %confirmation ] and give this value to the JS function
but i also think that Javascript is a client side so it won't see the value of $confirmation witch run on the server ....kindly clarify this point and help me please
Thanks
Bassem
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Jul 16, 2013 10:53 pm

Re: Pass PHP Variable to Javascript function

Postby johnj » Wed Jul 17, 2013 8:38 pm

to combine what is happening on the client with server and the opposite, you need to use Ajax. Please read about Ajax and that will help you solve this.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.