2 Replies Latest reply on Aug 2, 2006 2:57 PM by Josh_Pratt

    How well does Flash & PHP mix?

    Josh_Pratt
      I haven't worked much with forms yet in Flash. The existing site that I'm replacing with a flash site uses some PHP to interact with a MySQL database. It is for an email list that users can sign-up for or remove themselves. I'm wondering if I will be able to still (at least somewhat easily) do the same thing through Flash. Here's the code currently used:

      <?php
      switch ($_GET['a']) {
      case "s":subscribe();break;
      case "r":remove();break;
      default: subscribe();
      }
      function subscribe() {
      require_once 'database.php';
      require_once 'admin/lib.php';
      $db = new Database('eaglebro_lists');
      $show_form = true;

      if ($_POST) {
      if (is_email($_POST['email'])) {
      $email = format_email($_POST['email']);
      $uniqid = make_uniqid();
      $db->query("REPLACE users (name, email, uniqid, created) VALUES ('$_POST[name]','$email','$uniqid',NOW()) ");
      $id = mysql_insert_id($db->db);
      $db->query("REPLACE subscriptions (listid, userid, created) VALUES ('11','$id',NOW()) ");
      $show_form = false;
      echo '<h3>Success!</h3><p>Thank you for subscribing to the Route 212 email newsletter!</p><hr /><p><a href="/">Route 212 Home Page</a></p>';
      } else {
      echo "<h3>Error!</h3><p>The email address you entered appears to be invalid. Check for any errors and submit the form again.</p><hr />";
      }
      }
      if ($show_form == true) {
      ?>
      <p>Sign up to receive periodic email about Route 212 events and news.</p>
      <form method="post" action="/interact/updates.html?a=s">
      <p>
      <label class="m" for="f_name">Name:</label><input id="f_name" type="text" class="text m" name="name" />
      <label class="m" for="f_email">Email:</label><input id="f_email" type="text" class="text m" name="email" />
      <p><input id="f_submit" type="submit" class="button s" value="Subscribe" /></p>
      </form>
      <?php
      }
      }

      function remove() {
      require_once 'database.php';
      $db = new Database('eaglebro_lists');

      //if email address is provided
      if ($_GET['uid'] && $_GET['lid']) {
      //perform database removal and show confirmation
      $id = $db->query_fetch("SELECT id FROM users WHERE uniqid='$_GET[uid]'");
      $db->query("SELECT * FROM subscriptions WHERE userid='$id[id]' AND listid='$_GET[lid]'");
      if (mysql_num_rows($db->result)) {
      $db->query("DELETE FROM subscriptions WHERE userid='$id[id]' AND listid='$_GET[lid]'");
      $list = $db->query_fetch("SELECT name FROM lists WHERE id='$_GET[lid]'");
      echo "<h4>Unsubscription Successful</h4><p>You have been removed from the following list: <b>$list[name]</b></p>";
      echo '<hr /><p><a href="/">Route 212 Home Page</a></p>';
      } else {
      $list = $db->query_fetch("SELECT name FROM lists WHERE id='$_GET[lid]'");
      echo "<h4>Cannot Unsubscribe</h4><p>You have either already unsubscribed from the following list: <b>$list[name]</b>, or an
      error has occured.</p>";
      echo '<hr /><p><a href="/">Route 212 Home Page</a></p>';
      }
      } else {
      echo "<h4>Cannot Unsubscribe</h4><p>There is information missing.</p>";
      echo '<hr /><p><a href="/">Route 212 Home Page</a></p>';
      }
      }
      ?>
        • 1. Re: How well does Flash &amp; PHP mix?
          charmcityMD
          Josh,

          I'm much more comfortable with PHP that Flash too. You won't have a problem getting the two to work together. Its pretty easy actually.

          The key in my limited experience is to have your PHP to run as normal and then communicate what you want to display to the user via one of Flash's external data connection classes. There is a great chapter on this in the book "flash 8 action script" by Adobe --- the easiest way is to just use the "loadVars" class to accept a string you've generated as output of your PHP file in the format: &variable = data &variable = data etc...

          Also here is a good site with an example I've found that was helpful in getting the two languages to work together:
          http://webmonkey.wired.com/webmonkey/03/40/index0a_page3.html?tw=programming
          This example actually walks you through the communication between all 3 --> MySQL, PHP and Flash.

          Good luck!!
          Tom
          • 2. Re: How well does Flash &amp; PHP mix?
            Josh_Pratt Level 1
            Thanks Tom! That is some good feedback on my question. I'll look into trying some stuff you mentioned and I'll check out that example too. It seems like a daunting task being so new to flash, but hopefully it goes smoother than it seems it will go.