Skip to content
Advertisement

is there a way to exchange variables between JavaScript and PHP

I have build a website in Drupal, and I am trying to create a fun way to get members involved with the site by building a userpoint system, the system is all in place, but I’m trying to make a shop where they can buy ‘titles’.

This is the script I wrote for the shop, with a bit of error handling, but I’m stuck with a problem,

In my JavaScript, I have the function buyitem( ) with 2 variables, which I want to use in my PHP functions which check everything in my database, is there a way to get those variables from JavaScript to the PHP function I wrote without going to an external PHP file?

<?php
    include "php-scripts/DBConnection.php";
    $con = getconnection();
    mysql_select_db("brokendi_BD", $con);
    
    
    function getKarma()
    {
           $result = mysql_query("SELECT * FROM userpoints WHERE uid='getUID()'");
           $row = mysql_fetch_array($result);
           $currentkarma = (int)$row['points'];
           
          return $currentkarma;
    }
    
    function getUID()
    {
         global $user;
         if ($user->uid) 
         { 
              $userID=$user->uid;
              return $userID;
         }
         else
        {
              header('Location: http://brokendiamond.org/?q=node/40');
        }
    }
    
    function hasRole($roleID)
    {
           $usersid = getUID();
           $returnValue = false;
           $result = mysql_query("SELECT * FROM users_roles");
           while ($row = mysql_fetch_array($result))
          { 
               if ($row['uid'] == $usersid)
               {
                      if ($row['rid'] == $roleID)
                      {
                              $returnValue = true;
                              break;
                      }
               }
          }
          return $returnValue;
    }
    
    function enoughKarma()
    {
            if ( getKarma() >= $requiredKarma)
            {
                     return true;
            }
            else
            {
                    return false;
            }      
    }
    
    function buyRole()
    {
           $currentKarma = getKarma();
           $newkarma = $currentKarma - $requiredKarma;
           $userID = getUID();
           mysql_query("UPDATE userpoints SET points = '$newkarma' WHERE uid='$userID'");
           mysql_query("INSERT INTO users_roles (uid, rid) VALUES ($userID, $roleID)");
    }
    ?>
    
    <script type="text/javascript">
    buyItem(1 , 0);
    function SetStore()
    {
    
    
    }
    
    Function buyItem(itemID,reqKarma)
    {
           if (<?php enoughKarma(); ?>)
           {
                 <?php buyRole(); ?>
           }
           else
           {
                alert('You do not have enough Karma to buy this title.');
           }
    }
    </script>

Advertisement

Answer

PHP is a server side script and javascript is a client side, server side scripts are executed before the page loads, meaning your javascript cannot pass variables to it, BUT you can how ever pass php variables to your js.

Best solution in your case is to use ajax to send those variables to php and have the php set variables on it’s side, this doesn’t quite solve your problem, but with some creativity in your code you can make it happen.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement